def sendFile(dev, filename): if os.path.exists(filename): client = obexftp.client(obexftp.BLUETOOTH) channel = obexftp.browsebt(dev, obexftp.PUSH) print '[>] Sending file to %s@%s' % (dev, str(channel)) client.connect(dev, channel) ret = client.put_file(filename) if int(ret) >= 1: print '[>] File has been sent.' else: print '[!] File has not been accepted.' client.disconnect() else: print '[!] Specified file: "%s" does not exists.'
#!/usr/bin/python import obexftp obex = obexftp.client(obexftp.BLUETOOTH) devs = obex.discover(); print devs; dev = devs[0] print "Using %s" % dev channel = obexftp.browsebt(dev,0) print "Channel %d" % channel print obex.connect(dev, channel) print obex.list("/") print obex.list("/images") data = obex.get("/images/some.jpg") file = open('downloaded.jpg', 'wb') file.write(data) print obex.disconnect() obex.delete
#!/usr/bin/python import obexftp obex = obexftp.client(obexftp.BLUETOOTH) devs = obex.discover() print devs dev = devs[0] print "Using %s" % dev channel = obexftp.browsebt(dev, 0) print "Channel %d" % channel print obex.connect(dev, channel) print obex.list("/") print obex.list("/images") data = obex.get("/images/some.jpg") file = open("downloaded.jpg", "wb") file.write(data) print obex.disconnect() obex.delete
def main(): # Make device visible os.system("hciconfig hci0 piscan") # Create a new server socket using RFCOMM protocol server_sock = BluetoothSocket(RFCOMM) # Bind to any port server_sock.bind(("", PORT_ANY)) # Start listening server_sock.listen(1) # Get the port the server socket is listening port = server_sock.getsockname()[1] # The service UUID to advertise uuid = "00001101-0000-1000-8000-00805f9b34fb" # Start advertising the service advertise_service(server_sock, "RaspiBtSrv", service_id=uuid, service_classes=[uuid, SERIAL_PORT_CLASS], profiles=[SERIAL_PORT_PROFILE]) # Main Bluetooth server loop while True: print ("Waiting for connection on RFCOMM channel %d" % port) try: client_sock = None # This will block until we get a new connection client_sock, client_info = server_sock.accept() print ("Accepted connection from ", client_info) address = client_info[0] dev = client_info[0] channel = obexftp.browsebt(dev, obexftp.PUSH) print(channel) while True: # Read the data sent by the client data = client_sock.recv(1024) if len(data) == 0: break print ("Received") # Handle the request if data == "1": command = "obexftp --nopath --noconn --uuid none --bluetooth "+address+" --channel "+str(channel)+" -c /mnt/Health_tracker_transfer -p /home/pi/Bloxton/Test.wav" os.system(command) except IOError: pass
devices = [] line = process.stdout.readline() while line: mac, name = line[:-1].split(' ', 1) devices.append({ 'name' : name, 'mac' : mac}) line = process.stdout.readline() return devices def remove_device(self, (address,)): '''Method remove device from os''' subprocess.Popen(['bluez-test-device', 'remove', address], shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE) def send_file_obex(self, (address, filename)): '''Method send file using obex to selected address''' cli = obexftp.client(obexftp.BLUETOOTH) channel = obexftp.browsebt(address, obexftp.PUSH) cli.connect(address, channel) time.sleep(1) cli.put_file(filename) cli.disconnect() def connect_rfcomm_device(self, dev, port = 1): '''Method connecting with selected devices to specyfic port''' try: self.rfcomm_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) self.rfcomm_sock.connect((dev, port)) self.rfcomm_sock.settimeout(1) return self except bluetooth.btcommon.BluetoothError as ex: code = int(self.retrieve_exception_no(ex.message)) return code