Reputation: 7193
I am using the BluetoothLeGatt example from here: https://github.com/android/connectivity-samples/tree/master/BluetoothLeGatt
Assume BLE connection, service and characteristic detection have all happened properly. The following data being sent is value of a characteristic.
From a custom BLE device, I am sending an array of bytes to the smartphone, for example, something line {0x00, 0x01, 0x02, 0x03, 0x04}. In the android program this is received inside the onReceive() function inside BroadcastReceiver mGattUpdateReceiver
in DeviceControlActivity.java
The line
displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
actually displays it on a text view. If I analyze the String obtained through BLE, and view the individual bytes, I see something like this (values in hex):
0 1 2 3 4 a 33 33 20 33 31 20 33 32 20 33 33 20 33 34 20
Each number is the hex value of a byte. So when sending 5 bytes from the custom BLE device, I get five bytes 0 to 4, then a byte 'a' (value 10), then a sequence of characters representing the string version of each byte transferred. For example, the 33 33 20 33 31 20 33 32 20 33 33 20 33 34 20
translates to 00 01 02 03 04
in a text view (20 represents space). Values in this sequence change when the number of bytes sent by the custom device is different.
Is there a way to know how many bytes of data were actually sent by the custom device, based on the information obtained above? Is there a way to get the raw bytes received in BLE, as a byte[] array or something, instead of a String? In case this is not possible, what is the proper way to decode each byte received from BLE, from the string above?
Upvotes: 0
Views: 1096
Reputation: 2663
The example you are using receives the data as a byte array already, but it appends hex representation to the data as string. This is why you get your data in both representations.
You will need to change the example in the file BluetoothLeService.java
on line 149. It is currently reading
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
and you would need to change it to
intent.putExtra(EXTRA_DATA, new String(data) + "\n");
if you want to receive only the string representation.
Upvotes: 1