Wio terminal hangs after ~565 receive cycles.
I want to listen for specific ble advertisements with info in mfr data.
I’ve tried many settings and work-arounds b4t the only thing that works is hard reset of the WIO with NVIC_SystemReset();
Using scan callback vs. result crashes in about 450 scans.
I’m amazed it’s not a known problem…
Or am I doing something wrong?
Blockquote #include <rpcBLEDevice.h> #include <BLEScan.h> #include <BLEAdvertisedDevice.h> #include <TFT_eSPI.h>
//#include “rpcSystem.h” // Required for rpc_system_reboot()
//-------------------------------------------------------------------
// Global variables
// TFT display
TFT_eSPI tft = TFT_eSPI();
// Scan parameters & counters
const int scanTime = 1; // Scan time in seconds
BLEScan* pBLEScan;
unsigned long lastScanTime = 0;
unsigned long scanCount = 0;
unsigned long deviceCount = 0;
//-------------------------------------------------------------------
// Initialize BLE scanner and display
void setup() {
// Initialize serial communication
Serial.begin(115200);
// int scount=0;
// while(!Serial) {//serial check with timeout
// sleep(100);
// if(scount++ > 50);
// break;
// }
// Initialize TFT display
tft.begin();
tft.setRotation(3); // Landscape mode
tft.fillScreen(TFT_BLACK);
// Set header
tft.setTextSize(2);
tft.setTextColor(TFT_GREEN);
tft.drawString(“BLE Scanner”, 20, 20);
setupBLEScanner(); // Initialize BLE
updateDisplay(); // Initial display update
}
//-------------------------------------------------------------------
// loop: Main program loop - scans for devices once per second
void loop() {
// time to scan (once per second) ?
if (millis() - lastScanTime >= 1000) {
lastScanTime = millis();
scanCount++;
// Log scan start
Serial.print(“Start Scan #”);
Serial.print(scanCount);
BLEScanResults results = pBLEScan->start(scanTime, false); // false means not continuous.
deviceCount = results.getCount();
// Log scan result
Serial.print(" found: “);
Serial.println(deviceCount);
pBLEScan->clearResults(); // Clear the scan results immediately
// Update the display
updateDisplay();
}
delay(100);
}
//-------------------------------------------------------------------
// updateDisplay: Updates the scan counts on TFT
void updateDisplay() {
// Clear display area
tft.fillRect(0, 60, 320, 180, TFT_BLACK);
// Display scan count
tft.setTextSize(3);
tft.setTextColor(TFT_YELLOW);
tft.drawString(“Scan count:”, 20, 80);
tft.setTextColor(TFT_RED);
tft.drawString(String(scanCount), 240, 80);
// Display device count for last scan
tft.setTextSize(3);
tft.setTextColor(TFT_YELLOW);
tft.drawString(“Found:”, 20, 130);
tft.setTextColor(TFT_RED);
tft.drawString(String(deviceCount), 160, 130);
}
//-----------------------------------------------------------------
// BLE stack and scanner setup
void setupBLEScanner() {
BLEDevice::init(”");
delay(300);
pBLEScan = BLEDevice::getScan();
pBLEScan->setActiveScan(false);
pBLEScan->setInterval(50);
pBLEScan->setWindow(50);
Serial.println(“BLE scanner initialized.”);
}
Can you PLEASE use the code tags above " </> " just paste it in there. You will make it easier to read and test , so you’ll get better quality answers and help.
HTH
GL PJ
Can you describe it more too, So perhaps I can test it as well.
After a set number of scans (e.g., every 200 scans), re-init the BLE stack:
if (scanCount % 200 == 0) {
Serial.println("Reinitializing BLE stack...");
BLEDevice::deinit(true); // Force full deinit
delay(100);
setupBLEScanner(); // Reinitialize
}
You may need to add BLEDevice::deinit(true) in your build if the wrapper doesn’t expose it. If not exposed, a NVIC_SystemReset() might be your only workaround.
You do realize the code pasted here has errors and doesn’t compile?
The paste included some incorrect Quotes marks…from the C/P.
Reposting the garbage in Block quotes doesn’t make it run either.
also what is posted appears to be a bandaid, Post the actual code you are using with the Code tags above " </> " paste it in there.
Obviously it shouldn’t hang after any number of adverts , so. Let’s get serious if you want to fix it.