BLE - decoding service UUID
-
Hi,
I'm using an ESP32 to offer a service with a 128bit-UUID.
_HE_TB_UUID = UUID('04500100-39fd-49ec-b565-b5d6dc31b6ae') ... ...= self._ble.gatts_register_services(_SERVICES)On the client side is a GPy with the 1.20 firmware. Here I use 'service.uuid()' from https://docs.pycom.io/firmwareapi/pycom/network/bluetooth/gattccservice/
to read the uuid of the service. I'm getting back this bytes object:bβ\xae\xb61\xdc\xd6\xb5e\xb5\xecI\xfd9\x00\x01P\x04βHow can I decode that? The documentation only say '...but for 128-bit long UUIDs the value returned is a bytes object.' I've been through all permutations of struct.unpack but to no avail.
Thanks!
-
@poesel hat do you actually want to do? Display the UUID in UUID format? Compare it with a known UUID? Something else?
Note that the bytes object has the bytes reversed if you compare that to the original UUID string. Other than that, itβs just a matter of formatting...
-
True.
>>> UUID(bytes=b'\xae\xb61\xdc\xd6\xb5e\xb5\xecI\xfd9\x00\x01P\x04'[::-1]) UUID('04500100-39fd-49ec-b565-b5d6dc31b6ae') -
-
Hmm, sorry. I don't get it. Where do I find the UUID function?
The only one I can find is the one that reads it via bluetooth. The UUID library is not implemented in Micropython. -
@poesel said in BLE - decoding service UUID:
The UUID library is not implemented in Micropython.
That's true. I used vanilla Python to demonstrate what @jcaron tried to point out here.
-
FYI this is the function I came up to decode the UUID. Not nice but works. :)
def UUIDbytes2UUIDstring(uuid): ''' reverses the bytes of uuid and converts them to a properly formatted UUID string ''' from ubinascii import hexlify tmp = str(hexlify(bytes(reversed(uuid))))[2:34] return tmp[0:8]+'-'+tmp[8:12]+'-'+tmp[12:16]+'-'+tmp[16:20]+'-'+tmp[20:32] -
Thanks @poesel. We picked that up and wrapped it [1] into two corresponding routines for your convenience:
>>> from terkin.network.ble.util import BluetoothUuid >>> BluetoothUuid.from_bytes_le(BluetoothUuid.to_bytes_le('04500100-39fd-49ec-b565-b5d6dc31b6ae')) '04500100-39fd-49ec-b565-b5d6dc31b6ae'You will also find some background information about why that happens within the comments. Thanks also to @jmarcelino for
uuid2bytesfrom [2].[1] https://github.com/hiveeyes/hiveeyes-micropython-firmware/blob/a8249fd8e9895623de8047ca2d6478dba1351aa8/terkin/network/ble/util.py
[2] https://forum.pycom.io/topic/530/working-with-uuid/2
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register Login