serialization and deserialization
This commit is contained in:
@@ -55,6 +55,10 @@ class BluetoothHandler:
|
||||
|
||||
self.advertise()
|
||||
|
||||
def deserialize_msg(self, s: bytes):
|
||||
# returns packet type (int) and deserialized data
|
||||
return s[0], eval(s[1:].decode())
|
||||
|
||||
def _get_mac_address(self):
|
||||
mac = self.ble.config("mac")[1]
|
||||
return ':'.join('{:02X}'.format(b) for b in mac)
|
||||
@@ -108,10 +112,9 @@ class BluetoothHandler:
|
||||
self.advertise()
|
||||
elif event == IRQ_GATTS_WRITE:
|
||||
conn_handle, value_handle = data
|
||||
|
||||
msg = self.ble.gatts_read(value_handle)
|
||||
packet_type, msg = self.deserialize_msg(msg)
|
||||
|
||||
print(f"Received: \"{msg}\"")
|
||||
|
||||
if value_handle == self.tx_handle:
|
||||
msg = self.ble.gatts_read(self.tx_handle)
|
||||
|
||||
if msg == b"ping":
|
||||
for conn in self.connections:
|
||||
self.ble.gatts_notify(conn, self.rx_handle, b"pong")
|
||||
Reference in New Issue
Block a user