The officially official Devuan Forum!

You are not logged in.

#1 2019-03-10 11:00:18

NewForester
Member
Registered: 2019-03-08
Posts: 10  

HOW TO: Raspberry 3B Pi Internal Bluetooth

Some models of the Raspberry Pi come with internal WIFI and Bluetooth. The internal WIFI works 'out of the box' with the Devuan ascii image for Raspberry Pi 3 but not the Bluetooth.

This how to is not about how to get external Bluetooth dongles working (I believe these should work 'out of the box'). It describes how to install and configuration the necessary firmware and software to get the internal Bluetooth working on Devuan ascii. No GUI required.

The essential technical read is: How to get BT working on Pi3B. By that discussion's definition, I am not 'clever'. The notes below are hopefully simple enough for anyone to digest: just bring your own spoon. Consider them a work in progress and feel free to suggest improvements.

The essential user read is: RPi3 Bluetooth and Wifi don't play well together. That discussion there suggests strongly that you will not be able to get the best of both worlds. This was the case in March 2016 and, sadly, is still the case in March 2019. It seems most folk use the internal WIFI and (continue) to use an external Bluetooth dongle. If that isn't for you, read on.

I used (or made reference to) the most up to date hardware / firmware / software at the time:

  • Raspberry Pi 3B+

  • devuan_ascii_2.0.0_arm64_raspi3.img.xz (6090bec93b5b887988af0acf92e1d511)

  • for 'reference': 2018-11-13-raspbian-stretch.zip (5c5afb1448a7037be109fd69c8578a5c)

I actually found the internal Bluetooth worked better with the Deuvan image than with the 'reference' although I have no idea why. I was able to 'solve' the CapsLock issue. The result is quite usable provided you don't use the internal WIFI at the same time.

Offline

#2 2019-03-10 11:02:16

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

One-time Installation and Configuration

The installation and configuration is as for the 'reference' platform without the systemd bits.
This means:

  • preliminary changes to the Devuan startup on your Pi

  • installation of firmware

  • building and installation bluez software (optional)

  • adapting some simple system startup scripts/files

Offline

#3 2019-03-10 11:10:53

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

Preliminaries

So that you do not waste a lot of time by starting out the wrong place.

What you don't need to do is use /boot/overlays/pi3-disable-wifi.dtbo to disable the internal WIFI device on your Pi (it might help) but you do need to make sure /boot/overlays/pi3-disable-bt.dtbo has not been used to disable the internal Bluetooth (by default, it isn't). If unsure where to look, try /boot/config.txt

You should use an external WIFI USB dongle and alter your Pi's /etc/network/interfaces to use wlan1 by default (change to 'allow-hotplug') and not wlan0 (change to 'allow-manual'). You could use a wired Ethernet instead but you still need to check the wlan0 is not brought up automatically during system startup.

What is absolutely essential is to ensure /etc/inittab is not trying to initialise /dev/ttyAMA0 as a serial console. That should mean adding a # to the beginning of the last line in order to comment it out. It now reads:

    #T0:23:respawn:/sbin/agetty -L ttyAMA0 115200 vt100

Reboot.

I haven't (yet) used a serial console with a Raspberry Pi (I prefer ssh over minicom and both over PuTTY). If you do, you may want to use ttyS0 and you may need to change the baud rate. I recommend you read up on this first and make sure your new console arrangement is working before going any further.

Check /boot/cmdline.txt on your Pi. It may contain something like:

    console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

You may want to change these ttyS0 but, if you check /proc/cmdline, you may find system startup does this for you.

Offline

#4 2019-03-10 11:16:28

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

Packages

Installing the Devuan binary packages should ensure packages needed for Bluetooth are installed:

sudo apt-get install bluetooth bluez-firmware

Building (even downloading) source packages requires a large number of prerequisite packages be installed first:

sudo apt-get install build-essential
sudo apt-get install debhelper

The 'reference' distribution has several packages that are pertinent. I recommend downloading the source (not the binary) versions. They are not where you might expect to find them.

This works but is not pretty

mkdir somewheretidy && cd somewheretidy

# alter repository access
echo "deb-src http://archive.raspberrypi.org/debian/ stretch main ui" | sudo tee /etc/apt/sources.list.d/raspian-sources.list
sudo apt-get update

# download the sources
apt-get --allow-unauthenticated source bluez-firmware
apt-get --allow-unauthenticated source bluez
apt-get --allow-unauthenticated source pi-bluetooth

# restore repository access
sudo rm /etc/apt/sources.list.d/raspian-sources.list
sudo apt-get update

There is one other file of interest but that lives in a different source repository. We'll come back to that later.

Offline

#5 2019-03-10 11:34:03

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

Building bluez-firmware

The first source download includes new firmware not shipped with Devuan. To rebuild and install the bluez_firmware package:

    cd bluez-firmware-1.2
    dpkg-buildpackage -b -uc
    cd -
    sudo dpkg -i bluez-firmware_1.2-3+rpt7_all.deb

Offline

#6 2019-03-10 11:43:40

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

Building bluez

