Search by Tags

Bluetooth (Linux)

 
Applicable for

Compare with Revision


Subscribe for this article updates

This article describes how to use Bluetooth in Embedded Linux. For modules that don't come with Bluetooth hardware on-module, support can easily be added by using a USB Bluetooth module. The table below presents the modules with integrated Bluetooth:

Family Module Version Bluetooth adapter Description
Colibri iMX6ULL 512MB Wi-Fi / Bluetooth Wi2Wi WM828CC6 Bluetooth 4.2/BLE (Dual-Mode)

Custom Kernel/Backports

BSP 2.7b4 and newer

Since the BSP 2.7b4, Bluetooth support is added to the Toradex pre-built image through the kernel driver backports. Please see the article below for instructions about how to cross-compile the driver backports:

Kernel Driver Backports Integration

BSP 2.7b3 and older

Bluetooth support was either compiled into the kernel, or not shipped at all. Please see the details below:

Bluetooth kernel support

Custom Image

BlueZ 5 is already shipped in the Toradex pre-built image.

Additional user space utilities can be added to a custom Linux image. For this an OpenEmbedded build is required. Please refer the following article for setup of the same:

OpenEmbedded (core)

An example is provided with some useful packages. Add the following to "~/oe-core/build/conf/local.conf":

local.conf
IMAGE_INSTALL_append = " obexftp obex-data-server obex-client python-dbus python-pygobject python-pybluez"

Build the image for the module to be used and flash the image on the module.

Configuring the Device

A USB Bluetooth module should be detected after connecting it to the USB port (if using a Bluetooth USB dongle):

root@colibri-vf:~# lsusb
Bus 001 Device 005: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

There is a systemd Bluetooth service already enabled. Start it:

root@colibri-vf:~# systemctl start bluetooth.service

Enable the Bluetooth device using ConnMan.

root@colibri-imx6ull:~# connmanctl enable bluetooth
Enabled bluetooth

hciconfig should show the connected bluetooth devices up and running. Other information such as the MAC address and device bus are also provided.

For a USB dongle:

root@colibri-vf:~# hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 00:1A:7D:DA:71:13 ACL MTU: 310:10 SCO MTU: 64:8
UP RUNNING
RX bytes:574 acl:0 sco:0 events:30 errors:0
TX bytes:368 acl:0 sco:0 commands:30 errors:0

For an SDIO module:

root@colibri-imx6ull:~# hciconfig
hci0: Type: Primary Bus: SDIO
BD Address: 00:19:88:5E:10:B1 ACL MTU: 1021:7 SCO MTU: 120:6
UP RUNNING
RX bytes:1882 acl:0 sco:0 events:83 errors:0
TX bytes:2075 acl:0 sco:0 commands:83 errors:0

See below instructions for BSP release 2.7b3 and older:

Legacy instructions

Bluetoothctl

Starting with BSP version 2.7, bluetoothctl was added to the Linux image. It enables scanning, pairing and connecting to devices, among other features.

Start bluetoothctl:

root@colibri-imx6ull:~# bluetoothctl
[NEW] Controller 00:19:88:5E:10:B1 colibri-imx6ull [default]
Agent registered

List all available options:

[bluetooth]# help
Available commands:
list List available controllers
show [ctrl] Controller information
...

Scan for Bluetooth devices:

[bluetooth]# scan on

After you enable scanning, it will begin to list available devices:

Discovery started
[CHG] Controller 00:19:88:5E:10:B1 Discovering: yes
[NEW] Device 98:39:8E:1B:D8:88 Galaxy A5 (2016)
[CHG] Device 98:39:8E:1B:D8:88 RSSI: -86

The device number listed is the device MAC address. In the example above its value is 98:39:8E:1B:D8:88. You can stop scanning once you find the device you are looking for:

[bluetooth]# scan off

Pair to the device using its MAC address:

[bluetooth]# pair 98:39:8E:1B:D8:88
Attempting to pair with 98:39:8E:1B:D8:88
[CHG] Device 98:39:8E:1B:D8:88 Connected: yes
Request confirmation
[agent] Confirm passkey 117022 (yes/no):

Confirm that the passkey is the same as displayed on your device:

