Skip to content

Commit

Permalink
Add logging module and verbose command line option
Browse files Browse the repository at this point in the history
Useful for debugging.
  • Loading branch information
drinkcat committed Dec 13, 2023
1 parent ef80afe commit e4aa2a8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion bluetooth_battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

import argparse
import bluetooth
import logging
from typing import Optional, Union, List, Dict

logger = logging.getLogger(__name__)

class BatteryQueryError(bluetooth.BluetoothError):
pass
Expand All @@ -31,7 +33,9 @@ def __next__(self):
"""
Receive chunks
"""
return self._sock.recv(self._chunk_size)
data = self._sock.recv(self._chunk_size)
logger.debug("<<< " + str(data))
return data


class RFCOMMSocket(bluetooth.BluetoothSocket):
Expand Down Expand Up @@ -63,6 +67,7 @@ def send(self, data):
"""
This function sends a message through a bluetooth socket with added line separators
"""
logger.debug(">>> " + str(data))
return super().send(b"\r\n" + data + b"\r\n")


Expand Down Expand Up @@ -98,7 +103,9 @@ def _perform_query(self) -> int:
"""
result = None
sock = RFCOMMSocket()
logger.debug("Connecting to {}.{}".format(self._bt_settings[0], self._bt_settings[1]))
sock.connect(self._bt_settings)
logger.debug("Connected")
# Iterate received packets until there is no more or a result was found
for line in sock:
if b"BRSF" in line:
Expand Down Expand Up @@ -162,7 +169,10 @@ def main():
parser = argparse.ArgumentParser(description="Get battery level from Bluetooth headsets")
parser.add_argument("devices", metavar="DEVICE_MAC[.PORT]", type=str, nargs="+",
help="(MAC address of target)[.SPP Port]")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose logs")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
for device in args.devices:
query = BatteryStateQuerier(*device.split("."))
print("Battery level for {} is {}".format(device, str(query)))
Expand Down

0 comments on commit e4aa2a8

Please sign in to comment.