Raspberry Pi Bluetooth Data Transfer to the Smartphone
In addition to some wireless connections such as 433Mhz, infrared and NFC, Bluetooth is probably the most common and popular mode of data transmission between smartphones, which is why the Raspberry Pi Bluetooth data transfer is interesting. Meanwhile, pretty much all devices support Bluetooth and can not only be paired with it but of course, you can also transfer data, which is interesting for projects such as a Car PC.
This tutorial will show you how to exchange files between Raspberry Pi and a Bluetooth-enabled mobile phone/smartphone.
Required Hardware Parts
In this tutorial, I used the following hardware parts:
- Raspberry Pi Model (Raspbian OS)
- if neccessary: USB Bluetooth Adapter
- Bluetooth enabled mobile phone (Android, iOS, etc.)
The Bluetooth adapter can have a significant impact on the range. Cheap devices often do not recognize the connection after a few meters or obstacles, whereas higher-quality dongles also work up to a distance of 30 meters (outdoors).
Setting up the Software
First, some packages have to be installed:
sudo apt-get install bluez-utils libopenobex1 obexftp obexpushd --yes
After the USB Bluetooth dongle is connected, we check if it has been recognized correctly:
lsusb | grep Bluetooth
If a device has been detected, it will look like this:
Now we check if the Bluetooth service has already been started (it is automatically loaded every time you restart).
sudo service bluetooth status
sudo service bluetooth restart
For the device to be connected, it (smart phone) must be searched for. It starts a scan, which should find all devices in the immediate vicinity.
The mobile phone should be found directly. If not, wait a few seconds as the last result is cached. If it’s detected, it will look like this:
pi@raspberrypi ~ $ hcitool scan Scanning ... C4:42:02:41:B2:06 SM-G901F
This address of the device is important and will continuously be needed. In the following step, it must be changed according to your device address.
In order to connect the Pi and your mobile phone, this address must be specified (adjust it!):
sudo bluez-simple-agent hci0 C4:42:02:41:B2:06 yes
Then you will be asked for a code which you also have to enter on the mobile phone (for example 1234).
After that, the devices should be connected.
Send Files with the Raspberry Pi Bluetooth Module
Files can be sent on different channels. To find out which OBEX channel your smartphone uses, you can use the following command:
browse C4:42:02:41:B2:06 | egrep "Service Name:|Channel:"
Now you have to see which channel is under “OBEX Object Push”. For me, that is Channel 12.
To transfer a file (here
~/TEST.txt) you have to specify the following command (do not forget to change it):
obexftp --nopath --noconn --uuid none --bluetooth C4:42:02:41:B2:06 --channel 12 -p ~/TEST.txt
Subsequently, a request will pop up on the smartphone, which must be confirmed.
A number of other examples of obexftp can be found here.
Receive Files using Raspberry Pi Bluetooth
To receive files, we start an FTP server on the Raspberry Pi, which is waiting for files. With screen, the server can also be run in the background.
sudo obexpushd -B -n
Now you can select the file to be sent on the smartphone and send it to the connected Pi. Of course, depending on the size, it can take a bit. The file is created in the directory from which the server was started.
Finally, it should be said that the connected devices remain stored and no reconnecting is necessary, as long as both devices (Raspberry Pi Bluetooth Module and a terminal device such as a smartphone) are within reach of each other.