omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Writing hex value to BLE characteristic

    Pythonista
    ble
    3
    7
    1702
    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.
    • Aarown1017
      Aarown1017 last edited by Aarown1017

      I need to write "A00101A2" to a BLE Relay and cannot figure out how. The following does not work.
      self.peripheral.write_characteristic_value(c, '0xA00101A2', False)

      Have no idea how to format "A00101A2" for the Pythonista core bluetooth module. I can use something like "nRF Connect" or "BLE Hero" to set the value and it works.

      Executing self.peripheral.write_characteristic_value(c, '0xA00101A2', False) sets the value to something else thats not A00101A2

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        You might try swapping bytes -- BLE is supposed to send dat in little endian byte ordering, but your iPad is a big endian device.

        Since you are sending 32 bits, you might also need to swap the upper 16 and lower 16 bits -- sometimes characteristics contain two "fields".

        1 Reply Last reply Reply Quote 0
        • Aarown1017
          Aarown1017 last edited by

          Any insight on how to do that? Reading up and trying to learn....
          My background is mostly Bash and Powershell scripting.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @Aarown1017 last edited by

            @Aarown1017 the easiest is just to swap manually, if you are always writing a fixed value

            Instead of
            self.peripheral.write_characteristic_value(c, '0xA00101A2', False)
            Try
            self.peripheral.write_characteristic_value(c, '0xA20101A0, False)
            Or maybe
            self.peripheral.write_characteristic_value(c, '0x01A0A201', False)

            (A byte is two hex characters)

            Can you read back this characteristic? If so, what are you getting when you read it back?

            What device and GATT are you using?

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              Sorry, looking back at docs, the data needs to be a byte string. You passed an actual string.

              What you want is probably

              self.peripheral.write_characteristic_value(c, 0xA00101A2.to_bytes(4, 'little'), False)

              Or maybe
              self.peripheral.write_characteristic_value(c, 0xA00101A2.to_bytes(4, 'big), False)

              Or possibly
              self.peripheral.write_characteristic_value(c, 0x01A2A001.to_bytes(4, 'little'), False)

              Or maybe
              self.peripheral.write_characteristic_value(c, 0x01A2A001.to_bytes(4, 'big), False)

              cvp 1 Reply Last reply Reply Quote 1
              • cvp
                cvp @JonB last edited by

                @JonB said

                looking back at docs, the data needs to be a byte string

                I had seen that but all examples in the forum are strings, I did not understand as I never had used BLÉ and I did not answer.

                1 Reply Last reply Reply Quote 0
                • Aarown1017
                  Aarown1017 last edited by

                  @JonB said:

                  Sorry, looking back at docs, the data needs to be a byte string. You passed an actual string.
                  self.peripheral.write_characteristic_value(c, 0xA00101A2.to_bytes(4, 'big'), False)

                  THIS WORKED!!!!! I cannot thank you enough.
                  Hope enough people find this thread because there were so many unanswered threads online.

                  This is the device I have:
                  https://www.amazon.com/dp/B07MKMFP6H/ref=cm_sw_r_tw_dp_46SC6RK392XTNVHDRE89
                  https://drive.google.com/file/d/1aUKZaKQnj6v4Jz-hOtDGEAVyBKMAJmQk/view?usp=sharing

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors