back to notes

2x guides: CHIP-into-BT-Audio-Receiver + connecting-to-BT-speaker

source: https://bbs.nextthing.co/t/basic-guide-to-turning-chip-into-a-bluetooth-audio-receiver-audio-sink/2187?u=fordsfords

Basic guide to turning CHIP into a Bluetooth Audio Receiver (Audio Sink)

If you want to stream audio to your CHIP via bluetooth, and play it out CHIP’s 1/8" audio out jack, here’s a basic guide to using some commands in the terminal. This is not an ideal situation. The ideal situation is that you’d press a button of some sort that would make all this work, but this is the hidden rabbit in that magic!

First, install some dependencies. The bluez-tools package adds some convenient ways of controlling Bluetooth from commandline, so you don’t have to rely on the interactive shell of bluetoothctl. Since bluez 5 isn’t directly compatible with ALSA, we’ll need to rely on pulseaudio to make the audio connections.

$ sudo apt-get install -y bluez-tools pulseaudio-module-bluetooth

Now configure bluez audio by editing the bluez audio configuration file: sudo nano /etc/bluetooth/audio.conf so it has the content:

[General] 
Disable=Socket Enable=Media,Source,Sink,Gateway

Now, restart bluez and re-load the PulseAudio module that routes bluetooth audio: sudo systemctl restart bluetooth pactl unload-module module-bluetooth-discover pactl load-module module-bluetooth-discover

A quick confirmation will let you know that things are ok:

bt-adapter --info

where you should see items “AudioSink” and “AudioSource” in the UUID array: UUIDs: [PnPInformation, 00001800-0000-1000-8000-00805f9b34fb, 00001801-0000-1000-8000-00805f9b34fb, AVRemoteControl, AVRemoteControlTarget, 00001133-0000-1000-8000-00805f9b34fb, 00001132-0000-1000-8000-00805f9b34fb, PhoneBookAccess, IrMCSync, OBEXFileTransfer, OBEXObjectPush,00005005-0000-1000-8000-0002ee000001, AudioSource, AudioSink]

This PulseAudio module will switch to bluetooth audio when connected:

pactl load-module module-switch-on-connect

Now that that setup is done, we can move onto the good part: connecting and disconnecting!

Make the bluetooth module powered and discoverable with some eponymous commands: bt-adapter --set Powered 1 bt-adapter --set Discoverable 1

You’ll need to tell bluez if a PIN is required to pair. In this case it’s not (for keyboards, you generally need to enter in a unique PIN to pair). bt-agent -c NoInputNoOutput &

Once paired, you’ll need to ctl-z to suspend the agent, then type bg to put it in the background, or just suspend it entirely with CTRL-C You can confirm that there is a pairing and a Bluetooth audio source with a pactl command: pactl list short sources

which gives output like so: 0 alsaoutput.platform-1c22c00.codec.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz IDLE 1 alsainput.platform-1c22c00.codec.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED 3 bluezsource.F0E77E344721 module-bluez5-device.c s16le 2ch 44100Hz RUNNING

Play some audio on the source machine, and you’llGuide to connecting to a Bluetooth Speaker hear some lovely sound coming from CHIP’s audio jack. Hook up headphones or connect to an amplifier and speakers for even bigger sound!

If you are running a headless OS (no desktop manager) on CHIP, you’ll need to follow the steps in the “Headless” section in this guide 37

Special thanks to @jason for the real legwork.


source: https://bbs.nextthing.co/t/guide-to-connecting-to-a-bluetooth-speaker/4684

Guide to connecting to a Bluetooth Speaker

I noticed there’s a bit of a gap in how-to for this fairly common and desirable activity, so here it goes. CHIP with Desktop

If you are running a GUI-version of CHIP (including PocketCHIP), this is pretty straightforward. Just install some supporting packages, find the speaker, connect, pair, and trust (so connecting is easy the next time). I’ll just put all the commands for terminal in order here, and explain afterwards:

sudo apt-get update && sudo apt-get install -y bluez-tools pulseaudio-module-bluetooth unzip wget

bt-adapter -d

bt-device --set <MAC> Trusted 1
{ sleep 1; echo "connect <MAC>"; sleep 10; } | bluetoothctl
pactl load-module module-bluetooth-discover && pactl load-module    module-bluetooth-policy && pactl load-module module-switch-on-connect
pactl list short sinks

Some notes:

I’ve added unzip and wget to the packages to install, but they are optional.

You could do all the discovery, pair, connection, and trust using the GUI in the CHIP desktop. I think it’s worth including the command line stuff because we’ll have to go there eventually anyway.

The bt-adapter -d will output info about all nearby bluetooth devices. You’ll need to find your speaker among them. You want the MAC address, which is in brackets. A sample output for a single speaker is

chip@chip:~$ bt-adapter -d
Searching...
[FC:58:FA:BA:91:76]
Name: iLuv Aud Mini
Alias: iLuv Aud Mini
Address: FC:58:FA:BA:91:76
Icon: audio-card
Class: 0x260404
LegacyPairing: 0
Paired: 0
RSSI: -38

In this example, you’ll need FC:58:FA:BA:91:76 for your MAC address.

You’ll notice the connection is handled by a echo to bluetoothctl, rather than using bt-device. We discovered that, for whatever reason, bt-device fails to create the audio sink needed for pulse audio. You can confirm the existence of the sink with pactl list short sinks.

Now you can play some music with mpg123 <somefile.mp3> or aplay <somefile.wav>, or use SunVox or mpd or whatever.

Headless!

If you have re-flashed CHIP a headless OS (no desktop or window manager), there are some configurations you need to perform that are otherwise handled by using a desktop manager.

First, edit the file

sudo nano /etc/pulse/system.pa

so the line load-module module-native-protocol-unix has permissions appended (CTL-w in nano to find some words in the text): load-module module-native-protocol-unix auth-anonymous=1

You’ll also need to add a configuration file:

sudo nano /etc/dbus-1/system.d/pulseaudio-bluetooth.conf

with the text at this gist 205. If you want, you can get it with wget:

cd /etc/dbus-1/system.d/
sudo wget https://gist.github.com/nyboer/e1af648ebcaacba7ea962463867b9486/archive/095230b610fe36fc7670fddd4f24d32dbee27acc.zip
sudo unzip -j 095230b610fe36fc7670fddd4f24d32dbee27acc.zip e1af648ebcaacba7ea962463867b9486-095230b610fe36fc7670fddd4f24d32dbee27acc/pulseaudio-bluetooth.conf -d ./

Then reboot CHIP!

When CHIP has restarted use this very long command (several commands in one to make copy/paste easy):

sudo pulseaudio -D --realtime=false --high-priority=false --system --disallow-module-loading=false && pactl load-module module-bluetooth-discover && pactl load-module module-bluetooth-policy && pactl load-module module-switch-on-connect

Now you can go back to the top of this post and perform the steps for connecting to the speaker.

Many thanks to @jason for the guidance!



last updated july 2018