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

nRF52840 SDK16 s140 - Disable notifications when peer is disconnected

Hi everyone,

How could I disable notifications from a characteristic when the peer is get disconnected from the the peripheral? My problem is that when notification are enabled and a disconnection occurs then notifications remains enabled. This leads to the problem that when peer connect again with the peripheral the notifications are enabled (because they were enabled before disconnection)

This is the function for handiling write events

static void on_write(ble_cus_t *p_cus, ble_evt_t const *p_ble_evt) {

  ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 
  static ble_cus_evt_t evt;
  enum gatt_handles gatt_handle;


  // Here we check if the handle that has been written matches the handle of the CCCD and that the value has the correct length (2bytes). For indications and notifications len must be 2
  if ((p_evt_write->handle == p_cus->custom_value_handles.cccd_handle) && (p_evt_write->len == 2)) {

    // CCCD written, call application event handler
    if (p_cus->evt_handler != NULL) { // na - check that the event handler function is pointing somewhere (in our case on on_cus_evt function, see main.c)

      gatt_handle = CCCD_HANDLE; // na - assign the CCCD_HANDLE value

      // Here we need to check if the notification enabled or disabled
      if (p_evt_write->data[0] == 0x01) {
        evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED;
      } else if (p_evt_write->data[0] == 0x00) {
        evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED;
      } else {
        evt.evt_type =  BLE_CUS_EVT_INVALID_INPUT;
        NRF_LOG_INFO("Invalid State");
      }
      p_cus->evt_handler(p_cus, &evt, gatt_handle); // na - Call the application event handler.
    }
  }
}

This is the state when the peer is connected back with the peripheral. As you can see the CCCD value is 1. I want to be 0 (notifications disabled) when there is a reconnection.

How could I handle this?

Thanks in advance

Nick

Parents Reply Children
  • It is the central that controls the CCCD, always. You can't control this from the peripheral's application. The only thing the peripheral can do is to store the state of the CCCD between the connections if they are bonded. 

    You can of course disable bonding, but I assume there is a reason why you are using it in the first place. 

    Why do you need to unset the CCCD state?

    BR,
    Edvin

Related