BLE Examples bluezero stretch

From embeddedTS Manuals

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
git checkout f9179dc057d1428fd2d95eab82db4172c18d5e0b
python3 setup.py install
cp examples/ukBaz.bluezero.conf /etc/dbus-1/system.d/ #This is needed to register python-bluezero as a DBus peripheral application. The provided file is purely an example and may need modifications for specific situations.
Note: The commit of python-bluezero that is checked out is fairly old at this point, however, newer releases have a dependency of Python 3.6 which is not available on our platforms running Debian Stretch or older. If using newer distribution, it is advised to use latest release.


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!) and bluetoothd 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@tsimx28:~/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:

Ble eddystone url.png


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.

Note: There is a bug present in the Python binary distributed with Debian Stretch that prevents this example from working correctly. It is advised to either move to Debian Buster (or newer) or move to Buildroot. In both of these, Python sockets with respect to BLE are correctly implemented allowing aioblescan to work correctly. Please contact us with any questions regarding these issues.


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!) and bluetoothd 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@tsimx28:~/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@tsimx28:~# 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}
...