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

Connect-handler of service be redirected from original connection (Peripheral) to newest connection (Central) after SD_BLE_GAP_CONNECT called.

NRF52832 / S332 / SDK 14.2

Step 1: connect to mobile phone as Peripheral Role, and the connect handler of service was #0 that equal to Peripheral-connection handler number.

            Regular sent out the notifications to mobile phone.

Step 2: connect to other BLE device as Central Role, and the connect handler of service changed to #1 that equal to Central Roles'.

            Mobile phone no more got the notifications.

But, if I changed the connection sequence as following, then anything is fine althrough the connect handler of service also swap to new one:

Step 1: connect to BLE device as Central Role.  Step 2: connect to mobile phone as Peripheral Role.

Additional information:

I added a special workaround in beginning of ble_dispatch_handler to solve the wrong role (BLE_GAP_ROLE_INVALID) for first connection:

  if ((p_ble_evt->header.evt_id == BLE_GAP_EVT_CONNECTED) && (role == BLE_GAP_ROLE_INVALID))
  {
        (void)sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        // Do not process the event further.
        return;
  }

Does it make the further problem? if so, any suggest workaround for SDK 14.2/S332?

Please help, thank you.

  • Hi,

    Do you use the logger module to print debug messages to a serial terminal? If yes, do you get any relevant information here?

    Have you tried debugging? Have you for example checked whether the code responsible for sending notifications is being called at all? Or maybe the functions sending notifications are returning error codes?

    PS: It is holiday season in Norway these days and the response time might be slower than usual. 

  • Hi,

    I use RTT viewer as debugging for customized messages.

    # SEGGER J-Link RTT Viewer V6.18c Terminal Log File
    # Compiled: 16:47:51 on Aug 21 2017
    # Logging started @ 16 Jul 2018 10:03:29
    0>
    0> Scanning
    0> Advertising
    0> ble_evt_handler 0 (handle) 0(role) 10 bas-connect FFFF  ===> try to establish connection as PERIPHERAL role
    0> ble_evt_handler 0 (handle) 1(role) 11 bas-connect 0
    0> PERIPHERAL: Disconnected, handle 0, reason 0x16.
    0> ble_evt_handler 0 (handle) 1(role) 10 bas-connect FFFF
    0> PERIPHERAL: Connected, handle 0.
    0> ble_evt_handler 0 (handle) 1(role) 52 bas-connect 0
    0> pm_evt_handler
    0> ble_evt_handler 0 (handle) 1(role) 50 bas-connect 0
    0> ble_evt_handler 0 (handle) 1(role) 50 bas-connect 0
    0> Location/Speed: Notification enabled
    0> ble_evt_handler 0 (handle) 1(role) 50 bas-connect 0
    0> Navigation: Notification enabled
    0> ble_evt_handler 0 (handle) 1(role) 57 bas-connect 0
    0> ble_evt_handler 0 (handle) 1(role) 50 bas-connect 0
    0> Control Point: Indication enabled
    0> ble_evt_handler 0 (handle) 1(role) 57 bas-connect 0
    0> ble_evt_handler 0 (handle) 1(role) 57 bas-connect 0
    0> 1 Connection Request passed
    0> ble_evt_handler 1 (handle) 0(role) 10 bas-connect 0 ===> try to establish connection as CENTRAL role
    0> ble_evt_handler 1 (handle) 2(role) 11 bas-connect 1  ===> service's handler-num changed here.
    0> CENTRAL: Disconnected, handle: 1, reason: 0x16
    0> Scanning
    0> 1 Connection Request passed
    0> ble_evt_handler 1 (handle) 2(role) 10 bas-connect FFFF
    0> CENTRAL: Connected, handle: 1.
    0> CENTRAL: Searching for HRS on conn_handle 0x1
    0> ble_evt_handler 1 (handle) 2(role) 30 bas-connect 1
    0> ble_evt_handler 1 (handle) 2(role) 32 bas-connect 1
    0> ble_evt_handler 1 (handle) 2(role) 33 bas-connect 1 ===> No more event that coming from PERIPHERAL.

    Partial code:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
    uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    uint16_t role = ble_conn_state_role(conn_handle);

    if (conn_handle != 65535)
    SEGGER_RTT_printf(0, "ble_evt_handler %d (handle) %d(role) %x bas-connect %x\r\n",conn_handle,role,p_ble_evt->header.evt_id,m_bas.conn_handle);

    if ((p_ble_evt->header.evt_id == BLE_GAP_EVT_CONNECTED) && (role == BLE_GAP_ROLE_INVALID))
    {
    (void)sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    // Do not process the event further.
    return;
    }

    .............

    }

  • Hi,

    Are you checking all return codes from the Softdevice functions? Ignoring the return codes like you do with (void)sd_ble_gap_disconnect() is bad practice. You will see in the SDK that all return codes are usually checked like e.g. this:

     

    err_code = sd_ble_gap_disconnect();
    APP_ERROR_CHECK(err_code);

  • I also have the same situation. Let me briefly explain the situation that I encountered:

    We use NRF52832 chip to setup “central and peripheral” service in BLE. In peripheral part, we enables HRM service to connect to a cellphone, and pass heart rate data to it. In central part, we enable HRM service to try to search for a HRM peripheral (such as a heart rate patch) and connect to it.

    The code is running normally, while central and peripheral role in our NRF52832 device are working properly without error.

    The issue that we encounter is based on a particular sequence. We first connect cellphone to NRF52832 device, which is normally transmitting heart rate data. At this time, under the circumstance that cellphone is connected, we try to connect our NRF52832 device to HRM peripheral (heart rate patch). When our device is successfully connected to heart rate patch, cellphone can no longer get data from our NRF52832 device, while cellphone is still connected to our NRF52832 device.

    At this time, if our NRF52832 device is reconnected to cellphone, cellphone can again get heart rate data, while heart rate patch is also successfully connected at the same time.

    Since that my code can run normally without error at startup (all error codes are checked with APP_ERROR_CHECK), it is not due to wrong configuration. Under this condition, the situation mentioned by me and Iamjolintw still happens, so wrong code writing can be ruled out. 

  • Is it possible to reproduce this with the examples in the SDKs? Alternatively, can you upload some code I can use to reproduce it? 

Related