0

I need to transfer a file from my Windows laptop to a Raspberry Pi, and also measure the speed and transfer time of the file. Looking on the internet I chose to use Pybluez, but I am sure there is something missing to realise the transfer to a Raspberry Pi and I don't understand what. I should probably figure out how to merge the pieces of code.This is the inquiry code:

import bluetooth

print("performing inquiry...")

nearby_devices = bluetooth.discover_devices(
        duration=8, lookup_names=True, flush_cache=True, lookup_class=False)

print("found %d devices" % len(nearby_devices))

for addr, name in nearby_devices:
    try:
        print("  %s - %s" % (addr, name))
    except UnicodeEncodeError:
        print("  %s - %s" % (addr, name.encode('utf-8', 'replace')))

This is the code for the server (i.e. the device that has to send the file, in my case it's my laptop, right?):

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

advertise_service( server_sock, "SampleServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
#                   protocols = [ OBEX_UUID ] 
                    )
                   
print("Waiting for connection on RFCOMM channel %d" % port)

client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print("received [%s]" % data)
except IOError:
    pass

print("disconnected")

client_sock.close()
server_sock.close()
print("all done")

This is the code for the client (which in my case would then be the Raspberry Pi):

from bluetooth import *
import sys

if sys.version < '3':
    input = raw_input

addr = None

if len(sys.argv) < 2:
    print("no device specified.  Searching all nearby bluetooth devices for")
    print("the SampleServer service")
else:
    addr = sys.argv[1]
    print("Searching for SampleServer on %s" % addr)

# search for the SampleServer service
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
service_matches = find_service( uuid = uuid, address = addr )

if len(service_matches) == 0:
    print("couldn't find the SampleServer 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))

# Create the client socket
sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))

print("connected.  type stuff")
while True:
    data = input()
    if len(data) == 0: break
    sock.send(data)

sock.close()

What should i do? does the client-server code work directly by running the client programme on the Raspberry and the server on the computer? How can i then transfer the file? Is this a correct method or not? Help me please!

  • The common way to transfer files over Bluetooth is OBEX. It has 2 profiles for objects (files) transfering: Object Push Profile and File Transfer Profile. Use one that is supported by Raspbian, usually it is OPP by default. – Mike Petrichenko Oct 09 '22 at 14:27
  • Does this answer help? https://stackoverflow.com/a/68552026/7721752 – ukBaz Oct 09 '22 at 14:34

0 Answers0