Read gatt service value

Hi Team,

Someone could guide me on how to read the value from the peripheral (ex: ADC or battery value) and show it in the mobile GATT services. And how the value is going to parse while reading it.

I have gone to examples, but still some confusion.


  • Hi Logan.k,

    Taking the ble_app_peripheral example you can follow the steps below to read the value of the adc characteristic:

    1. In file user_custs1_def.h change the database initialization of the SVC1_IDX_ADC_VAL_1_VAL field to:

        // ADC Value 1 Characteristic Value
        [SVC1_IDX_ADC_VAL_1_VAL]           = {SVC1_ADC_VAL_1_UUID_128, ATT_UUID_128_LEN, PERM(RD, ENABLE) | PERM(NTF, ENABLE),
                                                 PERM(RI, ENABLE)|DEF_SVC1_ADC_VAL_1_CHAR_LEN, 0, NULL},

    this will enable the read indication feature for the characteristic.

    2. In file user_peripheral.c add the following to the case CUSTS1_VALUE_REQ_IND

        struct custs1_value_req_ind const *msg_param = (struct custs1_value_req_ind const *) param;
        switch (msg_param->att_idx)
            case SVC1_IDX_ADC_VAL_1_VAL:
    	        user_svc1_read_adc_value_handler(msgid, msg_param, dest_id, src_id);
    	    } break;

    this will invoke a user created handler for the read request of the adc value.

    3. In file user_custs1_impl.c add the implementation of the handler function 

    void user_svc1_read_adc_value_handler(     ke_msg_id_t const msgid,
                                               struct custs1_value_req_ind const *param,
                                               ke_task_id_t const dest_id,
                                               ke_task_id_t const src_id)
        // Increase value by five
        adc_value += 5;
        struct custs1_value_req_rsp *rsp = KE_MSG_ALLOC_DYN(CUSTS1_VALUE_REQ_RSP,
        // Provide the connection index.
        rsp->conidx  = app_env[param->conidx].conidx;
        // Provide the attribute index.
        rsp->att_idx = param->att_idx;
        // Force current length to zero.
        rsp->length  = sizeof(adc_value);
        // Provide the ATT error code.
        rsp->status  = ATT_ERR_NO_ERROR;
        // Copy value
        memcpy(&rsp->value, &adc_value, rsp->length);
        // Send message

    This will increase the adc value by 5 every time that the ADC 1 value characteristic is read. The gatt value is updated by the sending of the custs1_value_req_rsp message to the kernel.

    Best regards,

  • Hi Dialog,

    Thanks for your response. Now I understand the logic. Yes it is working.

    While changing the device name (Generic access - USER_DEVICE_NAME) write with string and read the same string after writing the bytes.

    could give an example for this write logic also under custom char.



  • Hi Lofna.k,

    I am assuming that your requirement is to add a service with a characteristic field called device name which you will use to write a new device name. There is no implemented example for this but you can do the following:

    1. Set a custom service with a custom characteristic called device name. You can follow our tutorial on creating a custom profile and services

    to get you started on that. The ble_app_peripheral example would be a good starting point as it contains custom services with custom characteristics that can be written or read.

    1. Update the device name in the advertising data using the characteristic string. You can use the app_easy_gap_update_adv_data api to update the advertising data with the new device name that you have set using the BLE service.

    Best regards,

  • Hi team,

    Solved my issues. Please close my thread.