Created
January 16, 2024 22:25
-
-
Save madushan1000/9744eb6350a5dd9685fb6bfbb25fbb8a to your computer and use it in GitHub Desktop.
See battery levels of a zmk split ble keyboard on linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dbus_next.aio import MessageBus | |
from dbus_next.constants import BusType | |
import asyncio | |
BLUEZ = "org.bluez" | |
#replace hci0 with your bluetooth adapter name and FF_FF_FF_FF_FF_FF with your keyboard address | |
BLUEZ_PATH = "/org/bluez/hci0/dev_FF_FF_FF_FF_FF_FF" | |
GATT_SERVICE = 'org.bluez.GattService1' | |
GATT_CHARACTERISCITC = 'org.bluez.GattCharacteristic1' | |
GATT_CHARACTERISCITC_DESCR = 'org.bluez.GattDescriptor1' | |
BATTERY_UUID = "0000180f-0000-1000-8000-00805f9b34fb" | |
BATTERY_LEVEL_UUID = "00002a19-0000-1000-8000-00805f9b34fb" | |
BATTERY_USER_DESC = "00002901-0000-1000-8000-00805f9b34fb" | |
loop = asyncio.get_event_loop() | |
async def main(): | |
bus = await MessageBus(bus_type=BusType.SYSTEM).connect() | |
# the introspection xml would normally be included in your project, but | |
# this is convenient for development | |
introspection = await bus.introspect(BLUEZ, BLUEZ_PATH) | |
device = bus.get_proxy_object(BLUEZ, BLUEZ_PATH, introspection) | |
for svc in device.child_paths: | |
intp = await bus.introspect(BLUEZ, svc) | |
proxy = bus.get_proxy_object(BLUEZ, svc, intp) | |
intf = proxy.get_interface(GATT_SERVICE) | |
if BATTERY_UUID == await intf.get_uuid(): | |
for char in proxy.child_paths: | |
intp = await bus.introspect(BLUEZ, char) | |
proxy = bus.get_proxy_object(BLUEZ, char, intp) | |
intf = proxy.get_interface(GATT_CHARACTERISCITC) | |
level = int.from_bytes(await intf.call_read_value({})) | |
if BATTERY_LEVEL_UUID == await intf.get_uuid(): | |
props = proxy.get_interface('org.freedesktop.DBus.Properties') | |
for desc in proxy.child_paths: | |
intp = await bus.introspect(BLUEZ, desc) | |
proxy = bus.get_proxy_object(BLUEZ, desc, intp) | |
intf = proxy.get_interface(GATT_CHARACTERISCITC_DESCR) | |
name = "Main" | |
if BATTERY_USER_DESC == await intf.get_uuid(): | |
name = bytearray(await intf.call_read_value({})).decode() | |
print(name + ": ", str(level)) | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment