Is there a function within the nRF51822 SDK to check if BLE is connected or not (or to check general BLE connection state)?
Thanks.
Hi there,
Right now there is no function for this in the SDK. The SDK applications and libraries use the BLE_GAP_EVT_CONNECTED and BLE_GAP_EVT_DISCONNECTED events triggered by the SoftDevice to update the link status. If you are interested, you may refer to the functions on_connect, on_disconnect and ble_hrs_sensor_contact_supported_set in ble_hrs.c to see how this is done.
Cheers, Balaji
I think you are supposed to handle the BLE events and remember the state your self. You can register to receive these events using softdevice_ble_evt_handler_set(ble_evt_dispatch) where your ble_evt_dispatch function looks like this :
static void ble_evt_dispatch(ble_evt_t * p_ble_evt) { switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: connected = true; break;
case BLE_GAP_EVT_DISCONNECTED:
connected = false;
break;
default:
break;
}
}
Hi, Nick,
Not necessary to set a flag for this purpose. You should have a connection handle for the BLE operation (e.g. m_conn_handle
), which is used in sd_ble_gap_disconnect(m_conn_handle, ...)
if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
{
/* BLE is connected. Do something. */
} else
{
/* BLE is not connected. Do something. */
}