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!
-
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
uuid2bytes
from [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
-
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]
-
@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.
-
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.
-
@andreas - Hallo Andreas! :)
Cool thanks, thats it.@jcaron - compare it with a known UUID. This is for a non-standard service.
-
True.
>>> UUID(bytes=b'\xae\xb61\xdc\xd6\xb5e\xb5\xecI\xfd9\x00\x01P\x04'[::-1]) UUID('04500100-39fd-49ec-b565-b5d6dc31b6ae')
-
@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...