BLE Examples bluezero buster plus
In order to continue with our examples, python-bluezero will be used. The python-bluezero project aims to provide a BlueZ API with zero boilerplate code that would normally be required. This API provides a number of very simple examples for testing and forward development of BLE applications. The API is fairly well documented and makes BLE application development in Python quite simple.
Note that Technologic Systems has no affiliation with this project. We simply have found that it served the purpose of providing real examples with a quick development thanks to its abstraction. At the time of writing this article, python-bluezero has had recent development and shows no signs of stalling. There are many different APIs for communicating with BlueZ and DBus; we are not able to recommend a particular API over any others.
Installing python-bluezero
The following commands can be used to clone the project and install it. Be sure to have first installed BlueZ from source before installing and running the examples:
pip3 install evdev aioblescan
git clone https://github.com/ukBaz/python-bluezero
cd python-bluezero
python3 setup.py install
Eddystone-URL Beacon
Eddystone is an open beacon format developed by Google and designed with transparency and robustness in mind. The Eddystone format can provide several different kinds of payloads including unique ID, URL broadcasting, and even a time-varying beacon that can be resolved to a stable identifier.
Prerequisites
- The example was tested against a smartphone with BLE capabilities, using a simple BLE beacon scanning tool.
- The Bluetooth device must already be set up via the device's manual's instructions (without the use of the
hciconfig
command!) andbluetoothd
started via system as noted above. - The python-bluezero project installed as outlined above.
- Be running as
root
user or a user with proper permissions.
Note: | Due to caching on some devices, it may be necessary to completely exit any BLE applications/tools, and potentially even power-cycling the BLE device itself on any devices connecting to the example GATT server below. This has been observed to be necessary on at least one specific smartphone that was used to connect to the unit's on-board BLE module running these examples. |
Running
The example can be run with:
root@tsimx6:~/python-bluezero# python3 examples/eddystone_url_beacon.py Advertisement registered
The beacon can be observed on a device running a scanner such as the tool below:
Eddystone Beacon Scanner
This example will constantly poll for and print out Eddystone format beacons. This tool will readily print URL and UID beacons, but does not appear to correctly handle EIM or TLM beacons at this time. This may change in the future.
Prerequisites
- The example was tested against a smartphone with BLE capabilities, using a simple BLE beacon emulation tool.
- The Bluetooth device must already be set up via the device's manual's instructions (without the use of the
hciconfig
command!) andbluetoothd
started via system as noted above. - The python-bluezero project installed as outlined above.
- Be running as
root
user or a user with proper permissions.
Running
The example can be run with:
root@tsimx6:~/python-bluezero# python3 examples/eddystone_scanner.py ... New Eddystone data: url = https://www.embeddedTS.com/ mac address = XX:XX:XX:XX:XX:XX tx_power = -69 rssi = -78 ... New Eddystone data: name space = 0xC7ACE534120F67D5345A rssi = -72 mac address = XX:XX:XX:XX:XX:XX tx_power = -65 instance = 0x42
Using aioblescan for Eddystone TLM Beacons
The latest version of the Python library aioblescan
correctly supports and prints Eddystone TLM beacons. Using the beacon emulator above, or a hardware beacon such at the ON Semi RSL10-COIN-GEVB demo board, TLM packets can be received and decoded:
root@tsimx6:~# python3 -m aioblescan -e Google Beacon {'tx_power': 0, 'url': 'https://www.onsemi.com/b-idk', 'mac address': '60:c0:bf:26:df:db', 'rssi': -51} ... Google Beacon {'temperature': 22.75, 'battery': 2478, 'uptime': 78100, 'pdu count': 109} Google Beacon {'tx_power': 0, 'url': 'https://www.onsemi.com/b-idk', 'mac address': '60:c0:bf:26:df:db', 'rssi': -49} ... Google Beacon {'temperature': 22.75, 'battery': 2479, 'uptime': 83000, 'pdu count': 116} Google Beacon {'tx_power': 0, 'url': 'https://www.onsemi.com/b-idk', 'mac address': '60:c0:bf:26:df:db', 'rssi': -49} ...