PSoC uses unsigned integers, Android uses signed integers | Cypress Semiconductor
PSoC uses unsigned integers, Android uses signed integers
I'm developing the software for a Pioneer kit BLE device and also an Android app to take sensor data.
I am trying to transmit a 32 bit value (so 4 bytes) from the BLE device to the Android app, but I am having a problem with formatting. I am using an array of uint8, with a length of 4, to separately store each byte of the uint32. The following code stores each byte in the array:
MeasureArray = (Measurement & 0xFF);
MeasureArray = ( (Measurement >> 8) & 0xFF);
MeasureArray = ( (Measurement >> 16) & 0xFF);
MeasureArray = ( (Measurement >> 24) & 0xFF);
The next line sets the value of the characteristic handle to the first element of the array. When I broadcast a notification, the handle only needs the location of the first byte, and then it will send all four.
MeasureHandle.value.val = &MeasureArray;
The issue I am having is that the values sent over BLE are unsigned. Even if I configure the BLE characteristic to be 32 bit signed, it doesn't make any difference. The Handle.value.val statement always takes unsigned uint8. On the Android side of the app, there are two options. Use the BluetoothGattCharacteristic function getIntValue(formatType, offset) which returns an integer, or use getValue, which returns a byte array. I have tried several different formats with the getIntValue function, but I get large discontinuities in my data. I am also getting discontinuities with the getValue function, after converting the bytes to integers.
The problem is that the byte value returned by getValue is basically a signed int. When I try to take the 4 bytes, do some bit shifts, then cast to int, there are errors because the signed ints are overflowing.
I am at a loss right now, so any tips are appreciated. If it helps at all, the data I'm transmitting is 24 bit two's complement, which I initially store in a 32 bit unsigned int. I can't upload my project as it's proprietary.