Skip to content

example: Add async example of GATT interface - #5

Merged
igo95862 merged 1 commit into
python-sdbus:masterfrom
kawasaki:dev
Apr 22, 2023
Merged

example: Add async example of GATT interface#5
igo95862 merged 1 commit into
python-sdbus:masterfrom
kawasaki:dev

Conversation

@kawasaki

@kawasaki kawasaki commented Apr 16, 2023

Copy link
Copy Markdown
Contributor

Hello @igo95862,

Recently, I wrote a simple python script and confirmed that python-sdbus-bluez works with BLE (Bluetooth Low Energy) device as expected. I suggest to add the script to python-sdbus-bluez as another example. Could you take a look in it?

@igo95862

Copy link
Copy Markdown
Contributor

Hello @kawasaki

Thank you for submitting pull request.

Let me review the code.

Comment thread examples/sdbus_async/gatt_props.py Outdated
# Clean up
await device.disconnect()

run(main())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind putting this function call under if __name__ == "__main__":. It is considered a good practice. (also the if len(sys.argv) < 2:)

Comment thread examples/sdbus_async/gatt_props.py Outdated
from typing import Any

from sdbus import SdBus, sd_bus_open_system
from sdbus.dbus_proxy_async_interfaces import DbusIntrospectableAsync

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way the DbusInterfaceCommonAsync inherits from DbusIntrospectableAsync and does not need to be imported from private internal module.

@kawasaki

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Will rework the code next weekend.

Add an example script which shows properties of services and
characteristics that the specified BLE device provides.

I confirmed this script using micro:bit with Scratch HEX installed.

$ python examples/sdbus_async/gatt_props.py XX:XX:XX:XX:XX:XX
Wait discovery for 5 seconds
Conneting to /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX...
Conneted
Find services...
 service0008
 service000c
 service0013
=============================================
service: service0008
UUID: 00001801-0000-1000-8000-00805f9b34fb
device: /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX
primary: True
includes paths: []
missing propeties: ['handle']
 char0009
    UUID: 00002a05-0000-1000-8000-00805f9b34fb
    service: /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0008
    value: b''
    flags: ['indicate']
    notifying: False
    MTU: 23
missing propeties: ['write acquired', 'notify acquired', 'handle']
=============================================
...

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
@kawasaki

Copy link
Copy Markdown
Contributor Author

I've reflected your comments and also avoided two global variables, DEV_ADDR and DEV_PATH. Thanks again for the comments. It looks better :)

for service in services:
await print_service_props(dbus, dev_path, service)
break
await sleep(1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this an unreachable code because of break?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, the next line "await sleep(1)" has extra indent... Will create another pull request to fix it.

@igo95862
igo95862 merged commit 1fa9b27 into python-sdbus:master Apr 22, 2023
@igo95862

Copy link
Copy Markdown
Contributor

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants