Transfer files wirelessly over the Bluetooth Low Energy protocol Using Python

July 2, 2021
Transfer files wirelessly over the Bluetooth Low Energy protocol Using Python

This tutorial will show how to transfer files wirelessly over the Bluetooth Low Energy protocol. We will use two BleuIO dongles for this project—one for sending and another one for receiving. The sender dongle will be in central mode, while the receiver dongle will be in peripheral mode. 

We have already created two python scripts for sending and receiving. You can get the source code from 

https://github.com/smart-sensor-devices-ab/bleuio_file_transfer_example

You can try this project on two different computers.

A video tutorial will show you how to do it on one computer and send files between two BleuIO dongles.

Requirements :

Steps:

  • First, connect two BleuIO dongles to your computer. 
  • Note down the COM port for each dongle. You can find the port using device manager.
  • Try to find receiver dongle MAC id. To do that, start advertising your receiver dongle and note down the MAC id.  Follow the video if you need more details on how to do it.
  • Open file_recieve.py and file_transfer.py files. Update COM port and MAC id where necessary.

Here is the file_recieve.py

import serial
import time

your_com_port = "COM6"  # Change this to the com port your dongle is connected to.
file_name = "result.png"  # Change this to match the file type you are recieving

connecting_to_dongle = True
trying_to_connect = False

print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings
while connecting_to_dongle:
    try:
        console = serial.Serial(
            port=your_com_port,
            baudrate=57600,
            parity="N",
            stopbits=1,
            bytesize=8,
            timeout=0,
        )
        if console.is_open.__bool__():
            connecting_to_dongle = False
    except:
        print("Dongle not connected. Please reconnect Dongle.")
        time.sleep(5)

print("Connected to Dongle.")

file_data = ""
file_transfer_done = False
connected = "0"
line = ""
while 1 and console.is_open.__bool__() or not file_transfer_done:
    console.write(str.encode("AT+DUAL"))
    console.write("\r".encode())
    print("Putting dongle in Dual role.")
    time.sleep(0.1)
    console.write(str.encode("AT+ADVSTART"))
    console.write("\r".encode())
    time.sleep(0.1)
    print("Starting advertising and awaiting connection from other dongle...")
    while connected == "0":
        dongle_output2 = console.read(console.in_waiting)
        time.sleep(2)
        if not dongle_output2.isspace():
            if dongle_output2.decode().__contains__("\r\nCONNECTED."):
                connected = "1"
                print("Connected!")
            if dongle_output2.decode().__contains__("\r\nDISCONNECTED."):
                connected = "0"
                print("Disconnected!")
            dongle_output2 = " "
    while connected == "1" or not file_transfer_done:
        time.sleep(0.2)
        dongle_output3 = console.read(console.in_waiting)
        if not dongle_output3.isspace():
            if dongle_output3.__contains__(str.encode("\r\nDISCONNECTED.")):
                print("Disconnected!")
                connected = "0"
            elif dongle_output3.__contains__(str.encode("[DONE]")):
                file_transfer_done = True
                print("File transfer complete!\r\n")
                fo = open(file_name, "wb")
                hex = bytes.fromhex(file_data)
                fo.write(hex)
                fo.close()
                print("Exiting script...")
                exit()
            elif dongle_output3.__contains__(str.encode("[Received]:")):
                line = dongle_output3.decode()
                line = line.replace("\r", "")
                line = line.replace("\n", "")
                line = line.replace("[Received]:", "")
                line = line.replace(" ", "")
                file_data += line
                print(line)
            dongle_output3 = ""
        msg = ""

  • Set the file type that you are expecting to receive. For example, if you are expecting a jpg file, change file_name on file_recieve.py to result.jpg, and for a text file, you can rename it to result.txt

Here is the file_transfer.py

import serial
import time

target_dongle_mac_address = (
    "[0]40:48:FD:E5:2D:AF"  # Change this to the peripheral's mac address.
)
your_com_port = "COM25"  # Change this to the com port your dongle is connected to.

connecting_to_dongle = True
trying_to_connect = False
file_name = "test.txt"

print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings
while connecting_to_dongle:
    try:
        console = serial.Serial(
            port=your_com_port,
            baudrate=57600,
            parity="N",
            stopbits=1,
            bytesize=8,
            timeout=0,
        )
        if console.is_open.__bool__():
            connecting_to_dongle = False
    except:
        print("Dongle not connected. Please reconnect Dongle.")
        time.sleep(5)

print("Connected to Dongle.")

connected = "0"
while 1 and console.is_open.__bool__():
    console.write(str.encode("AT+DUAL"))
    console.write("\r".encode())
    time.sleep(0.1)
    print("Putting dongle in Dual role and trying to connect to other dongle.")
    while connected == "0":
        time.sleep(0.5)
        if not trying_to_connect:
            console.write(str.encode("AT+GAPCONNECT="))
            console.write(str.encode(target_dongle_mac_address))
            console.write("\r".encode())
            trying_to_connect = True
        dongle_output2 = console.read(console.in_waiting)
        time.sleep(2)
        print("Trying to connect to Peripheral...")
        if not dongle_output2.isspace():
            if dongle_output2.decode().__contains__("\r\nCONNECTED."):
                connected = "1"
                print("Connected!")
                time.sleep(10)
            if dongle_output2.decode().__contains__("\r\nDISCONNECTED."):
                connected = "0"
                print("Disconnected!")
                trying_to_connect = False
            dongle_output2 = " "
    while connected == "1":
        dongle_output3 = console.read(console.in_waiting)
        want_to_exit = input(
            "Press Enter to start sending file or 'exit' to quit script.\n\r>> "
        )
        want_to_exit = want_to_exit.upper()
        if "EXIT" in want_to_exit:
            print("Exiting script...")
            exit()
        hex_to_send = ""
        fo2 = open(file_name, "rb")
        print("Sending file. Please wait...")
        while (byte := fo2.read(1)) :
            hex_to_send += byte.hex().upper()
            if len(hex_to_send) >= 200:
                # my_dongle.at_spssend(hex_to_send)
                console.write(str.encode("AT+SPSSEND=" + hex_to_send + "\r"))
                time.sleep(0.2)
                hex_to_send = ""
        if len(hex_to_send) != 0:
            # my_dongle.at_spssend(hex_to_send)
            console.write(str.encode("AT+SPSSEND=" + hex_to_send + "\r"))
            time.sleep(0.2)
        fo2.close()
        console.write(str.encode("AT+SPSSEND=[DONE]\r"))
        print("File transfer complete!\r\n")
        print("Exiting script...")
        exit()
  • Keep the file in the same directory with file_transfer.py and change the file name accordingly.
  • Now run the script file_recieve.py using a terminal. It will start advertising and will be ready to receive.
  • Now run the file_transfer.py script. 
  • Once both the dongles are connected, you will be asked to send the file from the file transfer script. 
  • The receiver dongle will give you a confirmation once received, and you will find the file stored in the same folder with the receiver script.
  • Finally, you can check if you received the file correctly by running a checksum. 

Please follow the video if you have difficulty in understanding. 

Share this post on :
Order Now

BleuIO

$15.99

Buy Now

Get a FREE product for every 10 pcs ordered.