Skip to content

Instantly share code, notes, and snippets.

@valeth
Created March 23, 2021 22:58
Show Gist options
  • Save valeth/0ad72473948077186c38b5ca231882ee to your computer and use it in GitHub Desktop.
Save valeth/0ad72473948077186c38b5ca231882ee to your computer and use it in GitHub Desktop.
import bluetooth
def main(uuid):
# port = find_port(uuid)
port = 11
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((uuid, port))
response = sock.recv(128)
# print(response) # AT+VGS=10\r
sock.send(b"\r\nOK\r\n")
response = sock.recv(128)
# print(response) # AT+XAPL=0000-0000-0100,7\r
sock.send(b"\r\n+XAPL: Android,9\r\n")
sock.send(b"\r\nOK\r\n")
response = sock.recv(128)
# print(response) # \nAT+IPHONEACCEV=2,1,8,2,0\r
_num_pairs, *pairs = response.strip().split(b"=")[-1].split(b",")
pairs = [tuple(pairs[i:i+2]) for i in range(0, len(pairs), 2)]
for k,v in pairs:
if k == b"1":
print("Battery Level:", battery_percent(v))
elif k == b"2":
print("Dock state:", v)
else:
print("Unknown key:", k, v)
def battery_percent(val):
return f"{(int(val) + 1) * 10}%"
def find_port(dev):
# Handsfree Service UUID
uuid = "0000111e-0000-1000-8000-00805f9b34fb"
proto = bluetooth.find_service(address = dev, uuid = uuid)
if len(proto) == 0:
print("couldn't find service port")
exit()
else:
return proto[0]["port"]
if __name__ == "__main__":
from sys import argv
main(argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment