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
-
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 -
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".
-
Any insight on how to do that? Reading up and trying to learn....
My background is mostly Bash and Powershell scripting. -
@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?
-
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) -
@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.
-
@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