I am taking over a firmware Bluetooth project and am reading the code that is having some issues. Below is a snippet from a BLE characteristic read callback. Here a double is being converted to a 16 bit int. I am not sure why it is converted to a long first, but anyway the problem is that there seems to be a random higher byte when it should be 0
For example, when force is equal to 250, I would expect the value seen in nrfConnect to be 0x00-FA, but the value received has the first byte with a value that makes no sense and it changes.
For example I receive 0x34-FA, 0x68-FA, etc. FA is 250 so that is correct, but the higher byte is seemingly random.
The same goes for when force is 0, nrfConnect shows
0x22-00, 0x10-00, and so on with a random higher byte when I would expect 0x00-00
//read force levels
double force = read_force();
//convert to 16 bit int
long val_long = (long) force;
int16_t val16 = (int16_t) val_long;
int16_t *val16_ptr = &val16;
//dispatch value
return bt_gatt_attr_read(conn, attr, buf, len, offset, val16_ptr,
sizeof(*val16_ptr));