I am making an app that communicates with a specific Bluetooth Low Energy device. It requires a specific handshake and this is all working perfectly in Objective-C
for iOS
, however, I am having trouble recreating this functionality in Java
Any thoughts greatly appreciated!
WORKING Objective-C
code:
uint8_t bytes[] = {0x04,0x08,0x0F,0x66,0x99,0x41,0x52,0x43,0x55,0xAA};
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
[_btDevice writeValue:data forCharacteristic:_dataCommsCharacteristic type:CBCharacteristicWriteWithResponse];
So far for android I have the following as an equivalent:
byte[] handshake = {0x04,0x08,0x0F,0x66,(byte)0x99,0x41,0x52,0x43,0x55,(byte)0xAA};
characteristic.setValue(handshake);
boolean writeStatus = gatt.writeCharacteristic(characteristic);
Log.d(TAG,"Handshake sent: " + writeStatus);
As mentioned, iOS works great, but the equivalent in Java
is getting no response from the device, leading me to think that the data being sent is wrong/not recognised
UPDATE
So, after plenty of wrestling with this I have a little more insight into what is going on 'I think!'
As Scary Wombat mentioned below the maximum value of an int is 127 so the 2 values in the array of 0x99 and 0xAA are of course out of this range
The below is where I am at with the values:
byte bytes[] = {0x04,0x08,0x0F,0x66,(byte)0x99,0x41,0x52,0x43,0x55,(byte)0xAA};
Log.d(TAG, Arrays.toString(bytes));
Produces
[4, 8, 15, 102, -103, 65, 82, 67, 85, -86]
However the expected values need to be
[4, 8, 15, 102, 153, 65, 82, 67, 85, 170]
I have tried casting these troublesome bytes implicitly and also tried the below below:
byte bytes[] = {0x04,0x08,0x0F,0x66,(byte)(0x99 & 0xFF),0x41,0x52,0x43,0x55,(byte)(0xAA & 0xFF)};
However the resulting values in the array are always the same.
Please help!!! :)
UPDATE 2
After a day of digging it appears that although the values are logging incorrectly the values perceived by the Bluetooth device SHOULD still be correct, so I have modified this question and continuing over here
See Question&Answers more detail:
os