Skip to content

Bluetooth Apple mouse and Fedora 20 [updated on July 12, 2014]

June 16, 2014

I am using Fedora 20 on Lenovo laptop and everything looks great, except the Apple Bluetooth mouse. The mouse is working fine, until I close the laptop or leave it unattended and it goes to sleep.When I login after, my mouse doesn’t work.  I need to open Bluetooth in Gnome control center, which could be done by clicking “Activites-Search-Blue..” or just typing “gnome-control-center bluetooth” in the terminal. Bluetooth control panel opens and shows me that Bluetooth controller is disabled and there is no connection to the mouse:

Everything is off

Everything is off

I need to enable BLuetooth , by clicking on the upper right corner  slider  turning it to the “ON” state:

bt_on_off

And, finally, I need to enable mouse connection, by clicking on the “Connection”  slider  turning it to the “ON” state as well:

Enabled Bluetooth and mouse connection

I was trying to find the way to automate this process and researched a lot of useful articles about Bluetooth and Linux. The first part (enabling Bluetooth controller) could be done more or less easily. You need to install rfkill package by :

yum install rfkill

After that you  can enable or disable Bluetooth controller with these commands:

rfkill unblock bluetooth”

rfkill block bluetooth.

The second part,  mouse connection, is not that easy to script. I was trying to use hcitool and hciconfig, but it didn’t work to me. The only thing that was working from the command prompt was bluetoothctl, which is the interactive tool. You need to launch it and type a command to connect the mouse (identifying it by its MAC address):

connect 00:1F:F3:E3:A7:FC” and then type “quit” to leave the session:

[root@localhost ]# bluetoothctl
[NEW] Controller 00:1F:3A:F0:5C:3A localhost.localdomain [default]
[NEW] Device 00:1F:F3:E3:A7:FC ckirshen’s mouse
[bluetooth]# connect 00:1F:F3:E3:A7:FC
Attempting to connect to 00:1F:F3:E3:A7:FC
[CHG] Device 00:1F:F3:E3:A7:FC Connected: yes
Connection successful
[bluetooth]# quit
[DEL] Controller 00:1F:3A:F0:5C:3A localhost.localdomain [default]
[root@localhost ]#

There are several tools to script interactive sessions, I have chosen empty.

In order to use empty, you need to download the source code and compile it, according to the instructions on empty.sourceforge.net website.

Finally, here is script that doing all Bluetooth job  “in a one shot”:

—————————–bt-mouse.sh begins————————————————

#!/bin/bash
#script for turning on bluetooth and connecting bluetooth mouse

#turn bluetooth on

rfkill unblock bluetooth

#sleep 3 seconds, let controller to become visible

sleep 3

#open interactive session with bluetoothctl

empty -f -i in.fifo -o out.fifo -p empty.pid -L empty.log bluetoothctl

#send connection command to bluetoothctl session

empty -w -i out.fifo -o in.fifo mouse ‘connect 00:1F:F3:E3:A7:FC\n’

#send quit command to bluetoothctl session

empty -w -i out.fifo -o in.fifo successful ‘quit\n’

—————————bt-mouse.sh ends————————————————-

After we enable bluetooth with “rfkill unblock bluetooth” we are waiting for 3 seconds, because the script is too fast and we need to give some time to the hardware to initialize itself properly.

Then we open interactive session with

“empty -f -i in.fifo -o out.fifo -p empty.pid -L empty.log bluetoothctl”

where -f is “to fork” the process (bluetoothctl),

-i is the input FIFO channel name for the process (bluetoothctl),

-o is the output FIFO channel name for the process (bluetoothctl),

-p is the file with process ID,

-L is the log file.

Next command is

 “empty -w -i out.fifo -o in.fifo mouse ‘connect 00:1F:F3:E3:A7:FC\n'”

where  -w means to write command (‘connect 00:1F:F3:E3:A7:FC\n’)

-i the input channel, where to read

-o the output channel, where to write

mouse -is the output that we are waiting  before we sending the command,

it is the end of this line:

“[NEW] Device 00:1F:F3:E3:A7:FC ckirshen’s mouse”

A little confusing is the fact that with “empty -w” we are using “-i out.fifo” and “-o in.fifo” in reverse to the first command, when we created interactive session. It is so, because we need to WRITE to the input channel and LISTEN the output channel of the session. In other words, the output channel from the first command (“epmty -f”) becomes the  input channel for the second command (“epmty -w”) .

In the last command

“empty -w -i out.fifo -o in.fifo successful ‘quit\n’ “

we are sending “quit” in the same manner we were sending the previous connection command.

If everything goes well, script exits without any output and Bluetooth mouse starts magically working.

If something wrong, there could be some error messages, like

./bt_mouse.sh
empty: Data stream is empty. Keyphrase wasn’t found. Exit on timeout
empty: Data stream is empty. Keyphrase wasn’t found. Exit on timeout

Mostly, the errors happen when the previous script run did something wrong (it could not do anything wrong  by design, unless you start change it and playing with it :-) ). In this case we need to run:

ps aux | grep blue

and kill all empty and bluetoothctl instances.

In addition, here are some  useful links on the topic:

1) https://wiki.archlinux.org/index.php/Bluetooth

2) http://wireless.kernel.org/en/users/Documentation/rfkill

3) http://empty.sourceforge.net/

[UPDATE]

Now, if you read all of this until the end, here is the bonus. It was really fun to create this script, but in most cases, the only thing I need to make my mouse working is to push the middle button after I woke up my computer. I didn’t know that :))

 

From → Hardware, Scripts

One Comment
  1. Johna49 permalink

    Hi, I desire to subscribe for this web site to obtain latest updates, therefore where can i do it please assist.

Leave a comment