How to Connect Bluetooth Headphones to Raspberry Pi

How to Connect Bluetooth Headphones to Raspberry Pi

Introduction

In the previous section, we discussed how to connect a camera. This time, we will learn how to connect Bluetooth devices to the Raspberry Pi.

Preparation

  1. One Raspberry Pi zero wh
  2. One Bluetooth headphone

Bluetooth Connection

1. Install Software

sudo apt-get install pulseaudio pulseaudio-module-bluetooth bluez  bluez-firmware

PulseAudio is a sound server, a background process that accepts sound input from one or more sources (processes or input devices) and then redirects the sound to one or more sinks (sound cards, remote network PulseAudio services, or other processes).

apt-get install mplayer

MPlayer is a lightweight player, small in size, quick to start, and consumes very little memory and CPU.

2. Add Authorized Users

adduser root pulse-access    
adduser pi pulse-access

3. Modify Configuration Files

a. /etc/dbus-1/system.d/bluetooth.conf

vim /etc/dbus-1/system.d/bluetooth.conf

Add the following content previously

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

b. /etc/pulse/system.pa

vim /etc/pulse/system.pa

Add the following content at the end of the file

### Bluetooth Support
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
load-module module-bluetooth-policy
.endif

c. /etc/pulse/default.pa

vim /etc/pulse/default.pa 

Add auth-anonymous=1 after the original load-module module-native-protocol-tcp.

load-module module-native-protocol-tcp auth-anonymous=1

d. Create a new pulseaudio.service file

vim /etc/systemd/system/pulseaudio.service

Write the following content:

[Unit]
Description=Pulse Audio
[Service]
Type=simple
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm --exit-idle-time=-1
[Install]
WantedBy=multi-user.target

4. Start the Service

a. Reload the service

systemctl daemon-reload

b. Restart bluetooth service

systemctl restart bluetooth.service

c. Start pulseaudio service

systemctl start pulseaudio.service 

d. Enable pulseaudio service to start at boot

systemctl enable pulseaudio.service

e. Check bluetooth process status

systemctl status bluetooth.service

5. Connect Bluetooth Headphones

Enter the Bluetooth console

sudo bluetoothctl
[bluetooth]$ power on # Turn on
[bluetooth]$ agent on # Agent
[bluetooth]$ default-agent # Default agent
[bluetooth]$ scan on # Scan for nearby Bluetooth devices
[NEW] Device 70:1C:E7:69:C0:DE huawei
[bluetooth]$ pair 70:1C:E7:69:C0:DE # Pairing (followed by Bluetooth address)
[bluetooth]$ trust 70:1C:E7:69:C0:DE # Trust the Bluetooth device
[bluetooth]$ connect 70:1C:E7:69:C0:DE # Connect Bluetooth device
[bluetooth]$ scan off # Turn off scanning
[bluetooth]$ exit # Exit
[bluetooth]$ power off # Disconnect

6. Play Music with MPlayer

Play music

mplayer running.mp3

Control the volume

mplayer -af volume=-10 *.mp3
# The range of volume can be from -200 to +60, -200 is mute, and +60 is noise

mplayer -softvol -softvol-max 10 *.mp3
# First enable soft volume with -softvol, then limit the maximum volume of the soft volume card with -softvol-max. Here we set the maximum volume to 10% of the default volume, which will be very quiet.

mplayer -af volume=-10 -softvol -softvol-max 200 *.mp3
# We combine the first two methods. This way, we can make the default volume not equal to 100%, and it will take effect immediately upon startup.

Control the volume of the Raspberry Pi system

alsamixer
How to Connect Bluetooth Headphones to Raspberry Pi

References

Installing Bluetooth speakers and Mopidy on Raspberry Pi raspberrypi3 (https://bbs.hassbian.com/thread-3404-1-1.html)

Detailed explanation of MPlayer volume control (https://blog.csdn.net/newnewman80/article/details/6177949?locationNum=4)

Leave a Comment