How to indicate of new characteristics to client without refreshing the nrf connect app in nrf ble?

Hi,

I am able to add custom services and characteristics dynamically. But still every time I need to refresh the nrfconnect application to see the newly added characteristics. Can I automate it, can I send any notification that new characteristics has been added from peripheral side, so that app can refresh it automatically.  I want to know about peripheral side rather than client(mobile) side. Can anyone help me in this?

Parents
  • Hello,

    To do this you will need to use the Services Changed notification - this lets your peripheral tell the central that there has been a change to the services / characteristics, and thus prompts the central to do a new discovery.

    Please give this a try, and dont hesitate to let me know if you should encounter any issues or questions! :) 

    Best regards,
    Karl

  • Hi, I called this function :  

    void our_service_change(ble_os_t * p_our_service)
    {
    uint32_t err_code_2;

    static uint16_t start_handle= 0x000B;
    const uint16_t end_handle = 0xFFFF;

    err_code_2 = sd_ble_gatts_service_changed(p_our_service->conn_handle,
    start_handle,
    end_handle);

    APP_ERROR_CHECK(err_code_2);
    }

    but its showing 12291 error, 12291 = 0x3003 and it's equal to BLE_ERROR_INVALID_ATTR_HANDLE.

    I am using following code to add characteristics:

    static uint32_t our_char_add(ble_os_t * p_our_service)
    {

    uint32_t err_code;
    ble_uuid_t char_uuid;
    ble_uuid128_t base_uuid = BLE_UUID_OUR_BASE_UUID;
    char_uuid.uuid = BLE_UUID_OUR_CHARACTERISTC_UUID;
    err_code = sd_ble_uuid_vs_add(&base_uuid, &char_uuid.type);
    APP_ERROR_CHECK(err_code);


    ble_gatts_char_md_t char_md;
    memset(&char_md, 0, sizeof(char_md));
    char_md.char_props.read = 1;
    char_md.char_props.write = 1;


    ble_gatts_attr_md_t cccd_md;
    memset(&cccd_md, 0, sizeof(cccd_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
    cccd_md.vloc = BLE_GATTS_VLOC_STACK;
    char_md.p_cccd_md = &cccd_md;
    char_md.char_props.notify = 1;




    ble_gatts_attr_md_t attr_md;
    memset(&attr_md, 0, sizeof(attr_md));
    attr_md.vloc = BLE_GATTS_VLOC_STACK;



    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);


    ble_gatts_attr_t attr_char_value;
    memset(&attr_char_value, 0, sizeof(attr_char_value));
    attr_char_value.p_uuid = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;


    attr_char_value.max_len = 4;
    attr_char_value.init_len = 4;
    uint8_t value[4] = {0x12,0x34,0x56,0x78};
    attr_char_value.p_value = value;


    err_code = sd_ble_gatts_characteristic_add(p_our_service->service_handle,
    &char_md,
    &attr_char_value,
    &p_our_service->char_handles);
    APP_ERROR_CHECK(err_code);
    return NRF_SUCCESS;
    }

  • Hello,

    Please elaborate - were you successful in prompting a rediscovery, or did you encounter any issues or questions?
    Please also use the Insert -> Code option when sharing code here on DevZone.

    Best regards,
    Karl

  • Hi, I am putting question properly with code again. No I was not able to

    I called this function :  

    void our_service_change(ble_os_t * p_our_service)
    {
    uint32_t err_code_2;
    
    static uint16_t start_handle= 0x000B;
    const uint16_t end_handle = 0xFFFF;
    
    err_code_2 = sd_ble_gatts_service_changed(p_our_service->conn_handle,
    start_handle,
    end_handle);
    
    APP_ERROR_CHECK(err_code_2);
    }

    but its showing 12291 error, 12291 = 0x3003 and it's equal to BLE_ERROR_INVALID_ATTR_HANDLE.

    I am using following code to add characteristics: 

    static uint32_t our_char_add(ble_os_t * p_our_service)
    {
    
    uint32_t err_code;
    ble_uuid_t char_uuid;
    ble_uuid128_t base_uuid = BLE_UUID_OUR_BASE_UUID;
    char_uuid.uuid = BLE_UUID_OUR_CHARACTERISTC_UUID;
    err_code = sd_ble_uuid_vs_add(&base_uuid, &char_uuid.type);
    APP_ERROR_CHECK(err_code);
    
    
    ble_gatts_char_md_t char_md;
    memset(&char_md, 0, sizeof(char_md));
    char_md.char_props.read = 1;
    char_md.char_props.write = 1;
    
    
    ble_gatts_attr_md_t cccd_md;
    memset(&cccd_md, 0, sizeof(cccd_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
    cccd_md.vloc = BLE_GATTS_VLOC_STACK;
    char_md.p_cccd_md = &cccd_md;
    char_md.char_props.notify = 1;
    
    
    
    
    ble_gatts_attr_md_t attr_md;
    memset(&attr_md, 0, sizeof(attr_md));
    attr_md.vloc = BLE_GATTS_VLOC_STACK;
    
    
    
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
    
    
    ble_gatts_attr_t attr_char_value;
    memset(&attr_char_value, 0, sizeof(attr_char_value));
    attr_char_value.p_uuid = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;
    
    
    attr_char_value.max_len = 4;
    attr_char_value.init_len = 4;
    uint8_t value[4] = {0x12,0x34,0x56,0x78};
    attr_char_value.p_value = value;
    
    
    err_code = sd_ble_gatts_characteristic_add(p_our_service->service_handle,
    &char_md,
    &attr_char_value,
    &p_our_service->char_handles);
    APP_ERROR_CHECK(err_code);
    return NRF_SUCCESS;
    }

  • I think setting end_handle to 0xFFFF is the issue. I doubt your attribute table has 65k entries. The API docs for sd_ble_gatts_service_changed states the context for BLE_ERROR_INVALID_ATTR_HANDLE: 

    "Invalid attribute handle(s) supplied, handles must be in the range populated by the application".

    -Edit: Also, I think start_handle might need to be larger than zero, because that handle index is reserved. 

Reply Children
No Data
Related