5

I am trying to send a series of bytes from the Raspberry Pi zero W to the Raspberry pi 3 via bluetooth. Im using the PyBluez python package.

I believe that Raspberry Pi Zero has Bluetooth 4.1 and Raspberry Pi 3s also has Bluetooth 4.1 which should result in transfer speeds in the Mbps, but I can only manage to get speeds around 40kbps using this script I wrote.

After the devices are paired and connected, which Ive done using bluetoothctl, I run these scripts on each of the pis. The script transfers 8000 bytes of data, from the zero to the three then sends it back again to the zero to confirm.

Im using RFCOMM to communicate between them, which I believe only works with bluetooth classic and not bluetooth low energy so I should be seeing these high speeds in the Mega bytes rather than kilo bytes /s.

Does anyone else have this problem with slower than expected bluetooth transfer speeds between pis?

Thanks! You can find the following code on my github https://github.com/mizunt1/bluetooth_tests

client_script.py:

import time
import sys
from bluetooth import *

def setup_bluetooth():
    if sys.version < '3':
        input = raw_input
    addr = None
    len_array = 0

    if len(sys.argv) < 2:
        print("no device specified.  Searching all nearby bluetooth devices for")
        print("the Rpi_zero_server service")
    else:
        addr = sys.argv[1]
        print("Searching for Rpi_zero_server on %s" % addr)
    uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
    service_matches = find_service( uuid = uuid, address = addr )
    if len(service_matches) == 0:
        print("couldn't find the Rpi_zero_server 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")
    return sock

sock = setup_bluetooth()

byte_to_send = bytes([i//250 for i in range(8000)])
print("size of byte sent")
print(len(byte_to_send))
bytearray_returned = bytearray(len(byte_to_send))
start_time = time.time()
sock.send(byte_to_send)
get_data = True
# must change this to allocated chunks                                                                                                                                                                      
i = 0
while get_data:
    data = sock.recv(1024)
    bytearray_returned[i*1008:(i*1008) + len(data)] = data
    i += 1
    if (((i-1)*1008) + len(data)) == len(byte_to_send):
        print("data returned")
        get_data = False

end_time = time.time()
print("data returned uncorrupted?")
print(byte_to_send == bytearray_returned)
total_time = end_time - start_time
print("total time taken for data to return")
print(total_time)
speed = len(byte_to_send)/ total_time
print("speed")
print(speed/1e3)
print("kbytes/s")
sock.close()

server_script.py

import time
from bluetooth import *

def advertise_server(name_server):
    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, name_server,
                   service_id = uuid)
    client_sock, client_info = server_sock.accept()
    print("Accepted connection from ", client_info)
    return client_sock, server_sock

client_sock, server_sock = advertise_server("Rpi_zero_server")
a = bytearray(8000)
get_data = True
i = 0
while get_data:
    data=client_sock.recv(1024)
    a[i*1008:(i*1008) + len(data)] = data
    i += 1
    if (((i-1)*1008) + len(data)) == 8000:
        client_sock.send(bytes(a))
        print("sent")
        get_data = False
print("recieved data")
client_sock.close()
server_sock.close()
0

Problems are often occuring when using both bluetooth and wifi at the same time with both rpi 3 and zero.

In order to solve problem concerning the bluetooth (used for me to share audio content), I've found two solutions :

  • disabling wifi when bluetooth is used (which can disconnect you from the pi if you use ssh)

  • using a specific bluetooth adapter instead of the rpi bluit-in one.

With both of these solutions, I've been able to stream audio correctly (meaning with a correct transfer rate).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.