注意:

The Funtoo Linux project has transitioned to "Hobby Mode" and this wiki is now read-only.

Difference between revisions of "Bluetooth made easy"

From Funtoo
Jump to navigation Jump to search
(→‎Usage: more updating to modern day)
(→‎Custom Tips: yet more cool updating to modern day)
Line 49: Line 49:
== Custom Tips ==
== Custom Tips ==
{{fancynote|Some of the following requires installation of net-wireless/rfkill, a handy thing to have anyway}}
{{fancynote|Some of the following requires installation of net-wireless/rfkill, a handy thing to have anyway}}
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:
Bluez-5.x 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:
<console>
<console>
##i###/etc/udev/rules.d/10-local.rules
##i###/etc/udev/rules.d/10-local.rules
Line 55: Line 55:
##i##ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"
##i##ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"
</console>
</console>
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:
However, I suspect the same technique the author has been using for years will work just as well.  I used this back in the days of bluez-4.x which powered on the receiver 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:
<console>
<console>
##i###/etc/local.d/*.start
##i###/etc/local.d/*.start
##i##rfkill block bluetooth
##i##rfkill block bluetooth
</console>
</console>
Bluez-4.x will start with the device powered off.
Bluez will start with the device powered off.


Or, you bluez-5.x users that want your device powered on start:
Or, if you want your device powered on start:
<console>
<console>
##i###/etc/local.d/*.start
##i###/etc/local.d/*.start
Line 125: Line 125:
##i##sudo /home/<username>/bin/bluetooth
##i##sudo /home/<username>/bin/bluetooth
</console>
</console>
== Yet Another Trick ==
== Yet Another Trick ==
Another handy thing that can be done.  I have 2 computers at my desk, 1 is rarely used and somewhat underpowered.  I sometimes use it though to talk on voip, leaving my main machine free to compile, restart, whatever.
Another handy thing that can be done.  I have 2 computers at my desk, 1 is rarely used and somewhat underpowered.  I sometimes use it though to talk on voip, leaving my main machine free to compile, restart, whatever.

Revision as of 06:44, April 12, 2015

Kernel Support

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

root ##i##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:

root ##i##Networking support →
root ##i##  Bluetooth subsystem support →
root ##i##    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

There was a big change in the transition from bluez4 to bluez5, namely support for HandsFree Profile/Headset Profile (HFP/HSP) was dropped. Pulseaudio was supposed to begin supporting those profiles but there was a _____long wait. However, the wait is over and you can now once again use headsets for Skype, Sip, etc. If you plan to use headsets you'll want to add use changes to pulseaudio, as the default usage profile in the Funtoo tree does not support headsets. headset-native and headset-ofono are needed. One example would be

echo ">=media-sound/pulseaudio-5.99 headset-native headset-ofono">>/etc/portage/package.use

A2DP and other protocols work fine in bluez-5 without help from pulseaudio, 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.

Unless you prefer controlling your devices at the command line you'll need either Blueman or Gnome-bluetooth, handy GUI applets, just click on the device you want to control.

You'll want to add bluetooth to your startup:

root # 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.

Pair up your devices, you're ready to go! With either Blueman or Gnome-bluetooth it's intuitive, they'll steer you in the right direction. There's a ton of info on the web if for some reason you wish to use the command line rather than a GUI.

Custom Tips

   Note

Some of the following requires installation of net-wireless/rfkill, a handy thing to have anyway

Bluez-5.x 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:

root ##i###/etc/udev/rules.d/10-local.rules
root ##i### Set bluetooth power up
root ##i##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. I used this back in the days of bluez-4.x which powered on the receiver 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:

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

Bluez will start with the device powered off.

Or, if you want your device powered on start:

root ##i###/etc/local.d/*.start
root ##i##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 one hotkey can turn bluetooth either off or on. The following code 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.

root ##i##$ cd ~
root ##i##$ mkdir bin
root ##i##$ cd bin
root ##i##$ touch bluetooth
root ##i##$ sudo chown root: bluetooth
root ##i##$ sudo chmod 744 bluetooth

Paste the following code into the file with your favorite editor, taking care to change the device name as noted.

#/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, you can tweak font size, etc. to your liking.

This one-liner will list out the available rfkill named devices on your computer:

for file in /sys/class/rfkill/* ; do echo "$(cat ${file}/name)"; done

You may have to snoop around a bit in /sys to find what you're looking for. The name of your device may be something as simple as hci0. I actually use /sys/devices/platform/hp-wmi/rfkill/* on my system, but this generic method should work for you. A static directory usually doesn't work, it's often different on every startup, hence the search code.

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:

root ##i##<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:

root ##i##sudo /home/<username>/bin/bluetooth

Yet Another Trick

Another handy thing that can be done. I have 2 computers at my desk, 1 is rarely used and somewhat underpowered. I sometimes use it though to talk on voip, leaving my main machine free to compile, restart, whatever.

The problem: I wish to use the same headsets on both computers. In practice, this is a bit of a hassle, because you must pair the headsets with only one machine, so as I switch back and forth, I have to change the pairing. Not the end of the world, but we can end that hassle.

   Note

Some newer headsets can be paired to as many as 3 different receivers, great if you have them

The first task is to acquire the bdaddr program. It's on the Backtrack Linux distros, you can probably find it out there in cyberspace somewhere, but it gets confusing on a search because bluez uses the bdaddr abbreviation a lot, creating a lot of noise in your search. I just put it in /usr/bin.

To accomplish this, one machine must be the “Master Record” and the other machine pretends to be that machine with pairing. Not as complicated as it sounds.

Now pair all of your devices on the “Master Record” machine. The secret link codes are stored at (shhhh!) /var/lib/bluetooth in a directory named with your receivers bluetooth address, let's say AA:AA:AA:AA:AA:AA. After this is done you are ready to start on the Pretender machine. It will fool bluez into thinking that it's bluetooth address is AA:AA:AA:AA:AA:AA, even though it has a different address. Copy the AA:AA:AA:AA:AA:AA directory from /var/lib/bluetooth on the “Master Record” machine to /var/lib/bluetooth on the “Pretender”. The Pretender will now have two directories in /var/lib/bluetooth, AA:AA:AA:AA:AA:AA and it's own receiver's address directory. (Unless you have more than 1 receiver, but let's keep it simple).

Then simply modify the code in /home/<username>/bin/bluetooth, only the “turn on” section is shown below, because that's all we are changing:

root #/home/<username>/bin/bluetooth (partial)
...   else
      rfkill unblock bluetooth
root ##i### add these 4 lines
root ##i##        sleep 2
root ##i##        /usr/bin/bdaddr -i hci0 AA:AA:AA:AA:AA:AA  2&>1>/dev/null #of course, you must change this to "Master Record's" receiver address
root ##i##        /usr/sbin/hciconfig hci0 reset
root ##i##        /etc/init.d/bluetooth restart
root ##i### end of addition
	BLUETOOTH_STRING="  Bluetooth powered on"
	DISPLAY_COLOR="blue"   ...

And that's it! Now both machines will use the same headsets and you won't constantly have to re-pair. When the Pretender bluetooth is toggled on it has the other machines bluetooth address for it's receiver. Note that I always start my bluetooth powered off, so it always starts in pretend mode, if you start with yours powered on it won't spoof the other address until you toggle it off & then on unless you add the above code to your /etc/local.d/*.start file as well.

The only caveat is I'm not sure what would ever happen if I had both bluetooth receivers on at the same time and turned on a headset. But far short of a nuclear meltdown I'm sure, I suspect one machine would grab the headset before the other? Anyhow, with hotkey controlled bluetooth it's never been an issue.