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

Parents
  • 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;
    //...

Reply
  • 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;
    //...

Children
No Data
Related