#!/usr/bin/python2 import getopt import sys import logging import bluetooth uuid = "11e63bf3-6baa-47d9-b31d-6045138c9add" outputfile = None try: opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) except getopt.GetoptError as err: # print help information and exit: print str(err) # will print something like "option -a not recognized" #usage() sys.exit(2) for o, a in opts: if o in ("-h", "--help"): print( sys.argv[0] + """: a program that will hopefully eventually provide a way to send firmware update zip files to a paired Olio watch. -h, --help Show this help message. -o, --output Set a file to write the output to.""") sys.exit(0) elif o in ("-o", "--output"): outputfile = open(a, "a") if outputfile is None: logging.warn(sys.argv[0] + ": failed to open " + a + ": " + str(err)) else: assert False, sys.argv[0] + ": unknown argument." server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) server_sock.bind(("", bluetooth.PORT_ANY)) server_sock.listen(1) port = server_sock.getsockname()[1] bluetooth.advertise_service(server_sock, "BluetoothChatSecure", service_id = uuid, service_classes = [uuid, bluetooth.SERIAL_PORT_CLASS], profiles = [bluetooth.SERIAL_PORT_PROFILE]) print("Waiting for connection on RFCOMM channel %d" % port) client_sock, client_info = server_sock.accept() macaddress = client_info[0] print("Recieved connection from: " + macaddress) # Endless loop (unless there is an error or disconnect) that reads # data from the Bluetooth RFCOMM socket 1,024 bytes at a time and # prints them to stdout as well as writing them to outputfile if it # is open. Use client_sock.recv(byte size) to read from the socket # and client_sock.write('data') to write to the socket. try: while True: data = client_sock.recv(1024) if len(data) == 0: logging.warn(sys.argv[0] + ': error.') if outputfile: outputfile.close() sys.exit(1) print(data) if outputfile: outputfile.write(data) except IOError: pass print(macaddress + " disconnected.") if outputfile: outputfile.close() client_sock.close() server_sock.close()