This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

ble_nus stops working after central connection

Hi,

I just found what looks like a bug in ble_nus.c in SDK12 - not sure how to report it so I'm just doing it as a question/answer combo.

Basically:

  • Connect to nRF5x normally (treating it as a peripheral)
  • ble_nus_string_send works great
  • Use nRF5x to connect in central mode to something else
  • disconnect from central mode
  • ble_nus_string_send responds with NRF_ERROR_INVALID_STATE
  • The problem is that ble_nus.c responds to BLE_GAP_EVT_CONNECTED and BLE_GAP_EVT_DISCONNECTED but doesn't check them to see if

    • the event is for a peripheral role or not
    • the disconnect event even matches the current connection handle

    You could modify ble_nus.c but then it makes changing SDK difficult, so in your main.c you can work around it by only calling the event handler at the right times. I actually just don't call it at all for disconnect events now:

     if (!((p_ble_evt->header.evt_id==BLE_GAP_EVT_CONNECTED) &&
              (p_ble_evt->evt.gap_evt.params.connected.role != BLE_GAP_ROLE_PERIPH)) &&
            !(p_ble_evt->header.evt_id==BLE_GAP_EVT_DISCONNECTED))
          ble_nus_on_ble_evt(&m_nus, p_ble_evt);
    

    Hope that helps someone! It'd be great if this could be fixed properly in the next SDK revision? The same class of bug may well appear for many other libraries too.

Related