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

BLE UART connection status

Hello Nordic Pro's,

We are developing a product based on the BLE UART peripheral.  We communicate heavily via NUS.  There are extended processes that continually send status messages over BLE via a central based device.  These processes need to continue if the system disconnects, but the messages need to resume once reconnected.  So, we need to be able to detect the connection status and not send any messages when not connected, then resume the message once connected.  I have read through most if not all of the related cases, but cannot decipher a straightforward answer.  How can I determine from within a peripheral app whether it is connected to a central device? Has anyone successfully done this? 

Thanks again,

Robin@TL

  • Hi Robin

    I'm having some trouble understanding what exactly you want.  Do you mean like a return value when the peripheral connects/disconnects to a central? Because this can be seen if connecting it to a terminal or something similar. You should also be able to implement an LED to turn on when connected and off when disconnected if that is what you are after.

    If I have misunderstood what you want to achieve, please try to explain it again.

    NOTE: Creating a public case would let the community come with suggestions as well, which increases the chance of someone providing I.E. a working example of what you want. We can also set this case to public mode if you'd like. Just give me the word.

    Best regards,

    Simon

  • Hello Simon,

    The LED is not an option.  This need to happen without operator intervention,  so yes, a return variable would work, but the process needs to be "synchronized" with the nus send string function.  And thank you for noting this case is not public.  I thought I had checked that option.

    Robin

  • You can look at the Connection Handle:

    • if its value is BLE_CONN_HANDLE_INVALID, then you are not connected;
    • otherwise, you are.

    It changes state on the BLE_GAP_EVT_DISCONNECTED and BLE_GAP_EVT_CONNECTED events.

  • Hello all,

    I have this finally figured out.  Thanks to everyone for their help ans support.

    Robin@TL 

  • Here are snippets of what I did, in case anyone is interested. 

    // Inside Main
    //...
    
    static bool connected = false;
    
    //...
    
    // Inside send_string()...
    
    static void send_str(const char * str)
    {
      if (connected)
      {
        uint8_t i = 0;
        while ((str[i] != 0x0A) && (str[i] != 0x00) && (i < BLE_NUS_MAX_DATA_LEN)) 
        {
          i+=1;
        }
        if (str[i] == 0x00)
          i-=1;
        ble_nus_string_send(&m_nus, (uint8_t*)str, i+1);
      }
    }
    
    // Inside on_ble_evt...
    
        case BLE_GAP_EVT_CONNECTED:
          err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
          APP_ERROR_CHECK(err_code);
          m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
          connected = true;
        break;
        case BLE_GAP_EVT_DISCONNECTED:
          err_code = bsp_indication_set(BSP_INDICATE_IDLE);
          APP_ERROR_CHECK(err_code);
          m_conn_handle = BLE_CONN_HANDLE_INVALID;
          connected = false;
    //...

Related