Skip to content

Commit

Permalink
Script to check battery levels of both the halves
Browse files Browse the repository at this point in the history
  • Loading branch information
tannakartikey committed Feb 3, 2024
1 parent 82494b5 commit a98970e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions split_battery_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from datetime import datetime
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_EC_88_89_88_6D_2A"
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({}), byteorder='little')
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))

# print the current time here
print(datetime.now())
loop.run_until_complete(main())

0 comments on commit a98970e

Please sign in to comment.