Transclusion: Debian: CL-SOM-iMX7: Bluetooth

From Compulab Mediawiki
Revision as of 14:16, 8 May 2017 by Grinberg (talk | contribs) (Bluez5 & PulseAudio)
Jump to: navigation, search

CL-SOM-iMX7 features Bluetooth 4.1 BLE interface, implemented with a TI WiLink8 Combo module.

Host Controller Interface (HCI) Initialization

  • HCI requires no user interaction for being configured. The driver gets loaded automatically unless this option has been disabled by putting a black list rule for this device.

Make sure that the Bluetooth driver is loaded:

root@cl-rootfs:~# lsmod | grep btwilink
btwilink                3822  0

The Bluetooth driver can be loaded manually:

root@cl-rootfs:~# modprobe btwilink
  • HCI device configuration utility can be run to retrieve detailed information about the Bluetooth interfaces:
root@cl-rootfs:~# hciconfig hci0 -a
hci0:   Type: BR/EDR  Bus: UART
        BD Address: 74:DA:EA:F9:CE:E3  ACL MTU: 1021:6  SCO MTU: 180:4
        UP RUNNING 
        RX bytes:636 acl:0 sco:0 events:39 errors:0
        TX bytes:1234 acl:0 sco:0 commands:39 errors:0
        Features: 0xff 0xfe 0x2d 0xfe 0xdb 0xff 0x7b 0x87
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
        Link policy: RSWITCH HOLD SNIFF 
        Link mode: SLAVE ACCEPT 
        Name: 'BlueZ 5.36'
        Class: 0x000000
        Service Classes: Unspecified
        Device Class: Miscellaneous, 
        HCI Version: 4.1 (0x7)  Revision: 0x0
        LMP Version: 4.1 (0x7)  Subversion: 0xac03
        Manufacturer: Texas Instruments Inc. (13)
  • If the HCI device is not running, use the below command to enable the HCI device:
root@cl-rootfs:~# hciconfig hci0 up
  • HCI Inquire remote devices.
root@cl-rootfs:~# hcitool scan
Scanning ...

Bluez5 & PulseAudio

Debian stretch/sid is using BlueZ 5 and PluseAudio 5, which are fairly new.
BlueZ 5 dropped support for alsa, so the solution for now is to use PulseAudio.
PulseAudio 5 only supports the A2DP profile and not HSP/HFP.

  • Software to install pulseaudio, pulseaudio-module-bluetooth
Admolition note.png debian-image.tar.bz2 has all these packages pre-installed. Continue to the Start PulseAudio section if you use this root filesystem.
Install and configure PulseAudio
root@cl-rootfs:~# apt-get install --no-install-recommends pulseaudio pulseaudio-module-bluetooth
  • Create a systemd service for running pulseaudio as the pulse user.
root@cl-rootfs:~# cat << eof > /etc/systemd/system/pulseaudio.service
[Unit]
Description=Pulse Audio

[Service]
Type=simple
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm

[Install]
WantedBy=multi-user.target
eof

root@cl-rootfs:~# chown pulse:pulse /etc/systemd/system/pulseaudio.service
  • Create a dbus configuration file for running pulseaudio. Give the pulse user permission to use Bluetooth.
root@cl-rootfs:~# cat << eof > /etc/dbus-1/system.d/pulseaudio-bluetooth.conf
<busconfig>

  <policy user="pulse">
    <allow send_destination="org.bluez"/>
  </policy>

</busconfig>
eof

root@cl-rootfs:~# chmod 0666 /etc/dbus-1/system.d/pulseaudio-bluetooth.conf
  • Paste the following lines to the end of /etc/pulse/system.pa:
root@cl-rootfs:~# cat << eof >> /etc/pulse/system.pa
### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif

.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
eof
  • Create /var/run/pulse/.config/pulse directory. Change its ownership.
root@cl-rootfs:~# mkdir -p /var/run/pulse/.config/pulse
root@cl-rootfs:~# chown -R pulse:pulse /var/run/pulse
  • Make the root belong to the pulse-access,audio groups
root@cl-rootfs:~# usermod -a -G pulse-access,audio root
Start PulseAudio
  • Start PulseAudio service
root@cl-rootfs:~# systemctl daemon-reload
root@cl-rootfs:~# systemctl start pulseaudio.service
  • Make sure that the service is active, running and reports on no errors.
root@cl-rootfs:~# systemctl status pulseaudio.service
   Loaded: loaded (/etc/systemd/system/pulseaudio.service; disabled)
   Active: active (running) since Mon 2015-07-20 12:00:13 UTC; 1min 34s ago
 Main PID: 805 (pulseaudio)
   CGroup: /system.slice/pulseaudio.service
           └─805 /usr/bin/pulseaudio --system --disallow-exit --disable-shm
Bluez5 pairing

PulseAudio 5.x supports A2DP per default.
Make sure the following packages are installed: pulseaudio pulseaudio-module-bluetooth pulseaudio-utils, bluez, bluez-tools.

