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

How to use sd_ble_gatts_value_get() for obtaining data of length more than 1 byte

Basically I used below function for sending 1 byte of data with bluetooth and it is working completely fine.

static void test_service_on_ble_write(ble_os_t *p_service, ble_evt_t *p_ble_evt)
{
    uint8_t data_buffer;
    ble_gatts_value_t rx_data;
    rx_data.len = 1;
    rx_data.p_value = &data_buffer;

    if(p_ble_evt->evt.gatts_evt.params.write.handle == p_service->char_handles.value_handle)
    {
        sd_ble_gatts_value_get(p_service->conn_handle, p_service->char_handles.value_handle, &rx_data);
        printf("%s: Value received on handle %#06x: %02x\r\n", print_timestamp(), p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer);
    }
    else if(p_ble_evt->evt.gatts_evt.params.write.handle == p_service->char_handles.cccd_handle)
    {
        sd_ble_gatts_value_get(p_service->conn_handle, p_service->char_handles.cccd_handle, &rx_data);
        printf("%s: Value received on handle %#06x: %02x\r\n", print_timestamp(),p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer);
        if(data_buffer == 0x0001)
        {
            printf("Notification enabled\r\n");
        }
        else if(data_buffer == 0x0000)
        {
            printf("Notification disabled\r\n");
        }// if(data_buffer == 0x0001)
    }// if(p_ble_evt->evt.gatts_evt.params.write.handle == p_service->char_handles.value_handle)

    if(p_ble_evt->evt.gatts_evt.params.write.handle == 0x0027)
    {
        printf("%s: Configuration Access\r\n", print_timestamp());
        // t.b.d.
    }
    else if(p_ble_evt->evt.gatts_evt.params.write.handle == 0x0029)
    {
        printf("%s: Status Access\r\n", print_timestamp());
        // t.b.d.
    }// if(p_ble_evt->evt.gatts_evt.params.write.handle == 0x0027)
}

// Example log-output: [in] 0x12 (from smartphone) - value = 0x12

But when I tried to write it for more than 1 byte of data it is not working. I am not getting what might be the problem. Function for obtaining more than 1 byte of data:

static void test_service_on_ble_write(ble_os_t *p_service, ble_evt_t *p_ble_evt)
{
    uint8_t data_buffer[20];
    ble_gatts_value_t rx_data;
	uint8_t i;
	
    rx_data.len = 20;
    rx_data.p_value = &data_buffer[0];

    if(p_ble_evt->evt.gatts_evt.params.write.handle == p_service->char_handles.value_handle)
    {
        sd_ble_gatts_value_get(p_service->conn_handle, p_service->char_handles.value_handle, &rx_data);
		
		printf("%s: Value received on handle %#06x: ", print_timestamp(), p_ble_evt->evt.gatts_evt.params.write.handle);
        for(i = 0; i < 20; i++) {
            printf("%02x", value[i]);
        }// for(i = 0; i < 20; i++)
        printf("\r\n");
        printf("%s: Value received on handle %#06x: %02x\r\n", print_timestamp(), p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer);
    }
    else if(p_ble_evt->evt.gatts_evt.params.write.handle == p_service->char_handles.cccd_handle)
    {
        sd_ble_gatts_value_get(p_service->conn_handle, p_service->char_handles.cccd_handle, &rx_data);
        printf("%s: Value received on handle %#06x: %02x\r\n", print_timestamp(),p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer);
        if(data_buffer == 0x0001)
        {
            printf("Notification enabled\r\n");
        }
        else if(data_buffer == 0x0000)
        {
            printf("Notification disabled\r\n");
        }// if(data_buffer == 0x0001)
    }// if(p_ble_evt->evt.gatts_evt.params.write.handle == p_service->char_handles.value_handle)

    if(p_ble_evt->evt.gatts_evt.params.write.handle == 0x0027)
    {
        printf("%s: Configuration Access\r\n", print_timestamp());
        // t.b.d.
    }
    else if(p_ble_evt->evt.gatts_evt.params.write.handle == 0x0029)
    {
        printf("%s: Status Access\r\n", print_timestamp());
        // t.b.d.
    }// if(p_ble_evt->evt.gatts_evt.params.write.handle == 0x0027)
}

// Example log-output: [in] 0x125689 (from smartphone) - value = 0x787878 (mostly random values)
  • Hi,

    What was the configuration you have for your characteristic ? If you set the maximum size of the characteristic (max_len) to 1, it's not possible to write more than 1 byte.

    Note,an alternative to calling sd_ble_gatts_value_get() to get the written value, you can also get the value in p_ble_evt->evt.gatts_evt.params.write.data[] directly. If there are several writes to one handle, calling sd_ble_gatts_value_get() may only return the value of the last write.

Related