def __init__(self, address, port, file_path, file_name): threading.Thread.__init__(self) self.address = address self.port = port self.client = Client(self.address, self.port) self.file_path = file_path self.file_name = file_name
def RFCOMM_Sendfile(address, fname="test.txt", fdata=b'Hello world\n'): print("Searching for OBEX service on %s" % address) # Use the server named "OBEX Object Push" to send file service_matches = bluetooth.find_service(name="OBEX Object Push", address=address) if len(service_matches) == 0: print("Couldn't find the service") sys.exit(0) match = service_matches[0] name = match["name"] host = match["host"] port = match["port"] print("Service Name: %s" % match["name"]) print(" Host: %s" % match["host"]) print(" Description: %s" % match["description"]) print(" Provided By: %s" % match["provider"]) print(" Protocol: %s" % match["protocol"]) print(" channel/PSM: %s" % match["port"]) print(" svc classes: %s " % match["service-classes"]) print(" profiles: %s " % match["profiles"]) print(" service id: %s " % match["service-id"]) print("Connecting to \"%s\" on %s" % (name, host)) client = Client(host, port) client.connect() # The parameter [file_data] must be bytes type like [b'****'],otherwise there will be some error occur in Python3 client.put(name=fname, file_data=fdata) client.disconnect() return
def sendFile(name, host, port): print("Connecting to \"%s\" on %s" % (name, host)) client = Client(host, port) client.connect() file_to_send = read_file() client.put("fileToSend.txt", file_to_send) client.disconnect()
def send_file(self, filename: str, device: str, port: int = None, data=None, service_name='OBEX Object Push', binary: bool = False): """ Send a local file to a device that exposes an OBEX Object Push service :param filename: Path of the file to be sent :param data: Alternatively to a file on disk you can send raw (string or binary) content :param device: Device address or name :param port: Port number :param service_name: Service name :param binary: Set to true if data is a base64-encoded binary string """ from PyOBEX.client import Client if not data: filename = os.path.abspath(os.path.expanduser(filename)) with open(filename, 'r') as f: data = f.read() filename = os.path.basename(filename) else: if binary: data = base64.decodebytes(data.encode() if isinstance(data, str) else data) addr, port, protocol = self._get_addr_port_protocol(device=device, port=port, service_name=service_name) client = Client(addr, port) self.logger.info('Connecting to device {}'.format(addr)) client.connect() self.logger.info('Sending file {} to device {}'.format(filename, addr)) client.put(filename, data) self.logger.info('File {} sent to device {}'.format(filename, addr)) client.disconnect()
def send_text_file(bd_addr, port, filename): res = bytes("my name is Safwan Mansuri", 'utf-8') client = Client(bd_addr, port) client.connect() print('\n') print("Waiting for acceptance......") client.put(filename, res) client.disconnect()
def send_music(bd_addr, port, filename): Image = open(filename, "rb") data = Image.read() client = Client(bd_addr, port) client.connect() print('\n') print("Waiting for acceptance......") client.put(filename, data) client.disconnect()
def blue_send(device, files=[]): service_matches = find_service(name=b'OBEX Object Push\x00', address=device) first_match = service_matches[0] port = first_match["port"] name = first_match["name"] host = first_match["host"] client = Client(host, port) client.connect() for file in files: file_content = open(file, 'rb').readlines() if type(file_content) == list: file_content = b''.join(file_content).strip() client.put('Output\\' + file, file_content) client.disconnect()
def connect(self): self.refresh() print('Sr.\tAddress \t\tName') for i in range(len(self.nearby_devices)): print( f'{i + 1}.\t{self.nearby_devices[i][0]}\t{self.nearby_devices[i][1]}' ) addr = self.nearby_devices[int( input( '\nEnter the number of the device, to which you wish to connect : ' )) - 1][0] service = find_service(name=b'OBEX Object Push\x00', address=addr)[0] logging.info( f'Connecting to {service["host"]} on port {service["port"]}...') self.client = Client(service["host"], service["port"]) self.client.connect() logging.info('Connection success.')
def bluetooth(target_name, file_name): file_contents = read_file(file_name) if file_contents == "": return 0 target_address = None nearby_devices = bt.discover_devices() for bdaddr in nearby_devices: if target_name == bt.lookup_name(bdaddr): target_address = bdaddr break if target_address is not None: print("Found device with MAC Address: " + target_address) else: print("Couldn't find device") return 0 services = bt.find_service(address=target_address, name=b'OBEX Object Push\x00') if len(services) == 0: print("Couldn't find service") return 0 first_match = services[0] port = first_match["port"] name = first_match["name"] host = first_match["host"] client = Client(host, port) client.connect() client.put(file_name, file_contents) client.disconnect() return 1
from bluetooth import * from PyOBEX.client import Client import sys addr = sys.argv[1] print("Searching for OBEX service on %s" % addr) service_matches = find_service(name=b'OBEX Object Push\x00', address=addr) if len(service_matches) == 0: print("Couldn't find the service.") sys.exit(0) first_match = service_matches[0] port = first_match["port"] name = first_match["name"] host = first_match["host"] print("Connecting to \"%s\" on %s" % (name, host)) client = Client(host, port) client.connect() client.put("test.txt", "Hello world\n") client.disconnect()
import bluetooth, os, sys from xml.etree import ElementTree from PyOBEX import client, responses from PyOBEX.client import Client if __name__ == "__main__": device_addr = sys.argv[1] path = sys.argv[2] services = bluetooth.find_service(uuid="0000112f", address=device_addr) if services: port = services[0]["port"] names = services[0]["name"] print("Connecting to port %s with %s" % (port, names)) c = Client(device_addr, port) response = c.connect() pieces = path.split("/") for piece in pieces: print(piece) response = c.setpath(piece) sys.stdout.write("Enteres directory %s \n" % path) response = c.listdir() if isinstance(responce, responses.FailureResponse): sys.stderr.write('Failed to enter directory\n') sys.exit(1) headers, data = response tree = ElementTree.formstring(data) for element in tree.findall("file"): name = element.attrib["name"]
from bluetooth import * from PyOBEX.client import Client import sys addr = '48:9D:24:CA:16:86' port = 3 #print("Searching for OBEX service on %s" % addr) #service_matches = find_service(name=b'OBEX Object Push\x00', address = addr ) #if len(service_matches) == 0: # print("Couldn't find the service.") # sys.exit(0) #first_match = service_matches[0] #port = first_match["port"] #name = first_match["name"] #host = first_match["host"] #print("Connecting to \"%s\" on %s" % (name, host)) client = Client(addr, port) client.connect() client.put("/home/pr0xy/export/pyscripts/test-bt.py", "Hello world\n") client.disconnect()
if len(service_matches) == 0: print "Couldn't find any appropriate services" sys.exit(1) # list out all matched services # let user select from the list for i in range(0, len(service_matches)): print"{}) {}".format(i, service_matches[i]) service_match_index = int(raw_input("Select a service: ")) service = service_matches[service_match_index] port = service["port"] server_address = service["host"] name = service["name"] # connect to server print "Connecting to {} ({})".format(name, server_address) client = Client(server_address, port) client.connect() # send some file print "Sending a file..." f = open("c:/users/vpxbo/desktop/test_image.jpg", 'rb') client.put("test.jpg", f.read()) # disconnect from server print "Disconnecting..." client.disconnect() print "Done"