Bluetooth made easy

From Funtoo
Revision as of 06:25, 6 April 2014 by Sputnik (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Kernel Support

You must have bluetooth support in your kernel if you are going to use bluetooth. Enable the following in your kernel config:

Networking support →
  Bluetooth subsystem support →
   <m> RFCOMM protocol support
     <y> RFCOMM tty support
   <m> BNEP protocol support
     <y> Multicast filter support
     <y> Protocol filter support
   <m> HIDP protocol support

I find no difference in choosing built-in modules vs. loading modules with the exception that someday you may wish to use out-of-tree drivers directly from wireless.kernel.org. Just adding all the modules guarantees you can do anything bluetooth in the future, not worth worrying about saving a tiny bit of space.

Also be sure and select your bluetooth device driver:

Networking support →
  Bluetooth subsystem support →
    Bluetooth device drivers →
        <m> HCI USB driver

Make sure you choose at least the HCI USB driver. Others may be applicable to your setup too, for example, if you have a Broadcom BCM203x device you'll want the driver for that which loads the appropriate firmware.

Userspace Programs

As of this writing there have been big changes in bluez. If you intend to use headsets, they (currently) will NOT work with bluez-5.x. The HSP/HFP protocol that these headsets use were set up by bluez (in the past), but that now will be handled by pulseaudio (in the future). For headset owners (in the present) you must choose bluez-4.x and pulseaudio-4.x if you intend to use them. So mask the newer versions in /etc/portage/package.mask:

echo “>bluez-4.(whatever version you choose)”>>/etc/portage/package.mask
echo “>pulseaudio-4.(whatever version you choose)”>>/etc/portage/package.mask

A2DP and other protocols still work fine in bluez-5, so if you don't intend to use headsets the above doesn't apply.

Add “bluetooth” to your USE flags in /etc/portage/make.conf

And then: emerge -aND @world

If you need firmware for your bluetooth receiver be sure and get that too. Sys-kernel/linux-firmware is always a safe bet to have on hand.

You'll want to add bluetooth to your startup:

# rc-update add bluetooth default

Usage

While it's rumored that some high-profile Funtoo users refuse to even run X server, for the rest of us a handy GUI can be nice. However, be aware that all operations can be done from the command line as well. If you intend to do that, bluez-4.x needs to be installed with the “test-programs” USE flag. I don't believe it's necessary with bluez-5 and above.

For bluez-4.x/pulseaudio-4.x users wanting a GUI you can use gnome-bluetooth (I recommend <=3.6) or blueman. The blueman project has been abandoned, but still works fine until we all move on past version 4 bluez.

For bluez-5.x users the choice is more limited. Blueman will not work. A newer version of gnome-bluetooth probably does, not tested here. I did test bluedevil and it seemed to work, but I suspect initial pairing must still be done at the command line.

Pair up your devices, you're ready to go! For bluez-4.x with GUI this is pretty trivial, if you have issues, there is plenty on the web. Bluez-5.x is a little different, as far as I have seen it is all done at the command line, once again I refer you to the web where it is covered in detail.

Custom Tips

With bluez-5.x, things are bit strange. It starts with your bluetooth device (receiver) powered off. For those that would like their bluetooth active on machine start, the following code passing around the web added to /etc/udev/rules.d/10-local.rules is purported to work:

#/etc/udev/rules.d/10-local.rules
# Set bluetooth power up
ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"

However, I suspect the same technique the author has been using for years will work just as well. At any rate, while bluez-4.x powers your device on at startup, by the same token you can start it powered down and then use a hotkey to toggle it. By adding the following to your /etc/local.d/*.start file:

#/etc/local.d/*.start
rfkill block bluetooth

Bluez-4.x will start with the device powered off.

Or, you bluez-5.x users that want your device powered on start:

#/etc/local.d/*.start
rfkill unblock bluetooth   #UNTESTED!

/etc/local.d/*.start operates as the root user. This is convenient, as rfkill requires root to execute.

Your bluetooth device will be physically powered down (no electricity even) when rfkill blocks it. Most desktop environments, Gnome, KDE, etc. have some way to assign a hotkey to a command. However, these commands are limited to the unprivileged user. Since rfkill must be run by root I use the following method.

First, we need toggle action, so the key can turn bluetooth either off or on. This accomplishes that. I have a /home/<username>/bin directory with many “helper” programs. In there, create a file owned by root, with chmod 744 permissions called bluetooth.

#/home/<username>/bin/bluetooth
#keyboard shortcut set to <your choice>, toggles bluetooth on & off
#this file must be added to sudoers ;)

# we need to find which rfkill applies to bluetooth, there can be one for wifi, one for bluetooth, I have a 3rd , don't even know what it is

for file in /sys/class/rfkill/*
do
	if [ "$(cat ${file}/name)" = "hp-bluetooth" ]; then   ### you'll have to snoop around in there, rename to what your device is called
		BT_STATE=$(cat ${file}/state)
	fi
done
if [[ ${BT_STATE} -eq 1 ]]; then
	rfkill block bluetooth
	BLUETOOTH_STRING="  Bluetooth powered off"
	DISPLAY_COLOR="red"
else
        rfkill unblock bluetooth
	BLUETOOTH_STRING="  Bluetooth powered on"
	DISPLAY_COLOR="blue"
fi
# Clean up any running aosd_cat processes
killall aosd_cat &> /dev/null
# Display the desired text
echo "$BLUETOOTH_STRING" | aosd_cat -n "Georgia Italic 120" -u 1000 -o 200 -R $DISPLAY_COLOR -S black -f 0 -e 4 -p 3 -x 300

As you can see, I also have a display that prints huge colored letters across my screen telling bluetooth status for a few seconds as I enable/disable it. To have the display you must install x11-libs/libaosd. That's it, the code in the bluetooth program shows how to use this handy library.

Since this program is owned by root, and in fact, rfkill itself must be run as root, simply put the following line in your sudoers file:

<username> ALL = NOPASSWD: /home/<username>/bin/bluetooth

Now just add the following command to your hotkey assignment, and you can toggle bluetooth on & off to your heart's content:

sudo /home/<username>/bin/bluetooth