Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    BLE - decoding service UUID

    Scheduled Pinned Locked Moved MicroPython
    8 Posts 3 Posters 558 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      poesel
      last edited by

      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!

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @poesel
        last edited by

        @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...

        1 Reply Last reply Reply Quote 1
        • A Offline
          andreas
          last edited by

          True.

          >>> UUID(bytes=b'\xae\xb61\xdc\xd6\xb5e\xb5\xecI\xfd9\x00\x01P\x04'[::-1])
          UUID('04500100-39fd-49ec-b565-b5d6dc31b6ae')
          
          P 1 Reply Last reply Reply Quote 1
          • P Offline
            poesel @andreas
            last edited by

            @andreas - Hallo Andreas! :)
            Cool thanks, thats it.

            @jcaron - compare it with a known UUID. This is for a non-standard service.

            1 Reply Last reply Reply Quote 0
            • P Offline
              poesel
              last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • A Offline
                andreas
                last edited by

                @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.

                1 Reply Last reply Reply Quote 0
                • P Offline
                  poesel
                  last edited by

                  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]
                  
                  1 Reply Last reply Reply Quote 1
                  • A Offline
                    andreas
                    last edited by andreas

                    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

                    1 Reply Last reply Reply Quote 1

                    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
                    • 1 / 1
                    • First post
                      Last post

                    Pycom on Twitter

                    Pycom