bryanchicken
Posts: 3
Joined: Mon Feb 09, 2015 9:59 am

Reconnecting bluetooh keyboard and mouse at startup

Mon Feb 09, 2015 10:05 am

Hi all.

I have successfully set up my Apple bluetooth keyboard and magic mouse using the latest version of Raspbian.
I don't have the exact commands right now (i can post later if anyone needs) but in a nutshell i used something like:

Scan:

Code: Select all

hcitool scan
Trust:

Code: Select all

bluez-simple-agent hci0 trusted <mac address> yes
Connect:

Code: Select all

bluez-agent-device connect <mac address>
This works fine for both keyboard and mouse.

However, when i reboot only the one i used first reconnects and i'm forced to run the final 'connect' command again for the other one in order to get it to work.

I've also done the same basic procedure using the GUI (bluetooth manager) with the same results.

Does anyone know how i can get both to reconnect please?

Thanks

User avatar
Raspruss
Posts: 93
Joined: Thu Jan 23, 2014 3:50 pm
Location: Virginia, USA

Re: Reconnecting bluetooh keyboard and mouse at startup

Mon Feb 23, 2015 2:20 pm

I'm currently having the same problem - I have successfully enabled both a bluetooth keyboard and mouse but have not been able to get that to remain across reboots. I used the GUI to "trust" both devices, etc.