Admolition note.png Without pulseaudio-module-bluetooth you won't be able to connect after the next pairing and you won't get any usable error messages.
  • Start the Bluetooth system:
root@cl-rootfs:~# systemctl start bluetooth

Now we can use the bluetoothctl command line utility to pair and connect. Run

root@cl-rootfs:~# bluetoothctl
[bluetooth]#

to be greeted by its internal command prompt.Then enter:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# default-agent
[bluetooth]# scan 

Now make sure that your headset is in pairing mode. It should be discovered shortly. For example,

[NEW] Device 00:07:A4:F2:B3:CB Motorola HT820

shows a device that calls itself "Motorola HT820" and has MAC address 00:07:A4:F2:B3:CB. We will now use that MAC address to initiate the pairing:

[bluetooth]# pair 00:07:A4:F2:B3:CB

After pairing, you also need to explicitly connect the device:

[bluetooth]# connect 00:07:A4:F2:B3:CB

If everything works correctly, you now have a separate output device in PulseAudio.
You can now disable scanning again and exit the program:

[bluetooth]# scan off
[bluetooth]# exit
Playback using PulseAudio
Admolition note.png Next examples assume that the device mac address is 00_07_A4_F2_B3_CB
  • Show the PulseAudio device
root@cl-rootfs:~# pactl list cards  | awk '/00_07_A4_F2_B3_CB/' RS=""
Card #1                                                                                                                                                                     
        Name: bluez_card.00_07_A4_F2_B3_CB                                                                                                                                  
        Driver: module-bluez5-device.c                                                                                                                                      
        Owner Module: 13                                                                                                                                                    
        Properties:                                                                                                                                                         
                device.description = "Motorola HT820"                                                                                                                       
                device.string = "00:07:A4:F2:B3:CB"                                                                                                                         
                device.api = "bluez"                                                                                                                                        
                device.class = "sound"                                                                                                                                      
                device.bus = "bluetooth"                                                                                                                                    
                device.form_factor = "headset"                                                                                                                              
                bluez.path = "/org/bluez/hci0/dev_00_07_A4_F2_B3_CB"                                                                                                        
                bluez.class = "0x240404"                                                                                                                                    
                bluez.alias = "Motorola HT820"                                                                                                                              
                device.icon_name = "audio-headset-bluetooth"                                                                                                                
                device.intended_roles = "phone"                                                                                                                             
        Profiles:                                                                                                                                                           
                headset_head_unit: Headset Head Unit (HSP/HFP) (sinks: 1, sources: 1, priority: 20, available: no)
                a2dp_sink: High Fidelity Playback (A2DP Sink) (sinks: 1, sources: 0, priority: 10, available: yes)
                off: Off (sinks: 0, sources: 0, priority: 0, available: yes)
        Active Profile: a2dp_sink
        Ports:
                headset-output: Headset (priority: 0, latency offset: 0 usec)
                        Part of profile(s): headset_head_unit, a2dp_sink
                headset-input: Headset (priority: 0, latency offset: 0 usec, not available)
                        Part of profile(s): headset_head_unit
  • Make sure the active card profile is a2dp_sink. Otherwise switch to it:
# pactl set-card-profile 1 a2dp_sink
  • Show the PulseAudio sink
root@cl-rootfs:~# pactl list sinks | awk '/00_07_A4_F2_B3_CB/' RS=""
Sink #1
        State: SUSPENDED
        Name: bluez_sink.00_07_A4_F2_B3_CB
        Description: Motorola HT820
        Driver: module-bluez5-device.c
        Sample Specification: s16le 2ch 48000Hz
        Channel Map: front-left,front-right
        Owner Module: 13
        Mute: no
        Volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
                balance 0.00
        Base Volume: 65536 / 100% / 0.00 dB
        Monitor Source: bluez_sink.00_07_A4_F2_B3_CB.monitor
        Latency: 0 usec, configured 0 usec
        Flags: HARDWARE DECIBEL_VOLUME LATENCY 
        Properties:
                bluetooth.protocol = "a2dp_sink"
                device.description = "Motorola HT820"
                device.string = "00:07:A4:F2:B3:CB"
                device.api = "bluez"
                device.class = "sound"
                device.bus = "bluetooth"
                device.form_factor = "headset"
                bluez.path = "/org/bluez/hci0/dev_00_07_A4_F2_B3_CB"
                bluez.class = "0x240404"
                bluez.alias = "Motorola HT820"
                device.icon_name = "audio-headset-bluetooth"
                device.intended_roles = "phone"
        Ports:
                headset-output: Headset (priority: 0)
        Active Port: headset-output
        Formats:
                pcm
  • Play sound using paplay. Example:
root@cl-rootfs:~# paplay -d bluez_sink.00_07_A4_F2_B3_CB /path/to/<audio-file.wav>