HFP on Raspberry Pi


Update (Sep 7, 2017):
If you are using Raspbian Stretch, please refer here instead of steps below. This post below is for Raspbian Jessie.


The purpose of this post is to enable Bluetooth Handsfree Profile (HFP) on Raspberry Pi so that Raspberry Pi can act like as a handsfree speaker phone or a handsfree car kit. There is one thing needs to be mentioned before start. The original plan was to use Raspberry Pi3’s on-board Bluetooth chip for this project. Unfortunately, it didn’t work. It seems that the problem is related with the chip. So, we have to use a Bluetooth dongle instead.

Here is the list of contents of this post.

Contents:
– Hardware Prerequisites
– Software Prerequisites
– Steps
1. Setting Up
2. oFono
3. PulseAudio
4. Connect your Phone
5. Make a Call!
– Conclusion
– Reference

Let’s get started.

 

Hardware Prerequisites
Since Raspberry Pi doesn’t have microphone input by default, it need to be added. In this post, I’ll use a USB sound card with a 3.5 mm jack microphone.

Also, as mentioned above, we’ll use a Bluetooth dongle instead of on-board Bluetooth chip.

Raspberry Pi 3 B+
– Bluetooth dongle (Panda Bluetooth 4.0 USB Adapter)
– USB Sound Card (SYBA USB sound adapter / FG-UAUDV1-C119)
Microphone with 3.5 mm audio jack
– Speakers with 3.5 mm audio jack (e.g. Rumfo Mini Speaker)

 

Software Prerequisites
Below are the required versions to enable Bluetooth HFP according to PulseAudio release note.

– Bluez 5.0 (or later)
– PulseAudio 6.0 (or later)
– oFono 1.13 (or later)

 

Steps
1. Setting Up
1-1. Connect Bluetooth dongle and USB sound card with Raspberry Pi.
1-2. Connect the microphone and speakers to 3.5 mm audio jacks on the USB sound card.
1-3. Boot up Raspberry Pi Board. If it’s not set up yet, please refer this post.
1-4. As described, we don’t use on-board Bluetooth chip. So it needs to be disabled. To do that, open “/etc/modprobe.d/raspi-blacklist.conf”.

sudo nano /etc/modprobe.d/raspi-blacklist.conf

1-5. Add lines below and save.

blacklist btbcm
blacklist hci_uart

1-6. Then reboot the board.

sudo reboot

1-7. After reboot, check dmesg and make sure there is no Bluetooth-related errors.

dmesg | grep -i bluetooth

1-8. Also, make sure hci0 is UP.

hciconfig

The result should be like this:

hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:1A:7D:DA:71:08  ACL MTU: 310:10  SCO MTU: 64:8
    UP RUNNING
    RX bytes:622 acl:0 sco:0 events:38 errors:0
    TX bytes:1437 acl:0 sco:0 commands:38 errors:0

If it’s DOWN for some reason, then turn it up.

sudo hciconfig hci0 up

2. oFono
2-1. oFono is not installed by default, so let’s install it.

sudo apt-get install ofono -y

2-2. Make sure the version is 1.13 or later.

pi@raspberrypi:~ $ ofonod --version
1.15

2-2. Then, start the service.

sudo systemctl start ofono

You can check the service running like below.

systemctl -a |grep ofono
  ofono.service     loaded    active   running   LSB: oFono Mobile Telephony Daemon

3. PulseAudio
Since the preinstalled PulseAudio is 5.0, it needs to be updated to at least 6.0.

pi@raspberrypi:~ $ pulseaudio --version
pulseaudio 5.0

3-1. Uninstall the current PulseAudio.

sudo apt-get purge pulseaudio -y

3-2. Open “/etc/apt/sources.list”.

sudo nano /etc/apt/sources.list

3-3. Add the line below:

deb http://ftp.debian.org/debian jessie-backports main

3-4. Execute commands below:

gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553
gpg --keyserver pgpkeys.mit.edu --recv-key 7638D0442B90D010
gpg -a --export 8B48AD6246925553 | sudo apt-key add --
gpg -a --export 7638D0442B90D010 | sudo apt-key add --

3-5. Update the package list.

sudo apt-get update

3-6. Install PulseAudio and its Bluetooth module:

sudo apt-get -t jessie-backports install pulseaudio pulseaudio-module-bluetooth -y

3-7. Check the version and make sure it’s 6.0 or later.

$ pulseaudio --version
pulseaudio 7.1

3-8. Open “/etc/pulse/default.pa”.

sudo nano /etc/pulse/default.pa

3-9. Add “headset=ofono” on the line of ‘module-bluetooth-discover’.

