Created
June 6, 2025 20:38
-
-
Save bradmartin333/d34f6b1dba8156375ef0bcdaa37b3f51 to your computer and use it in GitHub Desktop.
discover a bluetooth printer and send it a CLI argument message to print
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bluetooth | |
import sys | |
if len(sys.argv) != 2: | |
print(f"usage: {sys.argv[0]} <message>") | |
sys.exit(1) | |
message = sys.argv[1] | |
nearby_devices = bluetooth.discover_devices( | |
duration=8, lookup_names=True, flush_cache=True, lookup_class=False | |
) | |
for addr, name in nearby_devices: | |
if name and "printer" in name.lower(): | |
try: | |
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) | |
sock.connect((addr, 1)) | |
sock.send(message + "\n\n\n") | |
sock.close() | |
except Exception as e: | |
print("could not connect to printer: %s" % e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment