Skip to content

Latest commit

 

History

History
124 lines (99 loc) · 3.34 KB

File metadata and controls

124 lines (99 loc) · 3.34 KB
jupyter
jupytext kernelspec
text_representation
extension format_name format_version jupytext_version
.md
markdown
1.2
1.7.1
display_name language name
Python 3
python
python3

Android Bluetooth Low Energy functions

BLE functions could be used with the able library.

%load_ext pythonhere
%connect-there
%%there
from kivy.logger import Logger
from able import BluetoothDispatcher, GATT_SUCCESS

BLE dispatcher callbacks results should be printed in logs:

%there -b log

Setup BLE interface

with logging callbacks

%%there
class BLE(BluetoothDispatcher):

    def on_connection_state_change(self, status, state):
        Logger.info("on_connection_state_change: status=%s, state=%s", status, state)
        if status == GATT_SUCCESS and state:
            Logger.info("Connection: succeed")

    def on_services(self, status, services):
        Logger.info("on_services: status=%s", status)
        if status == GATT_SUCCESS:
            Logger.info("services discovered: %s", list(services.keys()))
            # save discovered services object
            self.services = services

    def on_characteristic_read(self, characteristic, status):
        Logger.info("on_characteristic_read: status=%s, characteristic=%s", status,
                    characteristic.getUuid().toString())
        if status == GATT_SUCCESS:
            Logger.info("Characteristic read: succeed")


ble = BLE()
print(ble)

Get list of paired BLE devices

%%there
for device in ble.bonded_devices:
    # device: https://developer.android.com/reference/android/bluetooth/BluetoothDevice
    print(type(device), device.getName(), "address:", device.getAddress())

Connect to remote device by a hardware address

In this example device hardware address address is known.

%%there
ble.connect_by_device_address("AA:AA:AA:AA:AA:11")

Discover device services and characteristics

Wait a while, while device is connected. Start services discovery:

%%there -d 5
ble.discover_services()

Wait a while, while services discovered. Print connected device characteristics:

%%there -d 2
print(type(ble.services))
for service, characteristics in ble.services.items():
    print(f"Service {service} characteristics:")
    for characteristic in characteristics:
        print(f"\t*{characteristic}")

Read characteristic

In this example, it is known that target device has characteristic with UUID = 16fe0d01-c111-11e3-b8c8-0002a5d5c51b
This characteristic is readable, and always returns a string value: "test".

%%there
characteristic = ble.services.search("0d01")  
print(f"Characteristic UUID found: {characteristic.getUuid().toString()}")
print(f"Characteristic object: {characteristic}")
# https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic#getStringValue(int)
print(f"Characteristic value is not available yet: {characteristic.getStringValue(0)}")

ble.read_characteristic(characteristic)

Wait a while, and check characteristic value.
on_characteristic_read message should appear in logs

%%there -d 2
# https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic#getStringValue(int)
print(characteristic.getStringValue(0))