Raspberry pi 3 bluetooth connects to Android mobile phone device: technologies and errors

Write before the post

There have been over tens of thousands of devices that are equipped with bluetooth and are capable of connecting to other devices such as mobile phones. Also, there are numerous tutorials and instructions around the internet telling people how to setup all kinds of bluetooth devices and configure the connection. However, an interesting thing is, if searching bluetooth connection in google, there are far more questions and errors than solutions and successful project. So I am motivated to write down my experience of setting up bluetooth connection between a raspberry pi 3 model B and a Marshmallow Android mobile phone.


1. A brief background
The project background was to setup bluetooth connection to send sensor data to mobile phone, so the phone can visualise and responds to the sensing results. The sensor was connected to RPi through I2C interface, RPi 3 had necessary pins of SDA, SCL, 3V3, and GPIOs. Because this is not the key part of the bluetooth connection, I just make a quick explanation here.

By the time of writing the post, there are three general bluetooth versions that are available for remote connection, including the conventional bluetooth 2, bluetooth low energy (BLE) and bluetooth smart. In the following project, the conventional bluetooth and the BLE were explored, but only the former was successfully implemented.

2. Hardware setting up

As mentioned earlier, hardwares of the project were quite straightforward:

RPi —–> bluetooth adapter —–> Android mobile phone

The RPi had a board integrated bluetooth adapter in UART interface, it was running os system Raspbien and installed with bluez, pi-bluetooth, pybluez and other necessary libs.

RPi official website says that the integrated bluetooth adapter uses mini UART interface, which affects the GPIO pins to connect additional bluetooth adapters through UART interfaces, and so, the board integrated bluetooth adapter needs to be disabled before using new UART bluetooth adapters such like HM-10. However, USB bluetooth adapters does not need such modification, as  these use separate USB interface.

3. Coding and debugging

Coding of bridging the rpi and mobile phone through bluetooth is the core part, also the hardest part. The basic coding environment was configured as this:

RPi installs Rapsbien Jessen, released on 27th MAY 2016.

bluez 5.23 is updated through

sudo apt-get update && sudo apt-get install bluez

Some tutorials say bluez needs to be updated to the latest version, which is 5.40 so far, but that seems unnecessary, 5.23 coming with rpi 2 actually works well. Also, bluez 5.40 was compiled and installed, but did not work in this project.
The necessary libs were updated and installed

sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev make

In addition, bluetooth lib in python ‘pybluez‘ was installed. This lib was used in RPi side in order to manage bluetooth relevant operations. A number of examples are provided in the git of pybluez, should these be referred to before coding.

The RPi played bluetooth server, which started sending sensor data once after successfully connected to the Android device. On the other side, the Android device kept listening to incoming connections, and once the connection was set it launched a new thread to read input byte streams.

Below is the codes on the RPi side:

import bluetooth
import sys
import time
if sys.version < ‘3’: input = raw_input address = “xx:xx:xx:xx:xx:xx” channel = 6 socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) socket .connect((address, channel)) print(“connected to server OK.\n %s” % st) while True: data = “default msg” # data from input() socket .send(data) print(“–> sent data [%s]” % data)
time.sleep(1)socket .close()

Below is pseudo codes on the Android device side:
– check if there is any existing bluetooth connection, if yes, cancel it;
– setup bluetooth adaptor of android device, and check if it is enabled, if not, enable it;
– get paired bluetooth devices, and make sure the target bluetooth device is one of them, if not found any paired devices, then prompt for pairing first;
– pass the bluetooth remote device as a parameter to a new thread, the thread sets up a BluetoothServerSocket with

mServerSocket = btAdapter.listenUsingInsecureRfcommWithServiceRecord(“raspberrypi”, UUID);

In this case, the UUID is


Available UUIDs of the remote device can be extracted like this:

ParcelUuid[] pUUIDs = mbtDevice.getUuids();
for(ParcelUuid puid : pUUIDs) {
debug(“\n03-*** ” + puid.getUuid().toString());

– cancel any bluetooth device discovery service with


– let BluetoothServerSocket to wait and accept incoming connection:

mbtSocket = mServerSocket.accept();

– check the results of accept(), if no exception and the connection is successful, start a new thread to read input streaming.

Several points that are worth mentioning:
1. according to RPi 3 model B instructions, it is supposed to support bluetooth low energy, whereas looking at its services and UUIDs through SDP tools does not show working services with available UUIDs. In other words, on the side of Android device, BLE services are all available, but on the RPi side, there are no good services with UUIDs for BLE programming. Here not saying that RPi 3 model B is not available for BLE, but it might be some programming issues that were not well addressed in the project. So any suggestions at this point are more than welcomed.

2. this project used RFCOMM, which was a good way to transmit streaming data such as continuous sensor results. Need to mention is that in RFCOMM connection, besides the MAC address of bluetooth devices, the channel is also very important. Bluetooth MAC address is easy to get, but available channels are not, which need sdp tools to check which channels are used for RFCOMM transmission. And usually there are a number of channels available in one bluetooth device, so here also needs to try out which really works for the project.

4. Final prototype

Author: Andol Li

A HCI researcher, a digital media lecturer, an information product designer, and a python/php/java coder.

Leave a comment
Due to technical adjustments, the comment function is shortly closed and will be re-openning soon. Thanks.

Copyrights 2006-2018 © All rights reserved
Theme Tree2, re-designed by Andol Li, powered by WordPress and Bootsrap
WWW.ANDOL.ME | 浙ICP备15040508号-1
公安备案图标 浙公网安备33010602004018号
Back to top