Bluetooth

From www.chip-community.org
Jump to: navigation, search

Pairing a Device

If you want to initiate the pairing request from the other device, use the following steps:

  • From a command shell:
$ sudo bluetoothctl
[bluetooth]# power on
[bluetooth]# discoverable on
  • Scan for the bluetooth device named "chip" on the other device, then request pairing. Upon succes, the following will be displayed in the shell:

[CHG] Device xx:xx:xx:xx:xx:xx Connected: yes

  • Exit from the Bluetooth API
[bluetooth]# exit

Changing the Name of CHIP's Bluetooth Device

If you want to change the name that is broadcast by C.H.I.P to other Bluetooth devices for pairing, edit the file /etc/machine-info (create it if necessary) and add the following line:

PRETTY_HOSTNAME=<name>

then restart the bluetooth service:

$ sudo service bluetooth restart

How To Connect to a Bluetooth Speaker

from a forum post by pnyboer Jun 2016

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 an 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. 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!

Editors note: The original forum thread continues on and some users report no success with the preceding instructions. Forum user Tfegleek created another thread with their PocketCHIP setup.

PocketCHIP

I will show you how to connect its Bluetooth speaker to the pocketchip. I am French so forgive if I make mistakes

  1. sudo apt-get update
  2. sudo apt-get install pulseaudio-module-bluetooth
  3. sudo nano /etc/bluetooth/audio.conf
    And write in:
    [General]
    Enable=Source,Sink,Média,Socket
  4. Enter these commands
bluetoothctl
power on
agent on
scan on
pair adress:ip example pair 00:00:00:00:00:00
connect your:bluetooth:adress example connect 00:00:00:00:00:00
quit

There, if you launch an application no sound will come out. So write

pactl set-default-sink bluez_sink.your_bluetooth_adressexamplepactl set-default-sink bluez_sink.00_00_00_00_00_00

Attention if you have a software like pico 8 open you will have to leave it with Ctrl + q and reopen it

Stack API

If you want to develop an application that uses Bluetooth connectivity, you must install the bluez bluetooth stack API libraries and utilities.

$ sudo apt-get install libbluetooth-dev
$ sudo apt-get install bluez-tools

Documentation is, unfortunately, rather scant. For C++ application, you can have a look at the source code and documentation (whatever exists) for the bluez library by fetching it from the following GIT repository:

$ git clone git://git.kernel.org/pub/scm/bluetooth/bluez.git bluez

It is far easier to interact with the bluetooth service using the bluez tools via a script, a system(2) call, or a popen(3) call. The following man pages will prove useful: