-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wait notification functionality feature #407
Comments
Apologies for the slow response on this. I see you have been offered a way forward with the Bleak library hbldh/bleak#1501 (comment) so is this still of interest? I thinking you are looking for a solution that doesn't use an event loop and blocks until the notification happens. I've put the following quick example together from dataclasses import dataclass
from typing import Optional
from gi.repository import GLib
from bluezero.central import Central
adapter_addr = "xx:xx:xx:xx:xx:xx"
peripheral_addr = "xx:xx:xx:xx:xx:xx"
batt_srvc_uuid = "0000180f-0000-1000-8000-00805f9b34fb"
batt_char_uuid = "00002a19-0000-1000-8000-00805f9b34fb"
@dataclass
class Battery:
battery_value: Optional[int] = None
notification_happened: bool = False
def on_battery_value_changed(self, iface, changed_props, invalidated_props):
print(iface, changed_props, invalidated_props)
if "Value" in changed_props:
self.battery_value = int.from_bytes(changed_props["Value"], "little")
self.notification_happened = True
def wait_on_notification(self):
main_context = GLib.MainContext.default()
self.notification_happened = False
while not self.notification_happened:
main_context.iteration(False)
return self.battery_value
def main():
batt_level = Battery()
batt_device = Central(adapter_addr=adapter_addr, device_addr=peripheral_addr)
batt_characteristic = batt_device.add_characteristic(batt_srvc_uuid, batt_char_uuid)
batt_device.connect()
batt_characteristic.add_characteristic_cb(batt_level.on_battery_value_changed)
batt_characteristic.start_notify()
returned_value = batt_level.wait_on_notification()
batt_characteristic.stop_notify()
batt_device.disconnect()
print(f"Battery value is: {returned_value}")
if __name__ == "__main__":
main() I probably need to think more about this (especially if it was to be added to the library), but it might give you a way forward to experiment and report back if it is what you are looking for. I did this experiment with the BLE Peripheral Simulator |
Yes, I have provided two uploader scripts in order to have user possibility to chose one:
Bleak is by default because supports other platform than Linux.
Yes
Added:
But callback was not called first time and we stuck on
Will be nice to have such functionality in library in future if it's possible |
Will be nice to have functionality like
tx_char.wait_for_notify()
in order to have possibility to write simple scripts of serial communication that works like:I have this issue in my ArduinoBleOTA library for firmware uploading over ble:
https://github.com/vovagorodok/ArduinoBleOTA/blob/7fb9e079cf54e02b0701d117754e14ebe5db34cf/tools/bluezero_uploader.py#L146
The text was updated successfully, but these errors were encountered: