Introduction: How to Transfer Files by Bluetooth From a Raspberry Pi to a Windows Laptop
My objective was to set up a method to transfer files from a RaspberryPi (Rpi) to a Laptop by bluetooth, and vice-wersa. I am using the Pi to capture temperature data from a sensor and I want periodically to transfer the log file to the laptop where I can view it using Excel. To keep it simple, I will show how to transfer photographs but the idea is the same.
(PLEASE NOTE THAT I AM USING THIS METHOD WITHIN A CLOSED ENVIRONMENT. DO NOT USE THIS METHOD TO TRANSFER PERSONAL OR SENSITIVE DATA OR IN A PUBLIC.OPEN ENVIRONMENT)
I have a Lenovo laptop running running Windows 8.1. and to keep costs down, I am using a RPi Zero W v 1.1. which has Bluetooth installed. I installed the RPi as headless, using the image 2017-07-05-raspbian-jessie-lite - downloaded from https://www.raspberrypi.org/downloads/raspbian/
Something to say before we start. When you're setting up bluetooth, firstly,you have to configure and set up the connections with other devices. Secondly, when this is done, you can use applications such as obexftp for file transfer.
Here are the steps in summary
1.1. Fix the bluetooth service issue
1.2. Make the RPi discoverable using Bluetoothctl (RPI Bluetooth Agent)
1.3. Install OpenOBEX
2.1. Set up a shared Bluetooth folder
2.2. Discover the Rpi and pair it with the laptop
3. Set up the file transfer system
4. Some applications and scripts to automate this process
Step 1: 1.1. Fix the Bluetooth Service Issue
I logged on to the Pi using Putty and accessed the command line.
Check the current status of Pi Bluetooth Service from the command line prompt typesudo service bluetooth status
This will display the following:
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
Active: active (running) since Tue 2017-08-01 16:28:15 UTC; 4s ago
Docs: man:bluetoothd(8) Main PID: 16899 (bluetoothd) Status: "Running" CGroup: /system.slice/bluetooth.service
Aug 01 16:28:14 raspberrypi bluetoothd: Bluetooth daemon 5.23
Aug 01 16:28:14 raspberrypi bluetoothd: Starting SDP server
Aug 01 16:28:14 raspberrypi bluetoothd: Bluetooth management interface 1.14 initialized
Aug 01 16:28:14 raspberrypi bluetoothd: Sap driver initialization failed.
Aug 01 16:28:14 raspberrypi bluetoothd: sap-server: Operation not permitted (1)
Aug 01 16:28:15 raspberrypi systemd: Started Bluetooth service.
To remove this sap error, change the bluetooth service to ignore Sap by using nano or another editor to access the service file
(note if you can't find this file, type sudo find / -name "bluetooth.service")
sudo nano /etc/systemd/system/bluetooth.target.wants/bluetooth.service
Save the file and execute the following commands
sudo systemctl daemon-reload
sudo service bluetooth restart
sudo service bluetooth status
The Sap error should now be removed
Step 2: 1.2. Make the RPi Discoverable Using Bluetoothctl (Bluetooth Agent)
This the first of our two RPi putty sessions, is the RPI Bluetooth Agent window (bluetoothctl),
Using Putty, log onto the command line of the Rpi then type
sudo bluetoothctl (This opens a new shell)
When I do this it displays the visible Bluetooth devices, e.g.
[NEW] Controller B8:27:EB:C9:7D:BB raspberrypi [default]
[NEW] Device AC:B5:7D:E2:52:CD LENOVO-PC
[NEW] Device 60:E7:01:05:2B:38 Kestrel
I am interested in the address of my laptop, AC:B5:7D:E2:52:CD
Displays Changing power on succeeded
Type agent on
Displays Agent registered
Displays Default agent request successful
Type discoverable on
Displays Changing discoverable on succeeded
[CHG] Controller B8:27:EB:C9:7D:BB Discoverable: yes
[CHG] Device AC:B5:7D:E2:52:CD Connected: yes
Keep this putty session open
Step 3: 1.3. Install OpenOBEX
Open a second RPi putty session, and install the software we need to transfer files via bluetooth FTP
Typeapt-get install obexftp bluetooth
Click y to accept any prompts
Step 4: 2.1. Set Up a Shared Bluetooth Folder
Turn Bluetooth on on your laptop-
To see if blue tooth is activated on the laptop - swipe the screen to reveal the settings link, then select change PC settings, then click on PC settings. You will see the button to turn Bluetooth on.
Next, go back to the desktop and click the little arrow on the bottom right toolbar to reveal the bluetooth icon. Click on this and select open settings. Select the "Share" tab and you can see the Bluetooth Exchange folder location and then on the click the "Allow remote devices.... " box.
Go to the Options tab and check the box that allow other Bluetooth device to find or discover this computer. When pairing is complete, remember to go to your Bluetooth Device’ Options tab and uncheck the box that allow other Bluetooth device to find or discover this computer. That’s means only your computer can discover other Bluetooth device, so that can avoid your computer from discovered by other unknown Bluetooth device.
Step 5: 2.2. Discover the RpI and Pair With It From the Laptop
Again, on the laptop, swipe the screen to reveal the settings link, then select change PC settings, then click on PC settings. You will see the list of available bluetooth devices. Remember that for the RPi to be visible you will have to make it discoverable from the RPi putty screen (bluetoothctl) and similarly, on the Bluetooth Device options tab on the laptop.
Still on the laptop, next click on the RPi pair button and then go to the RPI putty session (bluetoothctl) and you will asked to confirm the passkey (Confirm passkey xxxxxx (yes/no): yes) Remember to to type yes -
Then flick back to the Laptop bluetooth screen and likewise, click to select. It should tell you that the RPi is connected to the laptop.
(Note, that from a security perspective, the passkey should not a default, eg 1234, and if it is, don't accept it. Also, ensure that both the RPI and Laptop are no longer discoverable)
Step 6: 3. Set Up the File Transfer System Between the RPi and Laptop
Open the second RPi putty session.
Remember that you should still have two RPi putty sessions running 1) for Bluetoothctl and 2) a general one
On the 2nd session, typesudo sdptool browse XXXXXXXXXXX - the address of the laptop (e,g, AC:B5:7D:E2:52:CD in my case). Remember, that you will find the laptop address in the 1st putty session which is running Bluetoothctl.
This displays a list of available bluetooth services. Importantly, you should see something like this which makes a reference to ObexFTP,
Service Name: FTP Server
Service RecHandle: 0x10015 Service Class ID List: "OBEX File Transfer" (0x1106) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 4 "OBEX" (0x0008) Language Base Attr List: code_ISO639: 0x656e encoding: 0x6a base_offset: 0x100 Profile Descriptor List: "OBEX File Transfer" (0x1106) Version: 0x0100
(NOTE - OTHER BLUETOOTH SERVICES INCLUDE AUDIO AND CAN BE ACCESSED USED APPLICATIONS OTHER THAN OBEXFTP. ALSO, SOME LOW SPEC SMART/CELLPHONES DON'T HAVE OBEX FILE TRANSFER PROGRAMS)
We are now able to transfer files to and from the bluetooth device and the laptop
From the same (2nd )putty terminal, firstly get the tree of directories from the laptop.
type sudo obexftp -b XXXXXXXX (laptop address) -c / -l
Here is an example from my laptop
sudo obexftp -b AC:B5:7D:E2:52:CD -c / -l
the output will show folders and files in the laptop Bluetooth exchange shared folder. These are shown in the image above. Importantly, it gives us the file names. We can now download a file from the laptop -
e.g. typesudo obexftp -b AC:B5:7D:E2:52:CD -c / -g IMG_20170731_111503.jpg
Obviously, replace AC:B5:7D:E2:52:CD with the address of your laptop
typels-l to verify that you have downloaded this.
To upload, use the following syntax - obexftp -b AC:B5:7D:E2:52:CD -c / -p file.jpg
Obviously, replace AC:B5:7D:E2:52:CD with the address of your laptop
Step 7: 4. Some Applications and Scripts to Automate This Process
Obviously, the bluetooth FTP process is quite intensive and it can be automated with bash scripts. The problem of course is how do you script the bluetoothctl shell commands. Here is an example of how to do this:
bluetoothctl << EOF
As well as transferring sensor logs for analysis on Excel etc, there are other applications. These include capturing system and security information from your RPi, e.g. disk usage, log size etc , particularly if your RPi is connected to external storage devices.
You may also want to grab images from your web site using wget or curl using crontab and transfer it to your laptop.
I hope you found this of some benefit and that it helps you get a grasp of bluetooth technology.