load-module module-bluetooth-discover headset=ofono

3-10. To enable echo canceling, add the lines below:

### Echo cancel and noise reduction
.ifexists module-echo-cancel.so
load-module module-echo-cancel aec_method=webrtc source_name=ec_out sink_name=ec_ref
set-default-source ec_out
set-default-sink ec_ref
.endif

3-11. Start PulseAudio.

pulseaudio --start

4. Connect your Phone
4-1. Launch bluetoothctl.

bluetoothctl

4-2. Then, input below commands.

power on
agent on
default-agent

4-3. Start searching your phone. Make sure your phone is discoverable.

scan on

4-4. After detecting your phone, turn scan off.

scan off

devices command shows a list of found devices.

[bluetooth]# devices
Device XX:XX:XX:XX:XX:XX Galaxy S7

4-5. Then, pair, trust and connect to your phone by specifying the Bluetooth device address (in this case it’s “XX:XX:XX:XX:XX:XX”, please replace it wity your device’s address).

pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX

If everything is ok, you should see “successful” at the end of the result for each command like below.

[bluetooth]# pair XX:XX:XX:XX:XX:XX
...
Pairing successful
[bluetooth]# trust XX:XX:XX:XX:XX:XX
...
trust succeeded
[bluetooth]# connect XX:XX:XX:XX:XX:XX
...
Connection successful

5. Make a Call!
To make a call, we’ll use oFono test scripts which is included in the source code.
5-1. First, download the source and extruct.

wget https://www.kernel.org/pub/linux/network/ofono/ofono-1.15.tar.gz
tar -xzvf ofono-1.15.tar.gz

5-2. Then, you can make a call by:

python ./test/dial-number 1234567890

If you want to end the call, then execute:

python ./test/hangup-active

 

Conclusion
Thanks for all the forums and articles I referred, now HFP is working on Raspberry Pi!
Next, I’m planning to apply echo canceling and ambient noise suppression, maybe equalizer.


Update (June 22, 2017):
Added one step (step 3-10) to enable echo cancellation. It seems that ‘module-echo-cancel’ does not only echo cancellation but ambient noise suppression. I haven’t check the noise suppression yet but I think the echo cancellation is working well based on my brief testing.


Update (May 2, 2018):
The steps to enable equalizer on HFP sending audio is newly posted.


 

Reference
– Connect Bluetooth Headset To Raspberry Pi 3 (A2DP & HSP)
http://youness.net/raspberry-pi/bluetooth-headset-raspberry-pi-3-ad2p-hsp

– PulseAudio 6.0 Release Notes –freedesktop.org
https://www.freedesktop.org/wiki/Software/PulseAudio/Notes/6.0/

– PulseAudio Documentation –freedesktop.org
https://freedesktop.org/wiki/Software/PulseAudio/Documentation/

– module-echo-cancel –freedesktop.org
https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/Modules/#index45h3

– Pulseaudio echo cancellation — Debian User Forums
http://forums.debian.net/viewtopic.php?f=16&t=128306

 

Installing the Latest Qt Creator on Ubuntu Linux

This is a step-by-step procedure to install the latest official release of Qt Creator onto Ubuntu Linux PC. Qt Creator can be installed by “apt-get” command, however in that case you may not get the latest version. In this post we’ll use the online installer so that we can get the latest version.

For the reference, bellow are the latest version as of 6/2/2017.
– Qt Creator version : 4.3.0
– Ubuntu version : 16.04.02 (LTS)


Update (April 27, 2018):
My ubuntu got updated and I reinstalled Qt. The exact same steps still work. Now the versions are:
– Qt Creator 4.6.0
– Ubuntu 18.04 (LTS)


 

Prerequisites:
– 64-bit Ubuntu Installed PC
(I’m not sure why but I was not able to get the latest version when I was installing on 32-bit Ubuntu. So, in this post, I use 64-bit Ubuntu PC.)

 

Steps
1. Install required packages.

sudo apt-get update
sudo apt-get install build-essential libgl1-mesa-dev -y

2. Download the online installer.

wget http://download.qt.io/official_releases/online_installers/qt-unified-linux-x64-online.run

3. Add execute permission.

chmod a+x qt-unified-linux-x64-online.run

4. Run the installer.

./qt-unified-linux-x64-online.run

5. Follow the wizard and complete the installation.

6. Launch Qt Creator.

Qt Creator should automatically configure compilers, debuggers, etc for the PC environment. Now you can run examples from “Welcome” tab or create your a new project from “File” > “New File or Project…”!

 