I'm looking into it this morning - there are directions around the forums and on the raspberry.org site that may work to get the ddevices recognized, but they do not persist across reboots - one post I found says:
Add the bash command hciconfig hci0 piscan in the /etc/rc.local
which I will try soon - I will try that command manually FIRST, however, to see if it is sufficient ("I do not recognize the "piscan" parameter, for example, (thought that was "scan") so am having doubts it will work).

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Reconnecting bluetooh keyboard and mouse at startup

Mon Feb 23, 2015 2:46 pm

Raspruss wrote:
Add the bash command hciconfig hci0 piscan in the /etc/rc.local
which I will try soon - I will try that command manually FIRST, however, to see if it is sufficient ("I do not recognize the "piscan" parameter, for example, (thought that was "scan") so am having doubts it will work).
That command will put the Pi in discoverable mode. It MIGHT help (it shouldn't, but, well, Bluetooth), worth a try. By default, the Pi will quit 'discovery mode' after three minutes, long enough for a keyboard to connect, but safe enough not to leave the Pi exposed too long.

User avatar
Raspruss
Posts: 93
Joined: Thu Jan 23, 2014 3:50 pm
Location: Virginia, USA

Re: Reconnecting bluetooh keyboard and mouse at startup

Mon Feb 23, 2015 3:08 pm

My ultimate goal is to de-attach wired-keyboard and wired-mouse and simply use bluetooth same as my primary input devices. It's not clear at all how one goes about doing this - I was able to manually get all four devices working at the same time but, so far, am not getting any sort of persistence across reboots (necessary to have the bluetooth devices be the primary ones "attached" to the Pi).

There are, like, 5000+ posts here on the forum about "bluetooth keyboard" and uber-millions on The 'Net so actually asking for hand-holding is considerably faster than...reading-what's-been-written-already, thus my posts here.

In any case, thanks for the feedback.

rayfranco
Posts: 8
Joined: Thu Aug 16, 2012 11:14 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Fri Mar 06, 2015 1:23 pm

Well, I am glad to know that I am not alone with the problem of getting both the Apple wireless keyboard and magic mouse to reconnect at startup. The only thing I have to add, is that when I had the same Apple magic mouse connected to a Windows 7 machine, I had to do a mouse click before it would start working on the Windows 7 machine.

Did anybody figure out how to get this to work?

User avatar
Raspruss
Posts: 93
Joined: Thu Jan 23, 2014 3:50 pm
Location: Virginia, USA

Re: Reconnecting bluetooh keyboard and mouse at startup

Fri Mar 06, 2015 3:15 pm

i was able to get the keyboard to reconnect across reboots, however the mouse would not - in fact, I lost the ability to use the mouse, period - no joy trying to reconnect NOR re-pair-then-connect.

The keyboard, too, goes to sleep after a period of inactivity and doesn't come back - and, of course, since it is THE keyboard there is no "command" I can type in (...) to get it to reconnect.

I'm convinced this particular bluetooth implementation is not ready for prime time w.r.t. these two devices so decided to simply wait for some future effort by those so inclined to address the problem(s).

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Reconnecting bluetooh keyboard and mouse at startup

Fri Mar 06, 2015 3:55 pm

I'm having this issue too, Motorola BT keyboard & mouse.

I had to add the pi user to the bluetooth group before I was able to add the second device (got errors otherwise), but still I get one or the other paired after reboot, not both. :(

rayfranco
Posts: 8
Joined: Thu Aug 16, 2012 11:14 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Fri Mar 06, 2015 4:30 pm

I think this is a bug. Both devices are "paired" but not "input". The work-a-round is to include the two input commands in the "/etc/rc.local" file immediately above the last line, which is "exit 0". More precisely add the following two lines above the "exit 0" line:

bluez-test-input connect 58:1F:AA:F3:75:62
bluez-test-input connect E8:06:88:3B:C1:AB

58:1F:AA:F3:75:62 is the MAC address of my Apple Magic Mouse
E8:06:88:3B:C1:AB is the MAC address of my Apple Wireless Keyboard

Your MAC addresses will be different than mine.

On boot up the near the end the following will be printed from rc.local:

"My IP address is 192.168.0.16

there will then be a pause while both the mouse and keyboard connect (input),

then "Raspbian GNU/Linux 7 xxxx tty1". And then the login prompt.

Good Luck.

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting Bluetooth keyboard and mouse at startup

Mon Mar 16, 2015 4:13 pm

I'm working on the same difficulty. Clearly there is a lot of interest in using the Pi with living room televisions. I want to be able to sit on the living room couch with Bluetooth keyboard and mouse and do things like checking e-mail. A mouse and keyboard cord isn't appropriate in this setting.

Well, clearly other computers like Macs have a method for handling this. The problem is bigger than reboots -- both the mouse and keyboard shut down with disuse to save batteries. When this happens, the Pi will need to recognize it and go into a frequent series of attempts to reconnect.

This suggests a cron file/script that tests whether the keyboard or mouse is connected or whether they are scannable then connects them. Then the buttons on the mouse and keyboard will allow a reconnect without any USB keyboard and mouse required to start up the Bluetooth connections.

ktb
Posts: 1447
Joined: Fri Dec 26, 2014 7:53 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Mon Mar 16, 2015 10:48 pm

I use the Magic Mouse with Ubuntu usually (not on a Pi), but I have tested the Magic Mouse with my B+ and Pi2B. I use blueman-manager usually. The only issue I found (other than bugs in blueman) was that I have to click the mouse once or twice and move it before it wakes up and is enabled again after not using it for a while or after boot/reboot. However, IIRC that is the same behavior I saw on the Ubuntu PC and a Win7 laptop (I can't recall if it works the same way on OS X). I think the only time it became a problem is when the batteries died. There have been times where I need to reconfigure the mouse to get it working again. In times like those, it is convenient to have a shortcut to blueman-manager on the desktop or somewhere else I can easily navigate to with a keyboard and setup the connection again.

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Thu Mar 19, 2015 12:51 pm

I have found a solution to this problem that works for me on my Raspberry Pi 2. Of course, it could be tuned a bit and I am going to keep looking for more elegant solutions. Basically, the problem has been that the connection of the Bluetooth mouse and keyboard required that a hard-wired mouse and keyboard be used to command something like "hidd --connect xx:xx:xx:xx:xx:xx". But at reboot and power-cycles, and even after a short time of disuse the mouse and keyboard became disconnected again. My solution has been to call a script with cron each two minutes -- the script checks whether the device is scannable and connects it if it is. More on this when I have more time to post...

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Sat Mar 21, 2015 5:52 pm

OK, I finally have time to post. I think.

First, here is the script that sets up a cron event:

Code: Select all

echo '0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /home/pi/mse_kbd.sh' \ > /home/pi/cronfile
crontab -u pi cronfile

Next, the actual cronfile that is created by the script above:

Code: Select all

0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /home/pi/mse_kbd.sh
The script does more than create the file. Next, the /home/pi/mse_kbd.sh file that does the work

Code: Select all

set keyboard='04:DB:56:D4:99:7E'
set mouse='00:1F:20:F0:2D:EA'
sudo hcitool con > bluetooth_connected_devices
if [ 'grep -q ${keyboard} bluetooth_connected_devices' ]; then
    echo 'connecting keyboard'
    sudo hidd --connect 04:DB:56:D4:99:7E
fi
if O[ 'grep -q ${mouse} bluetooth_connected_devices' ]; then
    echo 'connecting mouse'
    sudo hidd --connect 00:1F:20:F0:2D:EA
fi
rm -f bluetooth_connected_devices
Of course, your own idents go into the mouse and keyboard variables. The script is a bit obtuse, because a file named bluetooth_connected_devices will be created and destroyed on your SD chip each two minutes. I suppose I'll have to work on that later. But I eliminated the scan operation that I had originally planned, because it tied up a thread for too long every two minutes.

When this is done, the mouse and keyboard doesn't get a break -- if you leave it on then the Pi will keep it awake constantly. So to save battery charge you'll need to power down the mouse and keyboard when not in use. When you want to use them again, apply power and in less than an average of a minute the Pi will connect to them.

I'll leave out all of the details of pairing and loading the necessary packages with apt-get install because there is plenty of that on these pages already.

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Sat Mar 21, 2015 6:13 pm

A minor error got introduced -- the backslash character in the first script is out of place without an end line following it

diederik
Posts: 393
Joined: Wed Mar 26, 2014 11:17 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Sat Mar 21, 2015 6:37 pm

kr6x wrote:Next, the actual cronfile that is created by the script above:

Code: Select all

0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /home/pi/mse_kbd.sh
Seems like you want it to run every 2 minutes. How about this:
*/2 * * * * /home/pi/mse_kbd.sh

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Sat Mar 21, 2015 8:48 pm

Yes, there's always more ways to skin a cat... care to suggest a way to accomp[lish this without writing a file every two minutes?

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Mon Mar 23, 2015 3:16 pm

OK, the modifications to my script are untested at the moment, but to summarize, if I use

hcitool lq xx:xx:xx:xx:xx:xx
if [$?]; then
hcitool cc xx:xx:xx:xx:xx:xx
fi

lq checks link quality, but will return a value of zero if connected and one if connected. Here hcitool cc is used, but that could be hidd --connect xx:xx:xx:xx:xx:xx also. The updated script would not write a file to the SD card every two minutes so causes less wear and tear on the memory.

kr6x
Posts: 12
Joined: Thu Feb 12, 2015 10:48 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Fri Mar 27, 2015 3:32 pm

Well, I'm still working on this one. I've been using a script that works fine over the short term, but testing over longer periods results in failure to reconnect. None of the commands that are available for reconnecting appear to be very robust. The cron setup and the use of hcitool lq x:x:x:x:x:x and $? for detecting connection in a /bin/sh script does work quite well.

puremoney23
Posts: 1
Joined: Thu Oct 08, 2015 3:59 am

Re: Reconnecting bluetooh keyboard and mouse at startup

Thu Oct 08, 2015 4:05 am

Ok, guys...have you found a solution for Jessie yet?? I can get to the add bluetooth on the Gui, but it never asks me to pair the device with the passcode. What is the problem??

evdemiguel
Posts: 1
Joined: Tue Oct 04, 2016 2:29 pm

Re: Reconnecting bluetooh keyboard and mouse at startup

Thu Oct 06, 2016 3:33 am

Hi Thanks Raspruss :D :D :D for your recommendation to add the line

hciconfig hci0 piscan

to the /etc/rc.local file It work very good for me. :D :D :D
I used the command "sudo nano /etc/rc.local" to add the line.

In my R-Pi 3 with 3.5 LCD using the builtin bluetooth radio, the problem was:
after the Bluetooth mini Keyboard and Mousepad was paired, connected good and worked fine, if I powered off,/on the R-Pi3 always was needed to click connect on Bluetooth menu just to the keyboard start to work,
But after only add your line, now, I can power off/on the R-Pi3 AND AFTER R-Pi3 boot, just push Power ON the Keyboard/mousepad, and it is connected automatically.
I do not need touch any thing in the LCD to connect the Keyboard/mousepad any more.
If the keyboard go to sleep, power off/on the Keyboard and connect again without touch the R-Pi3

Thanls a lot
Eduardo

Return to “Raspberry Pi OS”