I'm currently a bit lost in trying to figuring out how to transfer data from my ESP32 microcontroller to android phone. I have managed to send and read characteristics value, but don't know how to parse it.. For now, I'm sending simple integer value = 15
.
I found out that the data is sent using a byte array, therefore I converted it to hex-string and the result doesn't make sense 31-35-2E-30-30
. Checked the nRF connect app it also displays the same hex-string result but in addition it has parsed the value to 15.00
.
Here is the Arduino code:
...
char txString[8];
int someNumber = 15;
dtostrf(someNumber, 1, 2, txString);
_pCharacteristicNotify -> setValue(txString);
_pCharacteristicNotify -> notify();
...
Android Studio
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
_handler.handleMessage(Message.obtain(null, ..., characteristic));
}
private Handler _handler = new Handler() {
public void handleMessage(Message msg) {
// ------- The problem is here ----------------
BluetoothGattCharacteristic characteristic;
characteristic = (BluetoothGattCharacteristic) msg.obj;
String value = Utils.parseBLECharacteristicValue(characteristic);
// the value is "31-35-2E-30-30"
// HOW TO GET THE NUMBER 15 ??
}
};
The parse method that I'm using is from here Example 1
Could someone please explain me how to parse the given value?
And how the logic would change if the provided value is string "abcd123"
not an integer ?