pycycling.battery_service module

A module for interacting with Bluetooth devices which support the Battery Service.

Example

This example prints the current battery level to the console. Please see also information on obtaining the Bluetooth address of your device.

import asyncio
from bleak import BleakClient

from pycycling.battery_service import BatteryService


async def run(address):
    async with BleakClient(address) as client:
        battery_service = BatteryService(client)
        battery_level = await battery_service.get_battery_level()
        print(f"Battery is at {battery_level}%")


if __name__ == "__main__":
    import os

    os.environ["PYTHONASYNCIODEBUG"] = str(1)

    device_address = "DEVICE_ADDRESS HERE"
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(device_address))
class pycycling.battery_service.BatteryService(client)[source]

Bases: object

A wrapper around a bleak.backends.client.BaseBleakClient object adding Battery Service specific utility methods.

Parameters:

client – A valid bleak.backends.client.BaseBleakClient object

async get_battery_level()[source]

Returns current battery level of the Bluetooth device.

Returns:

An int representing the current battery level percentage of the Bluetooth device. A value of 100 indicates a fully charged battery while 0 a fully discharged battery.