Skip to content

Instantly share code, notes, and snippets.

@atx
Last active October 21, 2019 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atx/7d5e2415dc4925c3b3ee776bd7f14571 to your computer and use it in GitHub Desktop.
Save atx/7d5e2415dc4925c3b3ee776bd7f14571 to your computer and use it in GitHub Desktop.
Hacky way of displaying headset battery level by sniffing the raw bluetooth traffic and dumping it to a file
#! /usr/bin/env python3
import argparse
import pathlib
import pyshark
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", type=lambda s: pathlib.Path(s).expanduser(), required=True)
parser.add_argument("-i", "--interface", default="bluetooth0")
args = parser.parse_args()
# TODO: MAC address filtering
shark = pyshark.LiveCapture(
interface=args.interface,
display_filter='bthsp.at_cmd == "+XEVENT" and bthsp.parameter == "BATTERY"'
)
for packet in shark.sniff_continuously():
field = packet.bthsp.parameter.fields[1]
# TODO: Maybe make this atomic?
args.output.write_text(field.show)
print("RX:", packet.bthsp.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment