2 Replies Latest reply on Apr 4, 2016 9:54 AM by userc_40298

    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[0] = (Measurement & 0xFF);
          MeasureArray[1] = ( (Measurement >> 8) & 0xFF);
          MeasureArray[2] = ( (Measurement >> 16) & 0xFF);
          MeasureArray[3] = ( (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[0];  


      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.

        • 1. Re: PSoC uses unsigned integers, Android uses signed integers

          Assuming 'data' is the array where you receive your bytes on the Android side, what you can do is:


          long result=0;


          long result=(int)data[0]&0xff;


          long result+=256*((int)data[1]&0xff);


          long result+=256*256*((int)data[2]&0xff);


          long result+=256*256*256*((int)data[3]&0xff);


          This re-assembled the 32bit-value from the array. Each byte is converted into an int first, and then the upper part is truncated, which makes it unsigned again.

          • 2. Re: PSoC uses unsigned integers, Android uses signed integers

            Sorry I didn't respond earlier! This is very similar to what I attempted on the Android side, and thanks for the reassurance that I was on the right track. I eventually decided that using the getIntValue function would probably be the most reliable, and that meant there was an error in my PSoC code somewhere (which there was). Thanks for your response.