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:
Toradex SoMs featuring the Azurewave AW-CM276NF Wi-Fi / Bluetooth module:
|System on Module||Product Number||Version|
|Apalis iMX8 QuadMax 4GB Wi-Fi / Bluetooth IT||0037||V1.0A and newer|
|Apalis iMX8 QuadPlus 2GB Wi-Fi / Bluetooth||0048||V1.0A and newer|
|Colibri iMX8X QuadXPlus 2GB Wi-Fi / Bluetooth IT||0038||V1.0A and newer|
|Colibri iMX8X DualX 1GB Wi-Fi / Bluetooth||0051||V1.0A and newer|
|Colibri iMX6ULL 512MB Wi-Fi / Bluetooth IT||0040||V1.1A and newer|
|Colibri iMX6ULL 512MB Wi-Fi / Bluetooth||0045||V1.1A and newer|
|Verdin iMX8M Mini Quad 2GB Wi-Fi / Bluetooth IT||0055||V1.0B and newer|
|Verdin iMX8M Mini DualLite 1GB Wi-Fi / Bluetooth IT||0060||V1.1A and newer|
|Verdin iMX8M Plus Quad 4GB Wi-Fi / Bluetooth IT||0058||V1.0B and newer|
Toradex SoMs featuring the Wi2Wi WM828CC6 Wi-Fi / Bluetooth module:
Note: Wi2Wi WM828CC6 has been replaced by Azurewave AW-CM276NF on newer Toradex SoMs.
|System on Module||Version|
|Colibri iMX6ULL 512MB Wi-Fi / Bluetooth||V1.0x|
Wi-Fi adapter characteristics summary:
|Wi-fi Adapter||Wi-Fi Standards||Bluetooth version||Adapter's Chipset||Antenna Configuration|
|AzureWave AW-CM276NF||802.11 a/b/g/n/ac||5.0 compliant with 2.1||NXP 88W8997||Dual-Band 802.11ac 2x2 MU-MIMO|
|Wi2Wi WM828CC6||802.11 a/b/g/n/ac||4.2||NXP 88W8887||Dual-Band 802.11ac 1x1 spatial stream|
Since the BSP 2.7b4, Bluetooth support is added to the Toradex pre-built image through the kernel driver backports with BlueZ 5 bluetooth software stack. Please see the article below for instructions about how to cross-compile the driver backports:
BlueZ 5 is already shipped in the pre-built Reference Images for Yocto Project and is the software stack used for Bluetooth interaction.
Additional user space utilities can be added to a custom Linux image. For this, a Yocto Project/OpenEmbedded build is required. Please refer to the following articles:
An example is provided with some useful packages. Add the following to your
IMAGE_INSTALL_append = " obexftp obex-data-server python3 python3-dbus python3-pygobject python3-pybluez"
A USB Bluetooth module should be detected after connecting it to the USB port (if using a Bluetooth USB dongle):
root@colibri-vf:~# lsusbBus 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 bluetoothEnabled 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:~# hciconfighci0: Type: BR/EDR Bus: USBBD Address: 00:1A:7D:DA:71:13 ACL MTU: 310:10 SCO MTU: 64:8UP RUNNINGRX bytes:574 acl:0 sco:0 events:30 errors:0TX bytes:368 acl:0 sco:0 commands:30 errors:0
For an SDIO module:
root@colibri-imx6ull:~# hciconfighci0: Type: Primary Bus: SDIOBD Address: 00:19:88:5E:10:B1 ACL MTU: 1021:7 SCO MTU: 120:6UP RUNNINGRX bytes:1882 acl:0 sco:0 events:83 errors:0TX bytes:2075 acl:0 sco:0 commands:83 errors:0
Starting with BSP version 2.7, bluetoothctl was added to the Linux image. It enables scanning, pairing and connecting to devices, among other features.
root@colibri-imx6ull:~# bluetoothctl[NEW] Controller 00:19:88:5E:10:B1 colibri-imx6ull [default]Agent registered
List all available options:
[bluetooth]# helpAvailable commands:list List available controllersshow [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:88Attempting to pair with 98:39:8E:1B:D8:88[CHG] Device 98:39:8E:1B:D8:88 Connected: yesRequest 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: yesPairing 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: yesChanging 98:39:8E:1B:D8:88 trust succeeded
Connect to the Bluetooth device:
[bluetooth]# connect 98:39:8E:1B:D8:88Attempting to connect to 98:39:8E:1B:D8:88[CHG] Device 98:39:8E:1B:D8:88 Connected: yesConnection 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:88Device 98:39:8E:1B:D8:88Name: Galaxy A5 (2016)Alias: Galaxy A5 (2016)Class: 0x5a020cIcon: phonePaired: yesTrusted: yesBlocked: noConnected: yesLegacyPairing: noUUID: 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:v0075p0100d0200ManufacturerData Key: 0x0075ManufacturerData Value: 0x77ManufacturerData Value: 0x98ManufacturerData Value: 0x39ManufacturerData Value: 0x8eManufacturerData Value: 0x1bManufacturerData Value: 0xd8ManufacturerData Value: 0x88
You may want to explore the BlueZ API documentation.
Warning: obexftp and other old utilities like rfcomm or sdptool don't seem to work correctly with BlueZ 5+ unless you do the following procedure.
In our BSP 2.8, we updated the BlueZ stack into version 5.46. However, this meant that, by default, most of the older available tools that use the C API won't work out of the box.
However, you can change the Bluetooth service to launch the Bluetooth daemon with the compatibility mode to enable the use of this older API.
Edit the Bluetooth service to enable the compatibility mode
root@colibri-imx6ull:~# systemctl status bluetooth ● bluetooth.service - Bluetooth service Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset Active: active (running) since Mon 2019-04-01 18:57:46 UTC; 10min ago Docs: man:bluetoothd(8) Main PID: 302 (bluetoothd) Status: "Running" CGroup: /system.slice/bluetooth.service └─302 /usr/libexec/bluetooth/bluetoothd Apr 01 18:57:46 colibri-imx6ull systemd: Starting Bluetooth service... Apr 01 18:57:46 colibri-imx6ull bluetoothd: Bluetooth daemon 5.46 Apr 01 18:57:46 colibri-imx6ull systemd: Started Bluetooth service. Apr 01 18:57:46 colibri-imx6ull bluetoothd: Starting SDP server Apr 01 18:57:47 colibri-imx6ull bluetoothd: Bluetooth management interface
root@colibri-imx6ull:~# vi /lib/systemd/system/bluetooth.service
Then we change the line ExecStart from:
Finally, we save the file and reload the daemon
root@colibri-imx6ull:~# systemctl daemon-reload root@colibri-imx6ull:~# systemctl restart bluetooth
SPP, known as Serial Port Profile, is one of the most basic Bluetooth profiles that could confirm the operation between both devices.
At this point, you should have your Bluetooth enabled, the compatibility mode enabled and, for simplicity, have both devices paired and trusted already.
Warning: Since the following tools are based on the older C API, with BlueZ 5 the compatibility mode must be used.
Add the SPP to the list of Bluetooth profiles. In the example below the Bluetooth channel number to access this profile is 1:
root@colibri-imx6ull:~# sdptool add --channel=1 SP Serial Port service registered
Enable the background listening for raw connections on the channel 1:
root@colibri-imx6ull:~# rfcomm --raw listen /dev/rfcomm0 1 & Waiting for connection on channel 1
On the other device, connect to the Toradex module by using an SPP compatible application. We have tested to be working with:
$ sudo rfcomm --raw connect 0 <MAC Address of the module> 1 Connected /dev/rfcomm0 to XX:XX:XX:XX:XX:XX on channel 1 Press CTRL-C for hangup
At this point you should be able to send and read information through the rfcomm port with the echo and cat commands.
root@colibri-imx6ull:~# echo "Hello World from Toradex" > /dev/rfcomm0"Hello World from Toradex" should appear on the other end.
And if we send something from the other end, we can read it like this:
root@colibri-imx6ull:~# cat /dev/rfcomm0 Hello Toradex!
Warning: Disclaimer: As we have no control of what it is included or removed on BlueZ, there is a chance that in future revisions the compatibility mode might be removed. We recommend to use the newer D-bus API for new developments. However, we don't expect to make any revision changes on our 2.8 BSP.
At the moment of writing this documentation (Q4 2019), there are not many newer tools updated tools that use the D-bus API and therefore, for an out-of-the-box experience, it is very likely that you would want to enable the compatibility mode of the C API (See above).
You may find more information about this under BlueZ 5 D-Bus API Documentation.
Merely as an example, here is a project of an SPP echo server and client that will make the serial port service available for other devices: SPP BlueZ5 example
This program will capture the messages sent through SPP from another device and send them back to it. It is just a mere PoC, but feel free to use it as reference for your development.
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 updateopkg install gitgit clone https://github.com/karulis/pybluez.git
Then you can run the samples using the Python interpreter:
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.
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.txtBrowsing 14:DD:A9:36:DD:FC ...Connecting..\doneTried to connect for 20msSending "file.txt"...|doneDisconnecting../done
The transferred file is now available on the device and is shown by the Bluetooth File Transfer application.
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.