b3b / able

Python for Android Bluetooth Low Energy package
MIT License
38 stars 18 forks source link

Receiving data through notifications #31

Closed ilw closed 1 year ago

ilw commented 2 years ago

Hi, I really love the simplicity of this library for managing a GATT client. I just wanted to check my understanding about receiving data through notifications. Am i right in thinking that i enable it with "enable_notifications" (in dispatcher.py) and then "on_characteristic_changed" (in dispatcher.py) will be called when a notification for that characteristic is received. So i just have to somehow override the stub callback function "on_characteristic_changed" to handle the data being received?

Thanks!

b3b commented 2 years ago

Hi @ilw , Yes, you are right, that's how it's supposed to work.

class BLE(able.BluetoothDispatcher):

    def on_services(self, status, services):
        characteristic = self.services.search("aaaa")
        if self.enable_notifications(characteristic):
            Logger.info("Notifications enabled")

    def on_characteristic_changed(self, characteristic):
        data = characteristic.getStringValue(0)

And here is example of usage in the BLE functions testing app: https://github.com/b3b/able/blob/master/testapps/bletest/main.py#L222