[agent] Confirm passkey 117022 (yes/no): yes
[CHG] Device 98:39:8E:1B:D8:88 Modalias: bluetooth:v0075p0100d0200
[CHG] Device 98:39:8E:1B:D8:88 UUIDs: 00001105-0000-1000-8000-00805f9b34fb
...
[CHG] Device 98:39:8E:1B:D8:88 ServicesResolved: yes
[CHG] Device 98:39:8E:1B:D8:88 Paired: yes
Pairing successful
...
[CHG] Device 98:39:8E:1B:D8:88 Connected: no

Trust paired device:

[bluetooth]# trust 98:39:8E:1B:D8:88
[CHG] Device 98:39:8E:1B:D8:88 Trusted: yes
Changing 98:39:8E:1B:D8:88 trust succeeded

Connect to the Bluetooth device:

[bluetooth]# connect 98:39:8E:1B:D8:88
Attempting to connect to 98:39:8E:1B:D8:88
[CHG] Device 98:39:8E:1B:D8:88 Connected: yes
Connection successful
[CHG] Device 98:39:8E:1B:D8:88 ServicesResolved: yes
[Galaxy A5 (2016)]#

List the connected device information, including supported services:

[Galaxy A5 (2016)]# info 98:39:8E:1B:D8:88
Device 98:39:8E:1B:D8:88
Name: Galaxy A5 (2016)
Alias: Galaxy A5 (2016)
Class: 0x5a020c
Icon: phone
Paired: yes
Trusted: yes
Blocked: no
Connected: yes
LegacyPairing: no
UUID: OBEX Object Push (00001105-0000-1000-8000-00805f9b34fb)
UUID: Audio Source (0000110a-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Headset AG (00001112-0000-1000-8000-00805f9b34fb)
UUID: PANU (00001115-0000-1000-8000-00805f9b34fb)
UUID: NAP (00001116-0000-1000-8000-00805f9b34fb)
UUID: Handsfree Audio Gateway (0000111f-0000-1000-8000-00805f9b34fb)
UUID: Phonebook Access Server (0000112f-0000-1000-8000-00805f9b34fb)
UUID: Message Access Server (00001132-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific (936da01f-9abd-4d9d-80c7-02af85c822a8)
Modalias: bluetooth:v0075p0100d0200
ManufacturerData Key: 0x0075
ManufacturerData Value: 0x77
ManufacturerData Value: 0x98
ManufacturerData Value: 0x39
ManufacturerData Value: 0x8e
ManufacturerData Value: 0x1b
ManufacturerData Value: 0xd8
ManufacturerData Value: 0x88

PyBluez

PyBluez is a Python module that allows access to system Bluetooth resources. See how to add it to a custom Linux image in the section Custom Image above. Check the project GitHub page for documentation.

If you would like to test the samples provided by PyBluez, clone the git repository to the board:

opkg update
opkg install git
git clone https://github.com/karulis/pybluez.git

Then you can run the samples using the Python interpreter:

python pybluez/examples/<sample-folder>/<sample-name>.py
Running PyBluez samples

File Transfer

You must pair to another device before transferring files. Please see the Bluetoothctl section above for an example on how to pair.

Alternatively, a Python sample for pairing is provided below. Currently it only works with BSP version 2.5 and bluez4.

Python sample for Bluetooth pairing

On an Android Lollipop device install an application like Bluetooth File Transfer which provides the obexftp service. Enable Bluetooth and open the Bluetooth File Transfer application for enabling obexftp service on the Android device.
For some Android devices running Icecream Sandwich, the obexftp service might be available by default which could be enabled from the Bluetooth settings or elsewhere depending on the device.

From the module a file can be transferred as follows.

root@colibri-vf:~# obexftp -b 14:DD:A9:36:DD:FC -p file.txt
Browsing 14:DD:A9:36:DD:FC ...
Connecting..\done
Tried to connect for 20ms
Sending "file.txt"...|done
Disconnecting../done

The transferred file is now available on the device and is shown by the Bluetooth File Transfer application.

Notes

The python packages python-dbus, python-pygobject and python itself are added for using D-Bus with Python in the simple-agent python script which is used for pairing. On a device with limited NAND flash like the Colibri VF50 it is possible that the generated image with these packages included, might not fit in the flash memory. It should be possible to do what the simple-agent python script does using the low-level D-Bus C API.