serialization and deserialization
This commit is contained in:
@@ -55,6 +55,10 @@ class BluetoothHandler:
|
|||||||
|
|
||||||
self.advertise()
|
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):
|
def _get_mac_address(self):
|
||||||
mac = self.ble.config("mac")[1]
|
mac = self.ble.config("mac")[1]
|
||||||
return ':'.join('{:02X}'.format(b) for b in mac)
|
return ':'.join('{:02X}'.format(b) for b in mac)
|
||||||
@@ -109,9 +113,8 @@ class BluetoothHandler:
|
|||||||
elif event == IRQ_GATTS_WRITE:
|
elif event == IRQ_GATTS_WRITE:
|
||||||
conn_handle, value_handle = data
|
conn_handle, value_handle = data
|
||||||
|
|
||||||
if value_handle == self.tx_handle:
|
msg = self.ble.gatts_read(value_handle)
|
||||||
msg = self.ble.gatts_read(self.tx_handle)
|
packet_type, msg = self.deserialize_msg(msg)
|
||||||
|
|
||||||
|
print(f"Received: \"{msg}\"")
|
||||||
|
|
||||||
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