Linux Format - UK (2019-12)

(Antfer) #1
http://www.techradar.com/pro/linux December 2019 LXF256 55

Pi Bluetooth TUTORIALS


experimental
make -j4
sudo make install
There is a problem with the operating system groups
and permissions that prevents the ‘pi’ user from
accessing the service over DBus. Issue the following
command to correct the issue.
sudo usermod -G Bluetooth -a pi
An amendment is needed to the Bluetooth
configuration file in order to take advantage of the group
and permission changes. Make a copy of the
configuration file before starting.
sudo cp /etc/dbus-1/system.d/Bluetooth.conf /etc/
dbus-1/system.d/Bluetooth.conf.bak
Using your favourite text editor, add the following
lines to the Bluetooth configuration file /etc/dbus-1/
system.d/Bluetooth.conf. Ensure you preserve the
file contents layout structure during your edit – adding
additional spaces or tabs to make it look pretty may
result in a file that doesn’t work, and you may get no
indication of the problem.
<policy group=”Bluetooth”>
<allow send_destination=”org.bluez”/>
</policy>
Now that the Bluetooth stack is in place we need a
bridge to the audio infrastructure of the Pi. Advanced
Linux Sound Architecture (ALSA) is a Pi kernel
framework that provides access to sound card device
drivers. To interface the Bluetooth protocol stack with
the ALSA framework there is a service called bluealsa.
Let’s drop in the interface software.
sudo apt install bluealsa -y
Fire off a command to initiate a reboot to bring our
Raspberry Pi back up with our replacement Bluetooth
protocol stack. If you want to see the changes that were
just implemented, you can use the commands sudo
systemctl status Bluetooth and sudo systemctl status
bluealsa to return information on the status of the two
services just installed.
You may notice something is not working. There is
a problem with the order in which Systemd modules are
loaded when the Raspbian system boots. Since
Bluetooth is a Systemd service, the problem results in
Bluetooth not working. A check of the service shows the
following error:
raspberrypi Bluetoothd[473]: Failed to set privacy:
Rejected (0x0b)
A detailed discussion of this, called ‘Bluetooth LE
peripheral can not resolve a bound device’s random
address after reboot’, can be found at the Raspberry
Raspbian Forums at http://bit.ly/lxf257blue.
A quick fix to overcome the issue for our install is to
just restart of the Bluetooth service after the Pi OS is up
and running. To automate this task, use your favourite
text editor and add the following command to the
/etc/rc.local file: sudo systemctl restart Bluetooth.
service. Now when the Pi is rebooted, Bluetooth will
have been restarted after the failure. Hopefully this
issue will get corrected by the Pi’s development team
in the near future.

Bluetooth testing
Using one of the Bluetooth version commands
introduced earlier, you should discover 5.50 as the
version now installed, replacing the earlier 5.43 version.

Now that the tedious stuff is finished, let’s sit back and
enjoy the fruits of our labour, computer-geek style, by
using the command line.
Remember that bluetoothctl is the command-line
application that provides a console for configuring Linux
Bluetooth devices. You might recognise that the
command bluetoothctl -v can be used to determine
the version of the Bluetooth protocol stack. To enter
the Bluetooth device setup console, enter bluetoothctl
at the command line. If you enter help at this point, the
console will provide a list of commands available.
This tutorial will use the commands listed in the
following order to configure an amplified Bluetooth
speaker: default-agent, scan on , pair <Device MAC> ,
connect <Device MAC> , trust <Device MAC>. The
MAC value shown in these commands should be
determined from the results returned when the
Bluetooth device is set to discoverable mode.
Following these commands, the output from the
bluetoothctl console is shown in the screenshot above.
The commands are highlighted by the green ellipses.
The red ellipsis indicate the MAC address of the device
that was provided by the SD during discoverable mode
(that is, when pressing a button on the Bluetooth
device). The MAC associated with the device is used as
a parameter to complete the subsequent commands.
Let’s do a test of our Bluetooth speaker setup. Copy
a WAV sound file to the Pi and reference the file in the
follow command:
aplay -D bluealsa:DEV=E5:28:E9:87:6B:C3 <your_wave_
file.wav>

And that’s that
After reviewing some Bluetooth protocol theory, we’ve
completed an exercise of replacing the 5.43 version
Bluetooth protocol stack with version 5.50. With the
replacement protocol stack in place we used the
Bluetooth console line to establish a Bluetooth device.
If you remember, we experienced a few problems
during the replacement exercise. In order to complete
the task we implemented some quick fixes.
Raspbian’s implementation of Bluetooth pre-Buster
was not the most current version. During this
replacement exercise, we may have stumbled across
some of the issues that has kept the Bluetooth protocol
stack version at 5.43 rather than at 5.50.
Until the next time, keep your Pi oven warm!