There is a short cut I recommend you take first time around. All you need do is add a symbolic link:

    sudo ln -s /lib/firmware /etc

and go on to the next post.

The scenic route takes quite a while and involves some jiggery-pokery. I did it mostly because learning and being through are part of my nature but, until a serious patch for the Raspberry Pi Bluetooth comes along, rebuilding bluez seems unnecessary.

Installing the Devuan ascii bluetooth package gives you bluez revision 5.43-2+deb9u1. Rebuilding gives you revision 5.43-2+rpt2+deb9u2. That is three small patches, one of which changes the firmware directory from /etc/firmware to /lib/firmware.

There are prerequisite dependencies that need to be installed. See bluez_5.43-2+rpt2+deb9u2.dsc. Ignore systemd and dh-systemd:

    sudo apt-get install flex bison libdbus-glib-1-dev libglib2.0-dev
    sudo apt-get install libcap-ng-dev libudev-dev libreadline-dev libical-dev check

The bluez source package is, as expected, geared to building a binary package with systemd support. I downloaded the Devuan bluez source package but that too was geared to systemd. Odd. I tried to find notes in the Devuan documentation on how approach removing systemd from a source packages but all I found were references to script named d1h whose description sounded like it need a lot of set up and didn't admit to doing what I wanted.

So I used a sledgehammer approach. I changed/removed four lines in three files. You may be able to do better:

--- ../raspian/bluez-5.43/debian/rules  2017-09-13 07:16:27.000000000 +0000
+++ bluez-5.43/debian/rules     2019-02-17 15:34:37.565937194 +0000

@@ -17,13 +17,13 @@
        --enable-udev \
        --enable-obex \
        --enable-client \
 -       --enable-systemd \
 +       --disable-systemd \
        --enable-threads \
        --enable-sixaxis \
        --enable-experimental

 %:
 -       dh $@ --with systemd,autoreconf
 +       dh $@ --with autoreconf

 override_dh_install:
        dh_install --list-missing
--- ../raspian/bluez-5.43/debian/bluez.install  2017-09-13 07:16:27.000000000 +0000
+++ bluez-5.43/debian/bluez.install     2019-02-17 16:12:19.313738258 +0000
@@ -26,7 +26,7 @@
 lib/udev/rules.d/97-hid2hci.rules
 attrib/gatttool usr/bin
 #-- for systemd
-lib/systemd/system/bluetooth.service
+#lib/systemd/system/bluetooth.service
 etc/dbus-1/system.d/bluetooth.conf
 usr/share/dbus-1/system-services/org.bluez.service
--- ../raspian/bluez-5.43/debian/bluez-obexd.install    2017-09-13 07:16:27.000000000 +0000
+++ bluez-5.43/debian/bluez-obexd.install       2019-02-17 16:12:57.522554333 +0000
@@ -1,3 +1,3 @@
 usr/lib/bluetooth/obexd
 usr/share/dbus-1/services/org.bluez.obex.service
-usr/lib/systemd/user/obex.service
+#usr/lib/systemd/user/obex.service

After changing these three files, build and install the bluez package with:

    cd bluez-5.43
    dpkg-buildpackage -b -uc -d
    cd -
    sudo dpkg -i bluez_5.43-2+rpt2+deb9u2_arm64.deb

The build produces 12 binary packages. Depending on your specific (e.g. GUI) needs, you may need to install more than just the bluez binary package.

Offline

#7 2019-03-10 11:49:45

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

System Startup

Here you convert a small set of files designed for systemd into a (smaller) set of files that do not. This is the fun part.

There is one file that hasn't been downloaded yet. Get it with:

    wget https://raw.githubusercontent.com/RPi-Distro/raspberrypi-sys-mods/master/etc.armhf/udev/rules.d/99-com.rules

This SUBSYSTEM rules in this file are not required:

    sed -e '/KERNEL/,$!d' -i 99-com.rules

The KERNEL rules create /dev/serial0 and/or /dev/seria11 apparently so that you can rewrite your now broken apps that relied on /dev/ttyAMA0 being a console portably.

    sudo cp -p 99-com.rules /etc/udev/rules.d

