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

Send a float

I want to send a float as a value of a characteristic.

As I know is this all what I have to change to send a float...

float temperature;

uint16_t len = 20;

attr_char_value.init_len  = 4;
attr_char_value.init_offs = 0;
attr_char_value.max_len   = 4;
attr_char_value.p_value   = (uint8_t*)&temperature;

hvx_params.offset = 0;
hvx_params.p_len = &len;
hvx_params.p_data = (uint8_t*)&temperature;

But it doesnt work. I wont recieve any data.

  • First of all you are adding characteristic with exact size 4. It means that you have to set len = 4 while sending data. But your original problem most likely in the fact that the host didn't enable notification for that characteristic after connection and before you send data by sd_ble_gatts_hvx()

  • Should I change the size to maximum? I dont understand what you mean with the host didn't enable notification for that characteristic after connection and before you send data by sd_ble_gatts_hvx()

  • Regarding the size, in your case "len" is the number of bytes you are sending, so set to 4. Regarding notification, there are two way of exchanging data between nRF51 and the host(for example android device). One is to change temperature inside nRF51 but the host won't know about it. In this case host has to read data periodically but you don't use sd_ble_gatts_hvx() in nRF51. Instead you update value by sd_ble_gatts_value_set(). Another way is to use sd_ble_gatts_hvx() in nRF51 and data will be sent automatically to the host so host doesn't need to read it. But host has to enable notification or indication of that characteristics before nRF51 send data.

  • Oh very interesting! I didnt know that!! So there are two ways to send information from nRF51 to host. One way is to recieve a READ from the host. In this case the host reads the value of the Characterstic. The value of the characteristic will only be changed if I set a new value with sd_ble_gatts_value_set().

    But there is another way if I want that the value of the characteristic is automatically updated. The nRF51 will send the value of the Characteristic all the time. But the host will only recieve the value of the characteristic when the nRF51 recieves a notification or indication from the host.

    Do I understand something wrong?

  • Correct. The only small differences can be depending on your code in nRF51. For example if notification is not enabled and nRF51 tries to send data you may see call of APP_ERROR_CHECK() in some examples which may reset the chip in debug mode.

Related