PocketCHIP Bluez 5.23 A2DP to headset for audio

The reason for this post is to find a baseline for the actual bluetooth hardware on the PocketCHIP and see if bluez works properly on the device at all.

PocketCHIP is Jessie Debian based, I needed this information here, which I will build upon;

sudo apt-get install pulseaudio pulseaudio-module-bluetooth pavucontrol bluez-firmware
sudo service bluetooth restart
killall pulseaudio
sudo vi /etc/bluetooth/main.conf
Added “Enabled=Source,Sink” in [General] Section


sudo vi /etc/pulse/default.pa
load-module module-switch-on-connect

I needed a music player, this seemed great for this platform;
sudo apt-get install moc

mocp is command line to start it.

The sequence goes like this;

bluetoothctl to scan/trust/pair the a2dp headset

pacmd list sinks (sinks are outputs)
pactl set-default-sink bluez-device

pacmd list sinks | grep blue
Default sink name: bluez_sink.30_21_0A_30_06_CC
name: <module-bluetooth-policy>
module.description = “When a bluetooth sink or source is added, load module-loopback”
name: <module-bluetooth-discover>
name: <module-bluez5-discover>
name: <module-bluez4-discover>
name: <module-bluez5-device>
argument: <path=/org/bluez/hci0/dev_30_21_0A_30_06_CC>
name: <bluez_sink.30_21_0A_30_06_CC>
driver: <module-bluez5-device.c>
card: 1 <bluez_card.30_21_0A_30_06_CC>
bluetooth.protocol = “a2dp_sink”
device.api = “bluez”
device.bus = “bluetooth”
bluez.path = “/org/bluez/hci0/dev_30_21_0A_30_06_CC”
bluez.class = “0x240408”
bluez.alias = “BTH175”
device.icon_name = “audio-handsfree-bluetooth”
name: <bluez_sink.30_21_0A_30_06_CC.monitor>
driver: <module-bluez5-device.c>
card: 1 <bluez_card.30_21_0A_30_06_CC>
device.api = “bluez”
device.bus = “bluetooth”
bluez.path = “/org/bluez/hci0/dev_30_21_0A_30_06_CC”
bluez.class = “0x240408”
bluez.alias = “BTH175”
device.icon_name = “audio-handsfree-bluetooth”
name: <bluez_card.30_21_0A_30_06_CC>
driver: <module-bluez5-device.c>
device.api = “bluez”
device.bus = “bluetooth”
bluez.path = “/org/bluez/hci0/dev_30_21_0A_30_06_CC”
bluez.class = “0x240408”
bluez.alias = “BTH175”
device.icon_name = “audio-handsfree-bluetooth”
bluez_sink.30_21_0A_30_06_CC/#1: BTH175
bluez_sink.30_21_0A_30_06_CC.monitor/#2: Monitor of BTH175
chip@chip:~$ pactl set-default-sink bluez_sink.30_21_0A_30_06_CC


Notes on quality, pops in sound etc;
Worked great.
Sounded great.
It continually streamed properly like you would expect for a commercial device. 20+ minutes no issues.
PocketCHIP turned its display off and the audio kept playing.

Conclusions for this single test: A2DP wireless headsets can be used for PocketCHIP reliably.  The games will also use the pulseaudio default sink thru A2DP/Bluetooth which is great (no wires needed). With the Jessie provided Bluez 5.23 to boot.

Cross building bluez-5.46-1 for ARMHF

bluez is an interesting beast, requiring about 10 additional libraries to build if your building without a build system like dpkg-buildpackage.

Back Story: Last week I gave up cable TV (save yourself $100!!) to pursue more Linux projects to come up to speed with the community.  I love PocketCHIP and RaspberryPi 3 this year.  I want so many other linux gadgets (Voder, ReSpeaker V2 to name a few) I think I need to master  a few before looking much further.    The one constant with these small platforms is hindered by the ability to debug easily and effectively.  I’ve been actually working on a MIPSEL platform for 4 months and gdbserver interferes to much with our app, its slow. But to install GDB would require to many dependencies and we only have 95MB of free space.  Ideally a statically linked GDB would possibly work.  For now we (our team) will rely on static code checking, gdbserver, and gdb-multiarch.

Truly the Debian community have engineered wonderful solutions to the problems of multi-platform building.

I still need pbuilder to work on stretch, I will work that out today.  Once I do, I will then be able to provide ‘backport’ bluez packages to the debian community in the backports repository.

I need a Debian package sponsor!

How do I compile so fast?
quad core, but notice I’m not using -j4 to indicate four jobs.
look up and configure ccache for yourself.

A) First I check that the source and build system can build the package for the native architecture;

What surprised me here is that last week, I did a ‘sudo dpkg –add-architecture armhf’ and its still active.  At least I will be ready to cross build bluez.

sudo apt-get build-dep bluez
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libasan3:armhf libatomic1:armhf libc6-dev:armhf libcap-ng0:armhf
libdbus-1-3:armhf libdbus-1-dev:armhf libdbus-glib-1-2:armhf libexpat1:armhf
libffi6:armhf libgcc-6-dev:armhf libgcrypt20:armhf libglib2.0-0:armhf
libgomp1:armhf libgpg-error0:armhf libical2:armhf libicu57:armhf
liblz4-1:armhf libpcre16-3:armhf libpcre3-dev:armhf libpcre32-3:armhf
libpcrecpp0v5:armhf libstdc++-6-dev:armhf libstdc++6:armhf libsubunit0:armhf
libsystemd0:armhf libubsan0:armhf linux-libc-dev:armhf zlib1g-dev:armhf
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
check:armhf libcap-ng-dev:armhf libdbus-glib-1-dev:armhf
libglib2.0-dev:armhf libical-dev:armhf libicu-dev:armhf libsubunit-dev:armhf
The following NEW packages will be installed:
check libcap-ng-dev libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev
libical-dev libicu-dev libpcre3-dev libpcre32-3 libpcrecpp0v5 libsubunit-dev
libsubunit0 zlib1g-dev
0 upgraded, 13 newly installed, 7 to remove and 0 not upgraded.
Need to get 0 B/21.6 MB of archives.
After this operation, 30.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

B) Well at this point I just grab my source,  the source is multi-platform dpkg-buildpackage ready.
wget https://github.com/eSpecialized/bluez-5.46-rpi3/archive/5.46-1.tar.gz
tar xvf 5.46-1.tar.gz
mv bluez-5.46-rpi3-5.46-1/ bluez-5.46

C) Native architecture build time.
time dpkg-buildpackage -rfakeroot -uc -b
real 1m25.610s
user 1m13.108s
sys 0m3.988s

D) Moving on to build for arm!
https://wiki.debian.org/CrossCompiling << I use this a lot

sudo dpkg --add-architecture armhf
sudo apt-get update
sudo apt-get install build-essential crossbuild-essential-armhf
sudo apt-get build-dep -aarmhf bluez
dpkg-buildpackage -rfakeroot -Tclean -d
time CONFIG_SITE=/etc/dpkg-cross/cross-config.armhf DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -aarmhf -rfakeroot -uc -b
real 1m35.282s
user 1m21.416s
sys 0m4.432s

D) Moving on to build for mipsel!

sudo dpkg --add-architecture mipsel
sudo apt-get update
sudo apt-get install build-essential crossbuild-essential-mipsel
sudo apt-get build-dep -amipsel bluez

#you must use -d to skip deps checking for cleaning.
dpkg-buildpackage -rfakeroot -Tclean -d
time CONFIG_SITE=/etc/dpkg-cross/cross-config.mipsel DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -amipsel -rfakeroot -uc -b
real 1m47.038s
user 1m34.288s
sys 0m4.996s

Results are three platforms all ready to install

bill@BillsStretch:~/crossBuildArm/bluez-5.46$ ls ../*amd64.deb ; ls ../*armhf.deb ; ls ../*mipsel.deb

../bluez_5.46_amd64.deb ../bluez-hcidump_5.46_amd64.deb ../libbluetooth3 dbg_5.46_amd64.deb
../bluez-cups_5.46_amd64.deb ../bluez-obexd_5.46_amd64.deb ../libbluetooth-dev_5.46_amd64.deb
../bluez-dbg_5.46_amd64.deb ../libbluetooth3_5.46_amd64.deb

../bluez_5.46_armhf.deb ../bluez-hcidump_5.46_armhf.deb ../libbluetooth3-dbg_5.46_armhf.deb
../bluez-cups_5.46_armhf.deb ../bluez-obexd_5.46_armhf.deb ../libbluetooth-dev_5.46_armhf.deb
../bluez-dbg_5.46_armhf.deb ../libbluetooth3_5.46_armhf.deb

../bluez_5.46_mipsel.deb ../bluez-hcidump_5.46_mipsel.deb ../libbluetooth3-dbg_5.46_mipsel.deb
../bluez-cups_5.46_mipsel.deb ../bluez-obexd_5.46_mipsel.deb ../libbluetooth-dev_5.46_mipsel.deb
../bluez-dbg_5.46_mipsel.deb ../libbluetooth3_5.46_mipsel.deb


What comes after this?

I’ve documented cross building. Unfortunately this won’t work for your old Jessie Raspberry Pi Rasbian. If you have stretch then yes it will work.  I won’t release the packages I just built until I get pbuilder up and running.

Well I will need to setup pbuilder, and setting up a debian repo for testing with. Finally getting a sponsor to provide backports.

Test out bluez 5.46 for stability.   My PocketCHIP had a corrupt NAND cell (or 2). It has been re-flashed, so I will test out this build on the chip AFTER I test 5.23 with a2dp and BLE.


What is backports?
Debian backports allow you to use new software on older systems like Jessie.  That or you are left with just doing what I did here. Unfortunately bluez is not in that pool of packages.


What is pbuilder?
it allows a clean room environment to build package, thus ensuring a minimal package dependency build on your target platform, and a properly built one at that.  Notice that I’m using a fakeroot, but that isn’t quite the same. You still get all the packages and dependencies on my system (and Debian Stretch) that were used to build the packages.

pbuilder lets me target OS’s like Jessie and further back.


Bluez 5.46 hciuart configuration changes

Update; 2017-08-13 Sunday
I discovered the problem is really in the tools/hciattach_bcm43xx.c file
I have now uploaded a patch to the git repo.

1. The speed cannot be set before the fiimware is uploaded

2. Wait 1 second before the reset command is sent.

Patch information;

git diff tools/hciattach_bcm43xx.c
diff --git a/tools/hciattach_bcm43xx.c b/tools/hciattach_bcm43xx.c
index 81f38cb..13a877c 100644
--- a/tools/hciattach_bcm43xx.c
+++ b/tools/hciattach_bcm43xx.c
@@ -43,7 +43,7 @@
#include "hciattach.h"
-#define FIRMWARE_DIR "/etc/firmware"
+#define FIRMWARE_DIR "/lib/firmware"
#define FW_EXT ".hcd"
@@ -368,8 +368,6 @@ int bcm43xx_init(int fd, int def_speed, int speed, struct termios *ti,
if (bcm43xx_locate_patch(FIRMWARE_DIR, chip_name, fw_path)) {
fprintf(stderr, "Patch not found, continue anyway\n");
} else {
- if (bcm43xx_set_speed(fd, ti, speed))
- return -1;
if (bcm43xx_load_firmware(fd, fw_path))
return -1;
@@ -380,6 +378,7 @@ int bcm43xx_init(int fd, int def_speed, int speed, struct termios *ti,
return -1;
+ sleep(1);
if (bcm43xx_reset(fd))
return -1;

Nikolay patch; This has the binary file in it hciattach you can just replace yours with..

Nikolay solution Is here;

Nikolay (2xl) wrote on 2017-04-04:
I did the same patch – modify firmware patch and speed initialization patch… and I got bluetooth working on RPi!

############################## older information

When you upgrade to the latest bluez 5.46-1 everything will work as of 2017-08-12.

goal;  that firmware simply loads on boot and bluetooth works.

Indications that firmware loading failed;
   the mac address is all AA’s

hci0: Type: Primary Bus: UART
BD Address:  AA:AA:AA:AA:AA:AA ACL MTU: 1021:8 SCO MTU: 64:1
RX bytes:2095 acl:0 sco:0 events:118 errors:0
TX bytes:1974 acl:0 sco:0 commands:118 errors:0

How does it work to update bluez-5.46 then later not?

The Chip still has its firmware uploaded, until you reboot. Then the firmware needs to be reloaded.  And there is the change;

/etc/firmware is expected location from hciattach
BCM43430A1.hcd << one file needed

/var/firmware is where all the RPI’s firmware is located.
/var/firmware/BCM43430A1.hcd << cp this

mkdir /etc/firmware
cp /var/firmware/BCM43430A1.hcd /etc/firmware


Other related files;


My final analysis in retrospect;
The bluez 5.23 package that is in Jessie has an edit not part of the patches in tools/hciuart_bcm43xx.c, and this lead to a missed non working update to 5.46.  All is working in the 5.46-1 release.

Bluez 5.46 And BLE quick test

Once you have upgraded to Bluez 5.46 from the previous post;

You can perform a quick BLE test with this file

To start your BLE adventure;
sudo hciconfig hci0 leadv 0 #this sets LE advertising on and as ‘connectable’
./gatt-service #starts the service

The source for gatt-service;
inside the bluez/tools folder is all the source you need to get started with your own Linux BLE Applet.

How can I view the BLE service?
there are lots of BLE exploring apps, I use the “LightBlue” App on my iPhone.

Build Bluez 5.46 yourself for your Debian Jessie RPI3

Updated on 2017-08-13 Sunday – all working with 5.46-1 on RPI3.
All patches that were included as part of ‘stretch’ deb source Bluez 5.43 are now in 5.46-1

You can git the source package contents here;
git clone https://github.com/eSpecialized/bluez-5.46-rpi3.git


Pre-built packages ready to install;

Why would you want to do this?
-To get the latest BLE (Low Energy Features) of Bluez, its required.
-A lot of bug fixes were made since 5.23 that is available on RPI3 Jessie that are needed for BLE to work reliably.
-Using a debian package allows for a clean upgrade path. Files are guaranteed to upgrade as opposed to “make install” over a running bluetooth system which doesn’t work.
-Debian Stretch doesn’t include the latest bluez 5.46, it comes close but I found that (during May 2017) that BLE didn’t function correctly.

Note: This was built on a RPI3 with Jessie.

Steps are intentionally missing, you will need to make up the differences to get the new source into the old directory, update the changelog in the ./debian folders with dch -i and a few other things to get a new build.  In my source package all this done for convenience.

How I made it;
#Activate src in /etc/apt/sources.list
# Uncomment line below then ‘apt-get update’ to enable ‘apt-get source’

vi /etc/apt/sources.list

deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi

sudo apt-get install ccache  #configure this is required! ie Add to Path!
sudo apt-get install build-essential devscripts lintian diffutils patch patchutils
cd /usr/src
apt-get source bluez
apt-get build-depend bluez

#grab a version from bluez.org

cd bluez-5.23

#untar the source ball in the new directory

#How to build it;
#On the RPI3 it takes about 15 minutes without ccache
time dpkg-buildpackage -rfakeroot -uc -b

real 5m48.312s #<< using ccache
user 4m0.350s
sys 0m24.680s

How to clean it;

dpkg-buildpackage -rfakeroot -Tclean

Find what bluez packages you have installed now;

pi@billspi3:/usr/src $ dpkg -l | egrep -e '^\||blue'
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
ii bluej 3.1.7b all A simple but powerful Java IDE.
ii bluez 5.43-2 armhf Bluetooth tools and daemons
ii bluez-firmware 1.2-3+rpi1 all Firmware for Bluetooth devices
ii libbluetooth3:armhf 5.43-2 armhf Library to use the BlueZ Linux Bluetooth stack
ii pi-bluetooth 0.1.3 armhf Raspberry Pi 3 bluetooth
ii pulseaudio-module-bluetooth 5.0-13 armhf Bluetooth module for PulseAudio sound server

How to install only what you need;
sudo dpkg -i <package name>
#From the above list, you can see I only need bluez and libbluetooth3.
sudo dpkg -i bluez_5.46_armhf.deb libbluetooth3_5.46_armhf.deb