There are two scripts in the pi-bluetooth-0.1.10/ package that are required:

    sudo cp -p pi-bluetooth-0.1.10/usr/bin/* /usr/bin/

A quick sanity check is in order at this point. First that the device firmware can be loaded:

    sudo modprobe hci_uart
    sudo /usr/bin/btuart
bcm43xx_init
Set Controller UART speed to 3000000 bit/s
Flash firmware /etc/firmware/brcm/BCM4345C0.hcd
Set BDADDR UART: b8:27:eb:2c:68:67
Set Controller UART speed to 3000000 bit/s
Device setup complete

Then check the device is up and running:

    hciconfig
hci0:   Type: Primary  Bus: UART
        BD Address: B8:27:EB:2C:68:67  ACL MTU: 1021:8  SCO MTU: 64:1
        UP RUNNING
        RX bytes:785 acl:0 sco:0 events:49 errors:0
        TX bytes:2256 acl:0 sco:0 commands:49 errors:0

If things are not right, this command will show nothing.

So that these two scripts are run automatically during startup, create your own 90-pi-bluetooth.rules and place it in /etc/udev/rules.d/:

    # Attach hci uart
    ACTION=="add", SUBSYSTEM=="module", KERNEL=="hci_uart", RUN+="/usr/bin/btuart"

    # Raspberry Pi bluetooth module: enable routing of SCO packets to the HCI interface
    ACTION=="add", SUBSYSTEM=="bluetooth", KERNEL=="hci[0-9]", RUN+="/usr/bin/bthelper %k"

Finally, add to /etc/modules a line to load hci_uart.

Note: the copied and edited files may not end up owned by 'root:root'. Fix using chown(1)

Offline

#8 2019-03-10 11:55:33

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

Device Pairing

I have only paired a Bluetooth keyboard (with touchpad): it is all I need.

There are a lot of posts on the Internet complaining Bluetooth is not working that turn out to have been posted by folk who somehow have not learnt that Bluetooth devices require pairing.

I used bluetoothctl from the command line. Since I wanted to substitute the Bluetooth keyboard for the conventional keyboard, I used the 'trust' command.

I had some trouble with this. It goes better when the internal WIFI is down (or disabled). The dialogue should go something like this:

pi@devuan:~/keep/etc$ sudo bluetoothctl
[NEW] Controller B8:27:EB:2C:68:67 BlueZ 5.43 [default]

[bluetooth]# agent on
Agent registered

[bluetooth]# default-agent
Default agent request successful

[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:2C:68:67 Discovering: yes

Press the connect button on the Bluetooth keyboard, Press its return key for good measure. Wait.

[NEW] Device 20:20:01:00:4D:F8 Bluetooth 3.0 Keyboard

[bluetooth]# pair 20:20:01:00:4D:F8
Attempting to pair with 20:20:01:00:4D:F8
[CHG] Device 20:20:01:00:4D:F8 Connected: yes
[agent] PIN code: 772158

Enter the PIN code on the Bluetooth keyboard.

[CHG] Device 20:20:01:00:4D:F8 Paired: yes
Pairing successful

[bluetooth]# connect 20:20:01:00:4D:F8
Attempting to connect to 20:20:01:00:4D:F8
[CHG] Device 20:20:01:00:4D:F8 Connected: yes
Connection successful
[CHG] Device 20:20:01:00:4D:F8 ServicesResolved: yes

[Bluetooth 3.0 Keyboard]# trust 20:20:01:00:4D:F8
[CHG] Device 20:20:01:00:4D:F8 Trusted: yes
Changing 20:20:01:00:4D:F8 trust succeeded

[Bluetooth 3.0 Keyboard]# quit
Agent unregistered
[DEL] Controller B8:27:EB:2C:68:67 BlueZ 5.43 [default]

Offline

#9 2019-03-10 12:01:28

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

CapsLock Workaround

I don't use CapsLock but I do hit the key by mistake from time to time, especially on a keyboard I have not yet grown used to.

This appears to cause the Pi to lock up and accept no more input from the Bluetooth keyboard. If I waited long enough, it would accept a few more keys before appearing to lock up again. If I persisted, eventually a key would 'stick' and I'd get the same letter repeated hundreds of times. The key remained stuck even after powering off the Bluetooth keyboard. I had the same with the return key and also backspace (this ate my entire document backwards, one character at a time).

Debian Bug report logs - #776363 suggests the problem has been around since 2015. The third post in this thread CapsLock LED not working in Raspbian spells out the problem and suggests a solution. It suggests the problem is Debian, not Bluetooth specific. Software rot perhaps.

First you need to ensure the Linux command line keyboard utilities are installed:

    sudo apt-get install kbd

Then add:

   /usr/bin/dumpkeys | /bin/fgrep ' 58 ' | /bin/sed 's/CtrlL_Lock/Caps_Lock/g' | /usr/bin/loadkeys -

to the script /etc/rc.local and make sure the script is executable.

This restores the 'default', non-Debian, behaviour of the CapsLock key and I have had no problems with keys doing the unexpected since.

Note the use of full pathnames here: /etc/rc.local runs without a PATH.

Last edited by NewForester (2019-03-17 18:05:19)

Offline

#10 2019-03-26 11:51:43

NewForester
Member
Registered: 2019-03-08
Posts: 10  

Re: HOW TO: Raspberry 3B Pi Internal Bluetooth

CapsLock Workaround Revistied

It seems that changing the key mapping for the CapsLock key is not sufficient and may not be necessary:
I used a new installation to SD to check my notes and found I had a poor Bluetooth connection and unacceptable behaviour from the CapsLock key.

What had I not done this time around that had produced the right result last time around ?
I don't like the answer.  It is the kind of thing that I mistrust for it smacks of 'magic' rather than 'understanding'.
Your mileage may vary, but for what it is worth ...

I used bluetoothctl to delete the keyboard device and rebooted the Pi.
I then used bluetoothctl to scan for the keyboard, paired with it, connect to it and then set is as trusted and rebooted the Pi once more.
No trouble since then.

Offline

Board footer