To find your device/phone's mac address and name follow
this link . Note you must have your phone set to discoverable during these steps otherwise you will get errors and no information. Once you gathered all the required info change your device to hidden, my script still works fine with hidden devices.
I found that bluetooth proximity sensor script before as well, but I was never able to get it working quite well. I ported it over to perl and made some improvements that made things more reliable for me.
The original script was meant to automatically lock your computer screen when the bluetooth device signal strength is below a certain threshold. It would then detect when your device came back into range and automatically unlock the screen. Pretty neat, especially seeing how my local electronics store sells some proprietary system to do the exact same thing for $50 and it only works with windows.
I was having a lot of issues with the script locking my screen when it got a false read on the signal strength. I added a 2nd pass of signal strength checking so that it would not lock until 2 reads were below the threshold.
I have included my code here as a sample for you to use. I had to pull some stuff out that wasn't relevant to what you need and I haven't tested it to see if it runs or not. As you can see, I have a daemon option for the script to detach from the command line and run in the background. It has the endless loop already, so all you need to do is check the alarm armed status near the top of the script and do "$elk->disarm( code => $disarmCode ) ;" in place of the "#disarm;" line if the alarm is armed.
[codebox]
#!/usr/bin/perl
### README #############
## Make sure /usr/bin/hcitool has suid bit set:
## sudo chmod 4755 /usr/bin/hcitool
use Getopt::Long;
$| = 1;
$Getopt::Long::autoabbrev = 1 ;
$Getopt::Long::ignorecase = 1 ;
# How often to check the distance between phone and computer in seconds
$near_check_interval = 60;
$far_check_interval = 3;
# The RSSI threshold at which a phone is considered far or near
$threshold = -2;
$hcitool = "/usr/bin/hcitool";
my @optl = ("help", "mac_addr=s", "verbose", "daemon");
GetOptions @optl;
if ($opt_help) {
print "$0 -daemon -help -mac_addr <addr> \n\n";
print "-daemon Detach and run as daemin\n";
print "-mac_addr MAC address of bluetooth device to monitor\n";
print "-verbose print debug info to screen\n";
print "-help\n";
exit(0);
}
# Phone
if ($opt_mac_addr) {
$device = $opt_mac_addr;
} else {
$device = "xx:xx:xx:xx:xx:xx";
}
$connected = 0;
sub msg {
print "@_" if ($opt_verbose);
}
sub check_connection {
$connected=0;
my $found = 0;
foreach $s (`$hcitool con 2>&1`) {
if ($device =~ /$s/) {
$found = 1;
}
}
if ($found) {
$connected = 1;
} else {
msg "$time | Attempting connection... ";
if (`$hcitool cc $device 2>&1` eq "") {
msg 'Connected. ';
$connected = 1;
} else {
if (`l2ping -c 2 $device 2>&1` eq "") {
if (`$hcitool cc $device 2>&1` eq "") {
msg 'Connected. \n';
$connected = 1;
} else {
msg 'Not connected. \n';
$connected = 0;
}
}
}
msg "Could not connect to device $device. \n" unless ($connected);
}
if ($connected) {
$rssi =`$hcitool rssi $device`;
$rssi =~ s/RSSI return value: //g; chomp $rssi;
msg "RSSI: $rssi \n";
} else {
$rssi = "-99";
}
return $rssi;
}
## Become a daemon
sub daemonize {
exit(0) if fork();
chdir("/");
setpgrp(0,0);
close(STDIN);
close(STDOUT);
close(STDERR);
}
$name=`$hcitool name $device`;
chomp $name;
$time = `date "+%D %H:%M"`; chomp $time;
msg "$time | Monitoring proximity of \"$name\" [$device]\n";
$state="near";
daemonize() if ($opt_daemon);
while (1) {
$time = `date "+%D %H:%M"`; chomp $time;
my $rssi = check_connection;
if ($state eq "near" && $rssi <= $threshold) {
msg "Phone might have left, pause and recheck \n";
# probe again after 1 second
sleep 1;
$rssi = check_connection;
if ( $rssi <= $threshold) {
msg "$time | *** Device \"$name\" [$device] has left proximity:\n";
$state="far";
#####################
#arm;
#####################
}
} else {
if ($state eq "far" && $rssi >= $threshold+2 ) {
msg "$time | *** Device \"$name\" [$device] is within proximity:\n";
$state="near";
#####################
#disarm;
#####################
}
}
if ( $state eq "far" ) {
sleep $far_check_interval;
} else {
sleep $near_check_interval;
}
}
[/codebox]
On my ubuntu box, you have to be root to run hcitool, but I get around this by setting the uid bit of the hcitool binary.
I'm curious about how you envision a disarm cycle would occur. Do you open a door and expect this script to detect the bluetooth device before the entry timer expires and the alarm goes off?