BLE.Connect stalls

I’m having a problem with BLE.Connect from a Boron to a separate BLE device. Specifically, after calling BLE.Connect the Boron freezes, then plays an SOS on the LED, followed by 10 red flashes. Here’s our sample code:

#include "Particle.h"
SYSTEM_MODE(MANUAL);
SerialLogHandler logHandler(LOG_LEVEL_TRACE);
void scanResultCallback(const BleScanResult *scanResult, void *context);
Vector<BleCharacteristic>   ble_peer_all_chars;
bool connection_in_progress = false;

void setup() {
    (void)logHandler; // Does nothing, just to eliminate warning for unused variable
    BLE.on(); //Turn on BLE
}

void loop() {
    if (BLE.connected() || connection_in_progress){}
    else {
        Log.info("Starting Scan");
        int count = BLE.scan(scanResultCallback, NULL);
        if (count > 0) {
            Log.info("%d devices found", count);
        }
    }
    //wait a reasonable amount of time before trying again.
    delay(3000);
}

void scanResultCallback(const BleScanResult *scanResult, void *context) {
    //for test purposes, we only want to connect to a single device on my bench.
	if (scanResult->address[5]==0xF8 &&
        scanResult->address[4]==0xFE &&
        scanResult->address[3]==0x5C &&
        scanResult->address[2]==0xB4 &&
        scanResult->address[1]==0x5A &&
        scanResult->address[0]==0xC4){
        
        Log.info("Found target.");
        BLE.stopScanning();//we found one. Let's stop scanning for now and connect to it.
        delay(2000);//let's wait a smidge before we connect. If we connect immediately then it'll timeout before the next connection window on slow advertising
        Log.info("Starting connection");
        connection_in_progress = true;
        BlePeerDevice peer = BLE.connect(scanResult->address,false);//make the connection <---Nothing executes past this point.
        Log.info("Connection complete");
        if (peer.connected()) {//if we successfully connected, then HERE WE GO!!!
            Log.info("Successfully Connected");
            ble_peer_all_chars = peer.discoverAllCharacteristics();
            Log.info("Discovery Complete");
            Log.info("Disconnecting.");
            BLE.disconnect(peer);
        }
        else{
            connection_in_progress = false;
            Log.info("Connection Failed");
        }
    }
}

Note that nothing executes past the BLE.Connect line. It freezes, then displays an SOS and reboots. From the perspective of the device to which it is connecting, it sees a connection made, then connection parameters are negotiated, then nothing. After 5 seconds of inactivity our BLE device disconnects and resumes working. We do not have similar issues with our Android or iOS apps, so we’re pretty sure our device is functioning properly.

I have noticed that too and the only way around that for me was to not connect from inside the scan callback.
Save the peer address and connect from loop().

The best (indirect) warning in that direction can be found here
https://docs.particle.io/reference/device-os/firmware/boron/#ble-scan-callback-

1 Like

Yep. That was it. Thank you so much!