#!/usr/bin/python import sys from os.path import basename from lightblue.obex import OBEXClient if len(sys.argv) < 4: print sys.argv[0] + ": <btaddr> <channel> <file>" sys.exit(0) btaddr = sys.argv[1] channel = int(sys.argv[2]) my_file = sys.argv[3] print "Sending %s to %s on channel %d" % (my_file, btaddr, channel) obex = OBEXClient(btaddr, channel) obex.connect() obex.put({'name': basename(my_file)}, open(my_file, "rb")) obex.disconnect()
def __send_files(self, service): address = service["host"] port = service["port"] #device.HandleSignal(handler, signal) #print device try: client = OBEXClient(service["host"], service["port"]) attempts = self.__config.get_values('attempts') if attempts == None: attempts = 1 else: attempts = int(attempts[0]) attemptdelay = self.__config.get_values('attemptdelay') if attemptdelay == None: attemptdelay = 10 else: attemptdelay = int(attemptdelay[0]) # Unsupported attemptrfcommtimeout connected = False for i in range(1, attempts + 1): try: #print "# * Connecting(%d/%d) to %s port:%s" % (i, attempts, address, port) cresp = client.connect() if cresp.code == lightblue.obex.OK: connected = True break else: device = self.__adapter.CreatePairedDevice( address, self.__agent, 'NoInputNoOutput', reply_handler=None, error_handler=None) continue except Exception as error: self.__log_retry(address) time.sleep(attemptdelay) continue if not connected: self.__blocklist.update(address, status="FAIL") return False else: nr_of_files = len(self.__files) it = 0 if (len(self.__files) > 0): for i in self.__files: for k in i.keys(): try: try: send_file = file(i.get(k), "rb") except Exception as ioerror: self.__logger.sent( filename=k, address=address, error_code=ObexSenderLogger. ERROR_FILE_NOT_FOUND) continue client.put({'name': k}, send_file) it += 1 if (it == nr_of_files): self.__logger.sent( filename=k, address=address, error_code=ObexSenderLogger. ERROR_FILE_SENT) else: self.__logger.sent( filename=k, address=address, error_code=ObexSenderLogger.ERROR_MORE) except Exception as error: self.__log_retry(address, offset=it) self.__blocklist.update(service["host"], status="FAIL") return False return True else: self.__logger.sent( filename=k, address=address, error_code=ObexSenderLogger.ERROR_CONNECTED) return True except OBEXError as error: print(error) self.__blocklist.update(address, status="FAIL") return False