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

Notifications and read function to be used in the same application

Hi Devs,

based on this example

https://github.com/NordicPlayground/nRF52-Bluetooth-Course

i was able to run it, and the value field appear below the Unknown Characteristics properties incremented every second as expected.

MY question however is whether it is possible at all to have both the enable notification and "read" button to run at the same time and update that value field. The purpose is that if i don't enable notifications, and just push the read button, the value field will be read on demand and not pushed to the client. Of course a better functionality would be if "notifications enabled" and read button pushed, both will not crash each other and just update the value field at a faster pace. Hope i have explained the situation clearly, and any help would be appreciated. thx.

  • Hi Hung,

    regarding configuring the characteristics, here is what i have already done previously to the ble_cus.c sample provided.

    First,

    static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init){

    ....

    attr_md.rd_auth    = 1;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 0;

    .....

    }   

    Then for ,

    void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context)

    {..........

    ///switch statement....:

    case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
           
                ble_gatts_evt_rw_authorize_request_t  req;
                ble_gatts_rw_authorize_reply_params_t auth_reply;
                req = p_ble_evt->evt.gatts_evt.params.authorize_request;
                            
                if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID)
                {
                    
                        if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
                        {      
                            auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
                        }
                        else
                        {
                                                NRF_LOG_INFO("BLE_GATTS_AUTHORIZE_Read.\r\n");
                            auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
                        }

                        auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
                        err_code  = sd_ble_gatts_rw_authorize_reply(
                                                                  p_ble_evt->evt.gatts_evt.conn_handle, &auth_reply);
                        APP_ERROR_CHECK(err_code);
                    
                        }
                            ble_cus_evt_t evt;
                            evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED;
                            p_cus->evt_handler(p_cus,&evt);
                            break;

                 }

    ...........}

    I added the case for authorize read in the switch statement, and the code following should be for sd_ble_gatts_rw_authorize_reply. Finally i tried to direct the evt type towarrds BLE_CUS_EVT_NOTIFICATION_ENABLED to trigger a value update, hoping that would show the updated value to the characteristic field, which crashed the application and caused a reboot. I will definitely try to follow the debug guide and see what went wrong, but meanwhile i am thinking that this code " p_cus->evt_handler(p_cus,&evt);" is what might be wrong. Which brings us back to the main point of why I asked for examples that show how to update the characteristic field on authorize read event.

    Regarding step2 , the connection will be disconnected for power saving considerations. The time is not fixed for now, and will likely change . Probably a variable will be defined to hold how long that sleep duration will last, but lets say that is 20 seconds. BTW , what API do i call for triggering a disconnection ?

    Lots of questions, my apologies not being familiar with the nRF device, and thx again for your help.

  • Please walk through the message sequence chart, there you will find the use for many API, including the connection termination which is here.

    Please try to test step by step. The first step is to make sure authorization read is working. From what you have in the code, it seems that it would work fine. 

    Except for this part:  evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED;
                            p_cus->evt_handler(p_cus,&evt);

    It should be removed. 

    I'm not sure why you want to trigger a notification but if you want, you would need to first enable it from the client side. And then inside BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST you call ble_cus_custom_value_update() in particular sd_ble_gatts_hvx() should be called. It's described in this chart. 

Related