Bluetooth in Ubuntu

So I’ve been having a bit of an issue where my bluetooth mouse/keyboard would disconnect.  So I wrote a little script that I can leave running which at least helps with the issue. You’ll have to change the mouseid and keyboard id to match yours and you might also want to change the if(2 == $num_connected) test, so that it matches the number of devices you have. I.e. if you add a device you need to make a new id for it and add it in the grep test, if you remove a device, you don’t really need to remove it from the grep test, but you do need to change the num_connected test. I put this in bin, and run it and just put it somewhere out of the way. You could also set it to start running at startup. It does seem there’s no better way than this, as it is a limitation of my keyboard that it does not become discoverable if you press a button and it is not connected.

#!/usr/bin/perl
use strict;
my $num_connected;
my $mouseid = “00:22:48:D9:D6:7C”;
my $keyboardid = “00:07:61:AB:27:01”;
my $sleepval = 1;
my $lastaction = “sleep”;

while( 1 )
{
#++$current_cycle;
$num_connected = `hidd –show|wc -l`;
chomp($num_connected);
if(2 == $num_connected)
{
system(“sleep $sleepval”);
$lastaction = “sleep”;
next;
}
else
{
if($lastaction eq “sleep”){print “Scanning\n”;}
system “hcitool scan|awk ‘{print \$1}’|grep -E \”$mouseid|$keyboardid\”|xargs -t -n 1 -r sudo hidd –connect”;
$lastaction = “scan”;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: