AndrewKMitchell.com The online home of Andrew Kenton Mitchell

Using a Bluetooth Headset as an audio (input & output) device with Linux

Since I face physical challenges myself, I find it rewarding to help those with similar needs when I can.  Today I was asked to help a young lady setup a Bluetooth Headset as an audio input device (microphone), as well as an audio output device (speaker).  This way the headset could be used in conjunction with the user’s voice control system.

The paring of the device was pretty simple.  Getting the Fedora 11 system to see the headset as an audio device…  That was a bit more tricky.

So, once I figured it out, I wrote this simple bash script to make things much easier for the user.

#!/bin/bash
#
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
echo "Please place your Bluetooth audio device into pairing mode, then press "
read
echo "Scanning for Bluetooth audio device..."
scan=`hcitool scan | sed 's/^\s*//' | grep -v 'Scanning'`
line=$scan
if [[ "`echo "$scan" | wc -l`" -gt "1" ]]; then
echo "select device:"
save_ifs=$IFS
IFS="
"
select line in $scan; do
#echo got $line
break
done
IFS=$save_ifs
fi
BT_BDADDR=`echo $line | awk '{print $1}'`
[[ -z "$BT_BDADDR" ]] && echo no bluetooth device found && exit
echo "Selected bluetooth device: $BT_BDADDR"
# => e.g., 00:11:22:33:44:55

echo “Writing new ~.asoundrc…”
cat >> ~/.asoundrc <
pcm.bt_audioraw {
type bluetooth
device $BT_BDADDR
profile “auto”
}
pcm.bt_audio {
type plug
slave.pcm “bt_audioraw”
hint {
show on
description “Bluetooth audio device”
}
}
EOD

echo “Getting default adapter…”
_BT_ADAPTER=`dbus-send –system –print-reply –dest=org.bluez / \
org.bluez.Manager.DefaultAdapter|awk ‘/object path/ {print $3}’`
BT_ADAPTER=${_BT_ADAPTER//\”/}
echo “$BT_ADAPTER”

echo “Removing any stale Bluetooth audio device:”
_OLD_BT_DEVICE=`dbus-send –system –print-reply –dest=org.bluez $BT_ADAPTER \
org.bluez.Adapter.FindDevice string:$BT_BDADDR|awk ‘/object path/ {print $3}’`
OLD_BT_DEVICE=${_OLD_BT_DEVICE//\”/}
dbus-send –system –print-reply –dest=org.bluez $BT_ADAPTER \
org.bluez.Adapter.RemoveDevice objpath:$OLD_BT_DEVICE

echo “Creating Bluetooth audio device:”
_BT_DEVICE=`dbus-send –system –print-reply –dest=org.bluez $BT_ADAPTER \
org.bluez.Adapter.CreateDevice string:$BT_BDADDR|awk ‘/object path/ {print $3}’`
BT_DEVICE=${_BT_DEVICE//\”/}
echo “$BT_DEVICE”

# optional: echo “Connecting — BlueZ applet will prompt for pin…”
# optional: dbus-send –system –print-reply –dest=org.bluez \
# optional: $BT_DEVICE org.bluez.AudioSink.Connect
#
# NOTE: if the above dbus-send is NOT executed, then a pairing
# request for an A2DP device will be initiated the first time
# the ALSA virtual device bt_audio is used. Otherwise, when
# “AudioSink.Connect” is executed, a pairing request to the device
# will be initiated immediately and then ALSA will subsequently use
# this paired connection for playing sound through bt_audio. Executing
# “AudioSink.Connect” is useful for preparing devices ahead of time
# to avoid undesired delays and to avoid pairing mode timeouts.
#
# Also note that a device conforming to the headset profile (HSP)
# does not support “AudioSink.Connect”. However, this script (without
# running “AudioSink.Connect”) will still work because BlueZ will
# request pairing when “Adapter.CreateDevice” is executed for HSP
# devices. You can check out your device’s specific capabilities
# using sdptool.

Using the script is fairly basic.

  1. Copy the code and save it on your Linux box.
  2. Make sure the owner and group assigned to the script are those of the user executing it.
  3. Set the file permission to 755 (rwxr-xr-x).
  4. Place your headset in paring  mode.
  5. Execute the script and press ‘Enter’ when prompted.
  6. Watch for alerts on your screen because a security code maybe required.  If so, it is generally 0000.

Once the device has been paired using the script, the next step is to activate it as an audio device on your system.  Here we will assume your system uses the PulseAudio Sound Server.

First, ‘Right-Click’ the PulseAudio volume icon usually located on one of your Xwindows Panels.  Then choose ‘Sound Preferences’.

If you want to set the headset as your input device, follow the example shown below:

bt-pa-input

If you want to set the headset as your output device, follow the example shown below:

bt-pa-output

Once you have selected the device(s) you want, be sure you click the ‘Close’ button so your choices are activated.

When you turn off your headset. your choices are most often reset.  If you want to re-enable you headset in the future, repeat step 5, then reselect the devices as shown above.

Your email is never shared.
Required fields are marked *