Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created June 6, 2025 20:38
Show Gist options
  • Save bradmartin333/d34f6b1dba8156375ef0bcdaa37b3f51 to your computer and use it in GitHub Desktop.
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
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