Five commands
in the bluetooth
protocol stack
console tool

5552Decmbr rb2c019gb09uidsy December 2019 LXF256 55


Pi Bluetooth TUTORIALS


experimental
make -j4
sudo make install
There is a problem with the operating system groups
and permissions that prevents the ‘pi’ user from
accessing the service over DBus. Issue the following
command to correct the issue.
sudo usermod -G Bluetooth -a pi
An amendment is needed to the Bluetooth
configuration file in order to take advantage of the group
and permission changes. Make a copy of the
configuration file before starting.
sudo cp /etc/dbus-1/system.d/Bluetooth.conf /etc/
dbus-1/system.d/Bluetooth.conf.bak
Using your favourite text editor, add the following
lines to the Bluetooth configuration file /etc/dbus-1/
system.d/Bluetooth.conf. Ensure you preserve the
file contents layout structure during your edit – adding
additional spaces or tabs to make it look pretty may
result in a file that doesn’t work, and you may get no
indication of the problem.





Now that the Bluetooth stack is in place we need a
bridge to the audio infrastructure of the Pi. Advanced
Linux Sound Architecture (ALSA) is a Pi kernel
framework that provides access to sound card device
drivers. To interface the Bluetooth protocol stack with
the ALSA framework there is a service called bluealsa.
Let’s drop in the interface software.
sudo apt install bluealsa -y
Fire off a command to initiate a reboot to bring our
Raspberry Pi back up with our replacement Bluetooth
protocol stack. If you want to see the changes that were
just implemented, you can use the commands sudo
systemctl status Bluetooth and sudo systemctl status
bluealsa to return information on the status of the two
services just installed.
You may notice something is not working. There is
a problem with the order in which Systemd modules are
loaded when the Raspbian system boots. Since
Bluetooth is a Systemd service, the problem results in
Bluetooth not working. A check of the service shows the
following error:
raspberrypi Bluetoothd[473]: Failed to set privacy:
Rejected (0x0b)
A detailed discussion of this, called ‘Bluetooth LE
peripheral can not resolve a bound device’s random
address after reboot’, can be found at the Raspberry
Raspbian Forums at [http://bit.ly/lxf257blue.](http://bit.ly/lxf257blue.)
A quick fix to overcome the issue for our install is to
just restart of the Bluetooth service after the Pi OS is up
and running. To automate this task, use your favourite
text editor and add the following command to the
/etc/rc.local file: sudo systemctl restart Bluetooth.
service. Now when the Pi is rebooted, Bluetooth will
have been restarted after the failure. Hopefully this
issue will get corrected by the Pi’s development team
in the near future.

Bluetooth testing
Using one of the Bluetooth version commands
introduced earlier, you should discover 5.50 as the
version now installed, replacing the earlier 5.43 version.


Now that the tedious stuff is finished, let’s sit back and
enjoy the fruits of our labour, computer-geek style, by
using the command line.
Remember that bluetoothctl is the command-line
application that provides a console for configuring Linux
Bluetooth devices. You might recognise that the
command bluetoothctl -v can be used to determine
the version of the Bluetooth protocol stack. To enter
the Bluetooth device setup console, enter bluetoothctl
at the command line. If you enter help at this point, the
console will provide a list of commands available.
This tutorial will use the commands listed in the
following order to configure an amplified Bluetooth
speaker: default-agent, scan on , pair <Device MAC> ,
connect <Device MAC> , trust <Device MAC>. The
MAC value shown in these commands should be
determined from the results returned when the
Bluetooth device is set to discoverable mode.
Following these commands, the output from the
bluetoothctl console is shown in the screenshot above.
The commands are highlighted by the green ellipses.
The red ellipsis indicate the MAC address of the device
that was provided by the SD during discoverable mode
(that is, when pressing a button on the Bluetooth
device). The MAC associated with the device is used as
a parameter to complete the subsequent commands.
Let’s do a test of our Bluetooth speaker setup. Copy
a WAV sound file to the Pi and reference the file in the
follow command:
aplay -D bluealsa:DEV=E5:28:E9:87:6B:C3 <your_wave_
file.wav>

And that’s that
After reviewing some Bluetooth protocol theory, we’ve
completed an exercise of replacing the 5.43 version
Bluetooth protocol stack with version 5.50. With the
replacement protocol stack in place we used the
Bluetooth console line to establish a Bluetooth device.
If you remember, we experienced a few problems
during the replacement exercise. In order to complete
the task we implemented some quick fixes.
Raspbian’s implementation of Bluetooth pre-Buster
was not the most current version. During this
replacement exercise, we may have stumbled across
some of the issues that has kept the Bluetooth protocol
stack version at 5.43 rather than at 5.50.
Until the next time, keep your Pi oven warm!

Five commands
in the bluetooth
protocol stack
console tool
Free download pdf