Bluetooth error -170

Hey,

I have 2 tracker edge devices (with deviceOS 4.0.0) that run the following code.

#include "BLE.h"
#include "thread_info.h"
#include "yoda/time.h"
#include "yoda/status.h"
#include "yoda/BLE_logging.h"

BleLogging<BLE_LOGGING_STACK_SIZE> bleLogHandler(LOG_LEVEL_INFO);

static Logger Loglib("yoda.ble");

os_queue_t BLE_action_q;

void cb_BLE_connected (const BlePeerDevice& peer, void* context) 
{
    Loglib.info("BLE device connected");
    return;
}


void onPairingEvent(const BlePairingEvent& event, void* context) {

    Log.info("OnPairing event type %d received", (int) event.type);
    if (event.type == BlePairingEventType::REQUEST_RECEIVED) {
        Log.info("onPairingEvent REQUEST_RECEIVED");
    }
    else
    if (event.type == BlePairingEventType::PASSKEY_DISPLAY) {
        char passKeyStr[BLE_PAIRING_PASSKEY_LEN + 1];
        memcpy(passKeyStr, event.payload.passkey, BLE_PAIRING_PASSKEY_LEN);
        passKeyStr[BLE_PAIRING_PASSKEY_LEN] = 0;

        Log.info("onPairingEvent PASSKEY_DISPLAY %s", passKeyStr);
    }
    else
    if (event.type == BlePairingEventType::STATUS_UPDATED) {
        Log.info("onPairingEvent STATUS_UPDATED status=%d lesc=%d bonded=%d", 
            event.payload.status.status,
            (int)event.payload.status.lesc,
            (int)event.payload.status.bonded);
    }
    else
    if (event.type == BlePairingEventType::NUMERIC_COMPARISON) {
        Log.info("onPairingEvent NUMERIC_COMPARISON");
    }
}

void parse_device_id(uint8_t* buf) {
    for (uint8_t byte_index = 0; byte_index < 12; byte_index++) {
        uint8_t byte = strtoul(System.deviceID().substring(byte_index * 2, byte_index * 2 + 2), NULL, 16);
        buf[byte_index] = byte;
    }
}

int yoda::ble::setup()
{
    //first 2 bytes in manufacturer data are reserved for the company identifier, 0xffff => no Company Id  
    uint8_t manufacturer_data[14]={0xff,0xff};
    parse_device_id(&manufacturer_data[2]);

    BLE.setDeviceName("Yoda BLE");
    BLE.on();

    BleAdvertisingData advertising_data;
    advertising_data.appendLocalName("Yoda");

    advertising_data.appendCustomData(manufacturer_data,sizeof(manufacturer_data));

    BLE.setPairingIoCaps(BlePairingIoCaps::NONE);
    BLE.setPairingAlgorithm(BlePairingAlgorithm::LESC_ONLY);

    BLE.onPairingEvent(onPairingEvent);

    BLE.advertise(&advertising_data);

    os_queue_create(&BLE_action_q, sizeof(user_action_t), 2, 0);

    return 0;
}

On one device the bluetooth works perfectly fine, but on the other I'm unable to connect over bluetooth and I see the following errors in the logs ( -170 = SYSTEM_ERROR_NOT_FOUND):

0000017452 [system.ctrl.ble] ERROR: Channel error: -170
0000018042 [yoda.ble] INFO: BLE device connected
0000018117 [system.ctrl.ble] ERROR: Channel error: -170
0000018769 [yoda.ble] INFO: BLE device connected
0000018847 [system.ctrl.ble] ERROR: Channel error: -170
0000019539 [yoda.ble] INFO: BLE device connected
0000019613 [system.ctrl.ble] ERROR: Channel error: -170

and in the nRF Connect app I see following logs:

What is strange is that the bluetooth used to work fine on both devices and rolling back to previous version of my code from before the occurrence of the error now also generate this error.

It appears that Channel error occurs only in one place in Device OS:

This is handling a connection attempt on the BLE setup service. It would appear that something is attempting to establish a BLE setup session. It's possible that whatever is connecting is also disconnecting, possibly before the handshake completes. That matches the screenshot of the Android log, which seems to indicate connection followed by immediate disconnection.

I'd try increasing the log level to LOG_LEVEL_TRACE as there are other debugging messages in the setup control request handler.

I enabled LOG_LEVEL_TRACE but there is no more information:

0000016916 [main] INFO: BLE device connected
0000016916 [wiring.ble] TRACE: Connected
0000016919 [system.ctrl.ble] ERROR: Channel error: -170
0000017714 [hal.ble] ERROR: sd_ble_gattc_exchange_mtu_request() failed: 8.
0000029056 [main] INFO: BLE device connected
0000029057 [wiring.ble] TRACE: Connected
0000029059 [system.ctrl.ble] ERROR: Channel error: -170
0000031910 [main] INFO: BLE device connected
0000031910 [wiring.ble] TRACE: Connected
0000031913 [system.ctrl.ble] ERROR: Channel error: -170

Blockquote
It's possible that whatever is connecting is also disconnecting, possibly before the handshake completes. hat matches the screenshot of the Android log, which seems to indicate connection followed by immediate disconnection.

Are you shure? Because the android log say the connection is terminated by the peer:

Connection terminated by peer (status code 19)

I created this minimalistic code that generates the error.

Before uploading the code I do a factory reset of the board, then I do a setup with the browser. At this moment the device broadcast BLE under the name Boron_XXXX and I'm able to connect to it. Then I flash my code and I'm unable to connect to the BLE anymore on two of our 4 board (I tested it on more boards).