Add bluetooth battery

This commit is contained in:
Alexander Bantyev 2020-08-04 21:52:30 +03:00
parent cdada48f7f
commit fa48f88cba
Signed by: balsoft
GPG Key ID: E081FF12ADCB4AD5
3 changed files with 116 additions and 9 deletions

View File

@ -1,21 +1,37 @@
{ iconfont, bash, bluez, pulseaudio, utillinux, ... }: { iconfont, bash, bluez, pulseaudio, utillinux, python3, ... }:
'' ''
#!${bash}/bin/bash #!${bash}/bin/bash
if ${utillinux}/bin/rfkill | grep bluetooth > /dev/null; then if ${utillinux}/bin/rfkill | grep bluetooth > /dev/null; then
if ${utillinux}/bin/rfkill | grep bluetooth | grep blocked > /dev/null; then if ${utillinux}/bin/rfkill | grep bluetooth | grep blocked > /dev/null; then
if ${bluez}/bin/bluetoothctl info > /dev/null; then if ${bluez}/bin/bluetoothctl info > /dev/null; then
if ${pulseaudio}/bin/pactl list sinks | grep bluez > /dev/null; then if ${pulseaudio}/bin/pactl list sinks | grep bluez > /dev/null; then
echo "" echo -n ""
exit 33
else else
echo "" echo -n ""
fi fi
else else
echo "" echo -n ""
fi fi
else else
echo "" echo -n ""
exit 33
fi fi
fi fi
DEVICE=$(${bluez}/bin/bluetoothctl info | grep -o "[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]" | head -1)
CHARGE=$(${python3.withPackages (ps: [ ps.pybluez ])}/bin/python3 ${./bluetooth_battery.py} $DEVICE)
code=0
case $CHARGE in
1?) icon=; code=33;;
2?) icon=; code=33;;
3?) icon=;;
4?) icon=;;
5?) icon=;;
6?) icon=;;
7?) icon=;;
8?) icon=;;
9?) icon=;;
100) icon=;;
*) ; code=33;;
esac
echo "$icon"
exit $code
'' ''

View File

@ -0,0 +1,91 @@
#!/usr/bin/env python3
"""
A python script to get battery level from Bluetooth headsets
Shamelessly stolen from https://github.com/TheWeirdDev/Bluetooth_Headset_Battery_Level
"""
# License: GPL-3.0
# Author: @TheWeirdDev
# 29 Sept 2019
import errno
import bluetooth
import sys
def send(sock, message):
sock.send(b"\r\n" + message + b"\r\n")
def getATCommand(sock, line, device):
blevel = -1
if b"BRSF" in line:
send(sock, b"+BRSF: 1024")
send(sock, b"OK")
elif b"CIND=" in line:
send(sock, b"+CIND: (\"battchg\",(0-5))")
send(sock, b"OK")
elif b"CIND?" in line:
send(sock, b"+CIND: 5")
send(sock, b"OK")
elif b"BIND=?" in line:
# Announce that we support the battery level HF indicator
# https://www.bluetooth.com/specifications/assigned-numbers/hands-free-profile/
send(sock, b"+BIND: (2)")
send(sock, b"OK")
elif b"BIND?" in line:
# Enable battery level HF indicator
send(sock, b"+BIND: 2,1")
send(sock, b"OK")
elif b"XAPL=" in line:
send(sock, b"+XAPL: iPhone,7")
send(sock, b"OK")
elif b"IPHONEACCEV" in line:
parts = line.strip().split(b',')[1:]
if len(parts) > 1 and (len(parts) % 2) == 0:
parts = iter(parts)
params = dict(zip(parts, parts))
if b'1' in params:
blevel = (int(params[b'1']) + 1) * 10
elif b"BIEV=" in line:
params = line.strip().split(b"=")[1].split(b",")
if params[0] == b"2":
blevel = int(params[1])
else:
send(sock, b"OK")
if blevel != -1:
print(blevel)
return False
return True
def main():
if (len(sys.argv) < 2):
print("Usage: bl_battery.py <BT_MAC_ADDRESS_1>[.PORT] ...")
print(" Port number is optional (default = 4)")
exit()
else:
for device in sys.argv[1:]:
i = device.find('.')
if i == -1:
port = 10
else:
port = int(device[i+1:])
device = device[:i]
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((device, port))
while getATCommand(s, s.recv(128), device):
pass
s.close()
except OSError as e:
print(f"{device} is offline", e)
if __name__ == "__main__":
main()

View File

@ -1,4 +1,4 @@
{ pamixer, lxqt, iconfont, ... }: '' { pamixer, lxqt, iconfont, config, lib, ... }: ''
case $BLOCK_BUTTON in case $BLOCK_BUTTON in
2) ${pamixer}/bin/pamixer -t;; 2) ${pamixer}/bin/pamixer -t;;
4) ${pamixer}/bin/pamixer -i 5;; 4) ${pamixer}/bin/pamixer -i 5;;
@ -26,7 +26,7 @@
fi fi
fi fi
fi fi
[[ -n $BLOCK_BUTTON ]] && text=" $volume$end" ${lib.optionalString (! config.deviceSpecific.bigScreen) "[[ -n $BLOCK_BUTTON ]] &&"} text=" $volume$end"
echo "<span font='${iconfont}'>$icon</span>$text" echo "<span font='${iconfont}'>$icon</span>$text"
exit $code exit $code
'' ''