Skip to content

Commit

Permalink
made connection callback more accessible.
Browse files Browse the repository at this point in the history
peripheral.py
Added properties to wrap the self.dongle.on_connect
  • Loading branch information
a-krawciw committed Feb 16, 2021
1 parent 16c0f41 commit a1cccea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions bluezero/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,28 @@ def publish(self):
except KeyboardInterrupt:
self.mainloop.quit()
self.ad_manager.unregister_advertisement(self.advert)

@property
def on_connect(self):
"""
Callback for when a device connects to the peripheral.
Callback should take one parameter which is a Bluezero Device object
"""
return self.dongle.on_connect

@on_connect.setter
def on_connect(self, callback):
self.dongle.on_connect = callback

@property
def on_disconnect(self):
"""
Callback for when a device disconnects from the peripheral.
Callback takes either one parameter: a Bluezero Device object
or no parameters
"""
return self.dongle.on_disconnect

@on_disconnect.setter
def on_disconnect(self, callback):
self.dongle.on_disconnect = callback
4 changes: 2 additions & 2 deletions examples/ble_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def main(adapter_address):
read_callback=None,
write_callback=None)

ble_uart.dongle.on_connect = on_connect
ble_uart.dongle.on_disconnect = on_disconnect
ble_uart.on_connect = on_connect
ble_uart.on_disconnect = on_disconnect

ble_uart.publish()

Expand Down

0 comments on commit a1cccea

Please sign in to comment.