|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + text_representation: |
| 5 | + extension: .md |
| 6 | + format_name: markdown |
| 7 | + format_version: '1.2' |
| 8 | + jupytext_version: 1.7.1 |
| 9 | + kernelspec: |
| 10 | + display_name: Python 3 |
| 11 | + language: python |
| 12 | + name: python3 |
| 13 | +--- |
| 14 | + |
| 15 | +# Android Bluetooth Low Energy functions |
| 16 | +BLE functions could be used with the [able](https://github.com/b3b/able) library. |
| 17 | + |
| 18 | +```python |
| 19 | +%load_ext pythonhere |
| 20 | +%connect-there |
| 21 | +``` |
| 22 | + |
| 23 | +```python |
| 24 | +%%there |
| 25 | +from kivy.logger import Logger |
| 26 | +from able import BluetoothDispatcher, GATT_SUCCESS |
| 27 | +``` |
| 28 | + |
| 29 | +BLE dispatcher callbacks results should be printed in logs: |
| 30 | + |
| 31 | +```python |
| 32 | +%there -b log |
| 33 | +``` |
| 34 | + |
| 35 | +## Setup BLE interface |
| 36 | +with logging callbacks |
| 37 | + |
| 38 | +```python |
| 39 | +%%there |
| 40 | +class BLE(BluetoothDispatcher): |
| 41 | + |
| 42 | + def on_connection_state_change(self, status, state): |
| 43 | + Logger.info("on_connection_state_change: status=%s, state=%s", status, state) |
| 44 | + if status == GATT_SUCCESS and state: |
| 45 | + Logger.info("Connection: succeed") |
| 46 | + |
| 47 | + def on_services(self, status, services): |
| 48 | + Logger.info("on_services: status=%s", status) |
| 49 | + if status == GATT_SUCCESS: |
| 50 | + Logger.info("services discovered: %s", list(services.keys())) |
| 51 | + # save discovered services object |
| 52 | + self.services = services |
| 53 | + |
| 54 | + def on_characteristic_read(self, characteristic, status): |
| 55 | + Logger.info("on_characteristic_read: status=%s, characteristic=%s", status, |
| 56 | + characteristic.getUuid().toString()) |
| 57 | + if status == GATT_SUCCESS: |
| 58 | + Logger.info("Characteristic read: succeed") |
| 59 | + |
| 60 | + |
| 61 | +ble = BLE() |
| 62 | +print(ble) |
| 63 | +``` |
| 64 | + |
| 65 | +## Connect to remote device by a hardware address |
| 66 | +In this example device hardware address address is known. |
| 67 | + |
| 68 | +```python |
| 69 | +%%there |
| 70 | +if not getattr(app, "device_address", None): |
| 71 | + app.device_address = "FF:FF:FF:FF:FF:FF" |
| 72 | + |
| 73 | +ble.connect_by_device_address(app.device_address) |
| 74 | +``` |
| 75 | + |
| 76 | +## Discover device services and characteristics |
| 77 | +Wait a while, while device is connected. Start services discovery: |
| 78 | + |
| 79 | +```python |
| 80 | +%%there -d 5 |
| 81 | +ble.discover_services() |
| 82 | +``` |
| 83 | + |
| 84 | +Wait a while, while services discovered. Print connected device characteristics: |
| 85 | + |
| 86 | +```python |
| 87 | +%%there -d 2 |
| 88 | +print(type(ble.services)) |
| 89 | +for service, characteristics in ble.services.items(): |
| 90 | + print(f"Service {service} characteristics:") |
| 91 | + for characteristic in characteristics: |
| 92 | + print(f"\t*{characteristic}") |
| 93 | +``` |
| 94 | + |
| 95 | +## Read characteristic |
| 96 | +In this example, it is known that target device has characteristic |
| 97 | +with UUID = 16fe**0d01**-c111-11e3-b8c8-0002a5d5c51b |
| 98 | +This characteristic is readable, and always returns a string value: "test". |
| 99 | + |
| 100 | +```python |
| 101 | +%%there |
| 102 | +characteristic = ble.services.search("0d01") |
| 103 | +print(f"Characteristic UUID found: {characteristic.getUuid().toString()}") |
| 104 | +print(f"Characteristic object: {characteristic}") |
| 105 | +# https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic#getStringValue(int) |
| 106 | +print(f"Characteristic value is not available yet: {characteristic.getStringValue(0)}") |
| 107 | + |
| 108 | +ble.read_characteristic(characteristic) |
| 109 | +``` |
| 110 | + |
| 111 | +Wait a while, and check characteristic value. |
| 112 | +**on_characteristic_read** message should appear in logs |
| 113 | + |
| 114 | +```python |
| 115 | +%%there -d 2 |
| 116 | +# https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic#getStringValue(int) |
| 117 | +print(characteristic.getStringValue(0)) |
| 118 | +``` |
0 commit comments