Skip to content

Instantly share code, notes, and snippets.

@davemcphee
Last active August 10, 2021 18:55
Show Gist options
  • Save davemcphee/9fcbc29e2919ff4fd53243d5e6cd0a9f to your computer and use it in GitHub Desktop.
Save davemcphee/9fcbc29e2919ff4fd53243d5e6cd0a9f to your computer and use it in GitHub Desktop.
OW BLE handshake
# attach a callback to notify on SERIAL_READ_UUID reads
client.start_notify(SERIAL_READ_UUID, callback_handler)
# read the firmware value
fw_rev = await client.read_gatt_char(FW_REV_UUID)
# write the firmware value back
client.write_gatt_char(FW_REV_UUID, fw_rev)
# at this point, our callback receives a bytearray[20] in reply
# store first 3 bytes of reply in putput array
outputarray = reply[:3]
# store next 16 bytes in arrayToMD5_part1, dropping the last
arrayToMD5_part1 = reply[3:19]
# create a const bytearray arrayToMD5_part2
arrayToMD5_part2 = bytearray([217, 37, 95, 15, 35, 53, 78, 25, 186, 115, 156, 205, 196, 169, 23, 101])
# add the 2 arrays and md5 hash them
hasharray_digest_bytes = hashlib.md5(arrayToMD5_part1 + arrayToMD5_part2).digest()
# convert bytes from digest to bytearray
hasharray_digest_bytearray = bytearray(hasharray_digest_bytes)
# create a response from first 3 bytes in outputarray and md5 hash
response = outputarray + hasharray_digest_bytearray
# append a 20th value to our response array
response.append(0)
# some kind of crc?
for i in response:
response[19] = i ^ response[19]
# send result to SERIAL_WRITE_UUID
client.write_gatt_char(SERIAL_WRITE_UUID, response)
# hope to read some values from other UUIDs?
ride_mode = client.read_gatt_char(RIDE_MODE_UUID)
# doesn't work, only \x00 \x00 :/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment