1

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 ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mr. Blond
  • 1,113
  • 2
  • 18
  • 41

1 Answers1

1

31-35-2E-30-30 is the ASCII representation of the string 15.00.

According to the dtostrf() documentation:

Conversion is done in the format "[-]d.ddd".

which explains the string 15.00 with two decimal values due to your third argument to the call being 2.

Try to use the String constructor of Arduino for your integer-to-string conversion. Afterwards, you can use the Integer class in Android to convert the string back to an integer as stated in this answer.

Bora
  • 451
  • 2
  • 5