forked from TheWeirdDev/Bluetooth_Headset_Battery_Level
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bl_battery.py
executable file
·64 lines (51 loc) · 1.54 KB
/
bl_battery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
#
# This script prints the battery charge level of some bluetooth headsets
#
# License: GPL-3.0
# Author: @TheWeirdDev
# 29 Sept 2019
import socket, sys
def send(sock, message):
print('>' + message.decode())
sock.send(b"\r\n" + message + b"\r\n")
def getATCommand(sock, line):
print('<' + line.decode())
if b"BRSF" in line:
send(sock, b"+BRSF:20")
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"IPHONEACCEV" in line:
if not b',' in line:
return True
parts = line[line.index(b',') + 1 : -1].split(b',')
if len(parts) < 1 or (len(parts) % 2) != 0:
return True
i = 0
while i < len(parts):
key = int(parts[i])
val = int(parts[i + 1])
if key == 1:
blevel = (val + 1) * 10
print("Battery level: {}%\n".format(blevel))
return False
i += 2
else:
send(sock, b"OK")
return True
def main():
if (len(sys.argv) < 2):
print("Usage: bl_battery.py <BT_MAC_ADDRESS>")
exit()
BT_ADDRESS = sys.argv[1]
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((BT_ADDRESS, 3))
while getATCommand(s, s.recv(128)):
pass
if __name__ == "__main__":
main()