Bluetooth


This chapter describes common information and instructions of bluetooth on IoT Yocto, such as command to to scan, pair and so on.

But bluetooth on different platforms may have some platform-specific test results. For example, you will get different throughputs on different platforms.


Pair a Bluetooth Speaker then Play Sound

Bluetooth software stack in rity-demo-image supports hciconfig tool and BlueZ stack. Bluetooth service is registered as a systemd service. We cannot make bluetooth connections without these userspace services. Let’s check if the bluetooth service is running in the beginning.

Make Sure bluetooth.service and bluetoothd is Running

First, make sure bluetooth.service and bluetoothd is running.

Check bluetooth.service by systemctl.

systemctl status bluetooth.service

Check bluetoothd by ps.

ps -aux | grep bluetoothd

You should see 2 output lines correspond to each command respectively:

Active: active (running)
root         329  0.0  0.1   6952  4144 ?        Ss   10:44   0:00 /usr/libexec/bluetooth/bluetoothd

If not, launch bluetooth.service when you see the following output:

The following output means bluetooth.service has been stopped.

* bluetooth.service - Bluetooth service
 Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor pr>
 Active: inactive (dead)
    Docs: man:bluetoothd(8)

Use systemctl to enable bluetooth.service.

systemctl enable bluetooth.service

Systemctl should register dbus-org.bluez.service and bluetooth.service to systemd and here’s the output.

Created symlink /etc/systemd/system/dbus-org.bluez.service -> /lib/systemd/system/bluetooth.service.
Created symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service -> /lib/systemd/system/bluetooth.service.

Then, don’t forget use systemctl to start bluetooth.service, and then check if it is running again.

systemctl start bluetooth.service
systemctl status bluetooth.service

Make Sure the hciX Controller Exists

Use hciconfig to check if the bluetooth controller exists.

hciconfig

A valid BD Address (MAC address of the bluetooth controller) should be existed as the output instead of value 00:00:00:00:00:00.

Note

The Bus type of hciX will be different depends on the module and the platform are used. It could be SDIO or USB usually.

hci0:   Type: Primary  Bus: SDIO
    BD Address: 90:E8:68:D4:39:84  ACL MTU: 1021:6  SCO MTU: 244:4
    DOWN
    RX bytes:14464 acl:0 sco:0 events:1167 errors:0
    TX bytes:293798 acl:0 sco:0 commands:1167 errors:0

Create a New User Account

We could use pulseaudio, pactl, and paplay to play a wave sound to a paired bluetooth speaker. However, a non-root user account is required for accessing these tools. Please follow the following steps to create a non-root user..

First, add a new user.

useradd mtk

Assign /run/user/0/ to the new user.

chown -R mtk /run/user/0/

Switch to the user account mtk.

su mtk

Pair a Bluetooth Speaker

Start bluetoothctl to control the bluetooth interface interactively.

bluetoothctl

bluetoothctl will report the controller if there existed one.

Agent registered
[CHG] Controller 90:E8:68:D4:39:84 Pairable: yes

You’ll also find the prompt has been changed to [bluetooth]#, which means we are under the interactive interface and menu in bluetoothctl.

(waiting for bluetooth commands)

Before using bluetooth, we need to power on the device.

power on

If it is success, you’ll see the valid BD’s address in log with message Powered: yes.

The bluetooth controller is able to scan the neighbor devices now. Put the bluetooth speaker into pairing mode, then simply run:

scan on

bluetoothctl should dumps the BD address of each neighbor bluetooth device. Please wait until the BD address of the bluetooth speaker appeared (scanned by i350-EVK). For example, if the <BD address> of the speaker is FC:58:FA:34:46:06:

[NEW] Device FC:58:FA:34:46:06 AXLOIE Mega

After the speaker has been scanned by bluetoothctl, it should be trusted before the connection.

trust <BD address>

The device should be trusted by i350-EVK immediately.

Changing <BD address> trust succeeded.

Finally, we can pair the speaker with i350-EVK.

pair <BD address>

After a while, it should be paired successfully. Sometimes you need to repeat and enter the key for pairing, or, simply type yes in interactive interface.

[CHG] Device <BD address> Paired: yes
Pairing successful

After the devices has been paired, we could exit bluetoothctl and prepare audio tools for playing sound.

exit

Play Sound

If the previous steps worked fine, pulseaudio could be run as a daemon for dealing with audio data. Launch pulseaudio in daemon mode. Note: Please run as the user mtk.

pulseaudio -D --exit-idle-time -1

Now, we connect the bluetooth device.

bluetoothctl connect <BD address>

Wait for a while, it should be connected successfully.

[CHG] Device FC:58:FA:34:46:06 Connected: yes
Connection successful

Use pactl to list all BlueZ a2dp sink.

pactl list sinks | grep Name

You’ll find a line in outputs as below.

Name: bluez_sink.FC_58_FA_34_46_06.a2dp_sink

Finally, the music could be played to the bluetooth speaker.

paplay /usr/share/sounds/alsa/Front_Right.wav -d <sink Name>

You can hear voice Front Right form the connected speaker.

Turn off Bluetooth Service

If you don’t need bluetooth and want to turn it off, simply run following command.

systemctl disable bluetooth.service

Then the logs show as below.

Removed /etc/systemd/system/dbus-org.bluez.service.
Removed /etc/systemd/system/bluetooth.target.wants/bluetooth.service.

It deregisters the dbus-org.bluez.service and bluetooth.service from systemd. If you tried to use bluetoothctl after they’ve been deregistered, bluetoothctl will keep waiting for the bluetoothd is ready. It show the bluetooth service has been turned off.

Reference Boards

Bluetooth on different platforms may have some platform-specific test results. For example, you will get different benchmark results on different platforms. Please find more details about difference of bluetooth on each platform: