WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Howto: RPi3 built-in bluetooth  (Read 9500 times)

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14790
Howto: RPi3 built-in bluetooth
« on: March 15, 2017, 04:24:04 AM »
Enable bluetooth in the RPi3 config:
Code: [Select]
$ cat /mnt/mmcblk0p1/config.txt | grep -i pi3
[PI3]
#dtoverlay=pi3-disable-bt
#dtoverlay=pi3-miniuart-bt
..and reboot. Note that the mini-uart is now routed to GPIO14/15 and the PL011 UART is now used for bluetooth communications.

Load the required extensions and modules:
Code: [Select]
$ tce-load -i firmware-rpi3-bt bluez
$ sudo modprobe hci_uart

$ lsmod
Module                  Size  Used by    Not tainted
hci_uart               19088  0
btbcm                   6309  1 hci_uart
bluetooth             338146  2 hci_uart,btbcm
...

$ dmesg
...
[  479.835339] Bluetooth: Core ver 2.21
[  479.835665] NET: Registered protocol family 31
[  479.835677] Bluetooth: HCI device and connection manager initialized
[  479.835857] Bluetooth: HCI socket layer initialized
[  479.835877] Bluetooth: L2CAP socket layer initialized
[  479.835915] Bluetooth: SCO socket layer initialized
[  479.839214] Bluetooth: HCI UART driver ver 2.3
[  479.839234] Bluetooth: HCI UART protocol H4 registered
[  479.839244] Bluetooth: HCI UART protocol Three-wire (H5) registered
[  479.839429] Bluetooth: HCI UART protocol BCM registered

Start the bluetooth daemon in debug mode to be able to see what is going on (the bluez init.d script can also be used):
Code: [Select]
$ sudo /usr/local/etc/init.d/dbus start
$ sudo /usr/local/lib/bluetooth/bluetoothd -nd
bluetoothd[2829]: Bluetooth daemon 5.43
...
bluetoothd[2829]: src/adapter.c:read_index_list_complete() Number of controllers: 0

Open a second terminal window and start the command interface:
Code: [Select]
$ hciattach /dev/ttyAMA0 bcm43xx 921600 noflow
bcm43xx_init
Flash firmware /usr/local/lib/firmware/brcm/BCM43430A1.hcd
Set Controller UART speed to 921600 bit/s
Device setup complete

$ bluetoothctl -a
[NEW] Controller B8:27:EB:D3:66:A9 Rpi [default]
Agent registered
[bluetooth]#

Power on bluetooth, search for availaable devices and pair:
Code: [Select]
[bluetooth]# power on
Changing power on succeeded
[CHG] Controller B8:27:EB:D3:66:A9 Powered: yes
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:EB:D3:66:A9 Discoverable: yes
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:D3:66:A9 Discovering: yes
[NEW] Device 00:1B:66:00:31:88 Sennheiser P
[CHG] Device 00:1B:66:00:31:88 Name: Sennheiser PXC310BT
[CHG] Device 00:1B:66:00:31:88 Alias: Sennheiser PXC310BT
[bluetooth]# pair 00:1B:66:00:31:88
Attempting to pair with 00:1B:66:00:31:88
[CHG] Device 00:1B:66:00:31:88 Connected: yes
[CHG] Device 00:1B:66:00:31:88 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 00:1B:66:00:31:88 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 00:1B:66:00:31:88 ServicesResolved: yes
[CHG] Device 00:1B:66:00:31:88 Paired: yes
Pairing successful
[CHG] Device 00:1B:66:00:31:88 ServicesResolved: no
[CHG] Device 00:1B:66:00:31:88 Connected: no
..note a pairing PIN will be requested where appropriate.
« Last Edit: March 15, 2017, 04:27:12 AM by Juanito »

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14790
Re: Howto: RPi3 built-in bluetooth
« Reply #1 on: March 20, 2017, 02:49:51 AM »
To use the RPi3 with bluetooth headphones:

Load the pavucontrol extension, start bluetooth as explained above and then start pulseaudio by opening pavucontrol.

In the bluetooth command interface, issue the following command:
Code: [Select]
[bluetooth]# connect 00:1B:66:00:31:88
Attempting to connect to 00:1B:66:00:31:88
[CHG] Device 00:1B:66:00:31:88 Connected: yes
Connection successful
[CHG] Device 00:1B:66:00:31:88 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 00:1B:66:00:31:88 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 00:1B:66:00:31:88 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 00:1B:66:00:31:88 ServicesResolved: yes
..this will set the bluetooth headphones as an a2dp sink and the bluetooth headphones will appear as an available output in pavucontrol.

You can play music to the headphones using the xmms-pulse extension (use <ctrl-p> to access the configuration menu and choose the pulseaudio output plugin). Note that you may need to use pavucontrol to switch the output stream from the 3.5mm jack to bluetooth.

Optionally the blueman extension can be used to pair and set the headphones as an audio sink.

The issue now is that the music pauses every few seconds for the RPi3 to catch up  :(


Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1240
Re: Howto: RPi3 built-in bluetooth
« Reply #2 on: August 19, 2017, 12:27:26 PM »
Bluetooth a2dp audio is working really nicely with the latest kernel using the bluez-alsa project rather than pluseaudio. 

Audio is playing nicely using squeezelite  (squeezelite -n pcp-bluetooth -o bluealsa:HCI=hci0,DEV=xx:xx:xx:xx:xx:xx,PROFILE=a2dp )

Looks like device paring information is saved in /var/lib/bluetooth

Is there a way to automatically connect devices on reboot using bluetoothctl.   It doesn't seem to support scripting.

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1240
Re: Howto: RPi3 built-in bluetooth
« Reply #3 on: September 25, 2017, 10:12:33 PM »
I was able to get all this working, ended up using dbus commands to setup the adapter and reconnect the device, since I'm not using a graphical environment.   Everything is working on both rpi3 and rpi ZeroW devices.

I updated to the latest Bluez and had to rebuild sbc to work on armv6.

The rpi3 and ZeroW have different settings when calling hciattach.   Below is the latest startup script from rpi, and is what Raspbian is using.

Code: [Select]
HCIATTACH=/usr/local/bin/hciattach
SERIAL=`grep Serial /proc/cpuinfo | cut -c19-`
B1=`echo $SERIAL | cut -c3-4`
B2=`echo $SERIAL | cut -c5-6`
B3=`echo $SERIAL | cut -c7-8`
BDADDR=`printf b8:27:eb:%02x:%02x:%02x $((0x$B1 ^ 0xaa)) $((0x$B2 ^ 0xaa)) $((0x$B3 ^ 0xaa))`

# Rpi3 and ZeroW Serial port
SERIAL_DEV="/dev/ttyAMA0"

echo "Bluetooth Hardware Address:$BDADDR"

if [ "$(cat /proc/device-tree/aliases/uart0)" = "$(cat /proc/device-tree/aliases/serial1)" ] ; t
hen
        if [ "$(wc -c /proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm\,pins | cut -f 1 -d '
')" = "16" ] ; then
                $HCIATTACH $SERIAL_DEV bcm43xx 3000000 flow - $BDADDR
        else
                $HCIATTACH $SERIAL_DEV bcm43xx 921600 noflow - $BDADDR
        fi
else
        $HCIATTACH $SERIAL_DEV bcm43xx 460800 noflow - $BDADDR
fi

Offline anchodotto

  • Newbie
  • *
  • Posts: 2
Re: Howto: RPi3 built-in bluetooth
« Reply #4 on: August 24, 2018, 10:29:59 PM »
Hi, Paul_123.  I'm interested in how you get to connect via DBUS.  I'm trying to connect to a paired device from bluetoothctl and i'm getting a

Code: [Select]
[bluetooth]# connect FC:A8:9A:B6:7D:6D
Attempting to connect to FC:A8:9A:B6:7D:6D
bluetoothd[1352]: src/agent.c:register_agent() agent :1.6
bluetoothd[1352]: src/device.c:connect_profiles() /org/blues/hci0/dev_FC_A8_9A_B6_7D_6D (all), client :1.6
bluetoothd[1352]: profiles/audio/a2dp.c:a2dp_sink_connect() path /org/blues/hci0/dev_FC_A8_9A_B6_7D_6D
bluetoothd[1352]: a2dp_sink profile connect failed for FC:A8:9A:B6:7D:6D: Protocol not avalable
Failed to connect: org.bluez.Error.Failed

Thanks in advance.

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1240
Re: Howto: RPi3 built-in bluetooth
« Reply #5 on: August 25, 2018, 11:39:04 PM »
bluetoothctl is using dbus as well, if you cannot get it connected with bluetoothctl, then direct dbus calls will not work either.

What is your full startup process.  The order in which things start is pretty critical.