References
Getting Started With Qt and Qt Creator on Linux
http://www.ics.com/blog/getting-started-qt-and-qt-creator-linux

Online installer of Qt Creator official releases
http://download.qt.io/official_releases/online_installers

 

Setting up Wi-Fi on Raspberry Pi

You can setup Wi-Fi on GUI, or you can configure it from your console. Here is how to do it from the console.

Brief steps:
1. Registering Wi-Fi Network
2. Enabling Automatic Connection
3. Connecting to Wi-Fi network

Let’s get started.

 

1. Registering Wi-Fi Network
1-1. Open /etc/wpa_supplicant/wpa_supplicant.conf

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

1-2. Add last four lines below at the end of the file. Replace <SSID> and <PASSWORD> to appropriate strings for your Wi-Fi network.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
 
network={
    ssid="<SSID>"
    psk="<PASSWORD>"
    key_mgmt=WPA-PSK
}

 

2. Enabling Automatic Connection
2-1. Open “/etc/network/interfaces”.

sudo nano /etc/network/interfaces

2-2. add “auto wlan0” line like below:

auto lo
 
iface lo inet loopback
iface eth0 inet dhcp
 
auto wlan0
allow-hotplug wlan0
...

 

3. Connecting to Wi-Fi Network
3-1. Disable Wi-Fi interface

sudo ifdown wlan0

3-2. Enable Wi-Fi interface

sudo ifup wlan0

* Alternatively, you can reboot the system Instead of step 3-1 and 3-2, to establish Wi-Fi connection.

sudo reboot

3-3. Check assigned IP address

ifconfig wlan0

if everything is ok, you can see a line like below:

inet addr:192.168.51.102 Bcast:129.168.51.255 Mask:255.255.254.0

Changing Keyboard Layout on Raspberry Pi

 

By default, the keyboard layout is set for UK on Raspbian OS. So, if your keyboard is not UK layout, you may want to change it. Here is the steps.

1. Launch Terminal if you are on  graphical desktop mode.

2. Open “/etc/default/keyboard” with nano (or any other editor you prefer).

sudo nano /etc/default/keyboard

The file should be like this:

# KEYBOARD CONFIGURATION FILE

# Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="gb"
XKBVARIANT=""
XKBOPTIONS=""

BACKSPACE="guess"

3. Edit XKBLAYOUT line. In case your keyboard is US layout, changed to “us”.

XKBLAYOUT="us"

4. Reboot the board.

sudo reboot

 

Setting & Booting up Raspberry Pi

This post shows steps to setup and boot up Raspberry Pi board with Raspbian, the recommended OS [1]  .

Here is the list of contents of this post.

Contents
– Prerequisites
– Steps
1. OS Image Preparation
2. Booting up Raspberry Pi board
– What’s next?
– Reference

 

Prerequisites (parentheses indicate my environment)

 

Steps
1. OS Image Preparation
Raspberry Pi loads the OS image from micro SD card, so let’s prepare the micro SC card.

1-1. Download a zip file of the latest Raspbian OS images from the official site to your PC.

1-2. Download Etcher and install it on your PC.

1-3. Insert micro SD card into PC and launch Etcher.

1-4. Select the zip file. Etcher should automatically select the drive if it detects the SD card.

1-5. Click on “Flash!” button and wait.

1-6. After complete the write process, safely eject the micro SD card. Your micro SD card is now ready.

 

2. Booting up Raspberry Pi board
2-1. Connect keyboard, mouse and display to Raspberry Pi.

2-2. Insert the micro SD card.

2-3. Connect the power supply cable and turn it on.

If everything goes well, Raspberry Pi board boots up.

 

What’s next? 
Now Raspberry Pi is up, from here is totally up to you, but before starting your project, here are some suggestions.

– Keyboard layout change
The keyboard layout is set for UK by default. So, you may want to change it first.

Wi-Fi setup
Raspberry Pi3 has on-board wi-fi chip and it makes it easy to connect to the Internet and/or your private network.

Enable SSH
By enabling SSH, you can access Raspberry Pi’s console from your remote PC.

Free up SD card space
SD card storage is relatively small and you may run out of the free space as your project goes on. Here are some tips to free up your SD card space.

Update software
Whatever you gonna do with Raspberry Pi board, it’s better to have the latest software. Enter command below and get the package list and software updated. For detail, please see the official documentation.

sudo apt-get update && sudo apt-get upgrade -y

– Disable Screen Sleep
By default the screen goes to sleep after some minutes (10 minutes?) of inactivity. It can be disabled by editing configuration file.

 

References
[1] Raspbian – Raspberry Pi Documentation
[2] Raspbian Download Page