PaulDotCom mailing list archives

Re: bluetooth device for proximity detection


From: Bruce Barnett <grymoire () gmail com>
Date: Tue, 17 Jan 2012 22:06:11 -0500

On Tue, Jan 17, 2012 at 10:03 AM, Robin Wood <robin () digininja org> wrote:

On 17 January 2012 14:43, Bruce Barnett <grymoire () gmail com> wrote:
A MetaWatch? http://www.metawatch.org/ However it's $200.

Nice kit

A perl script can do this easily.

You can do something easily in perl?


Sure. It's a love/hate relationship. :-) Writing  maintainable  code does
take more time.

I wrote this script that does what you want. It can use any BlueTooth
device. You just have to specify the device name and BlueTooth address.
It doesn't do any pairing, and if someone spoofs the name and address, they
can prevent the screenlock.
I added an "unsafe" option that if enabled, it unlocks the screen as well
when you come back. This is cool as the computer will lock/unlock
automatically.

Since the MetaWatch has a toggle to turn on and off the BlueTooth, it's a
great way to drive someone crazy if you can run this script on the computer
they are using.

You can change the timeout. I also added a check to make sure the device
was nearby before it entered screenlock mode, to prevent an accidental
lock.

Note: I don't know how long the battery will last on the MetaWatch if it's
always in discover mode. It might need frequent re-charging.

----------------------------


#!/usr/bin/perl -w
#
# KeepNear - this program wants to keep a BlueTooth device near the Linux
system.
# If the BlueTooth device leaves the vicinity, the system screenlocks.
# If the device comes back, the system unlocks the screen
# Written by Grymoire - Tue Jan 17 21:29:03 EST 2012
#
#
use strict;
#User modifiable configuration parameters
my $verbose=0; # set to 1 for more detail/debug
my $address = "D0:37:61:3F:F0:0A"; # bluetooth address the system is
looking for
my $name = "MetaWatch";       # name of bluetooth device the system is
looking for
my $interval_between_samples = 10; # interval in seconds
my $maximum_interval_before_locking = 60*5; # 5 minutes; If device is gone
for this time, exit with status=1;
my $starting_delay = 60*3; # 3 minutes - wait this amount of time to start
the timer. If no device is found, exit with status=2;
my $unsafe = 0; # true if you want the bluetooth device to unlock the
screen.
# naturally - any device that can spoof the BlueTooth address and name can
unlock the computer.
# Use the unsafe mode at your own risk.

# Pick command to lock the screen
my $lock_command = "gnome-screensaver-command -l";
#my $lock_command = "xscreensaver-command -activate";
#my $lock_command = "dcop kdesktop KScreensaverIface lock";

# Is there a command to unlock the screen? - Warning - spoofing BT address
can unlock the computer
my $unlock_command = "gnome-screensaver-command -d";

# other variables used

my $timestamp_last_time_device_was_seen;
my $timestamp_last_time_device_was_missing;

# Load the Bluetooth module - with friendly error
eval   'use Net::Bluetooth;';
if ($@) {
  $verbose && print $@; # print the error message
  print "Hmm. I cannot find the perl library for Bluetooth.\n";
  print "If you do not mind, I have some suggestions that might help...\n";
  print "\n";
  print "If you have Ubuntu, try 'apt-get install libnet-bluetooth-perl'\n";
  print "Otherwise, try installing Net::Bluetooth in one of the following
directories: " . join(", ", @INC) . "\n";
  print "If it's installed elsewhere, try adding the following line to the
perl file: 'unshift \@INC \"/home/local\"'\n";
  die "Sorry - I cannnot go any further. Aborting";

}

sub am_i_near() {
  #### look at all of the remote bluetooth devices in the area
  #### return 1 (found) or 0 (not found)

  my $addr; # address of BT device we found
  my $device_ref = get_remote_devices();
  foreach $addr (keys %$device_ref) {
    $verbose && printf("Name: %s, Address: %s\n",  $addr,
$device_ref->{$addr});
    if (($addr =~ /$address/i) && (($device_ref->{$addr}) =~ /$name/i)) {
      # Both the name and the BlueTooth address matches
      return 1; # I found the device
    } else {
      $verbose && printf( "Device %s != %s, and/or BlueTooth Address %s !=
%s\n",
                          $addr, $address, $name, $device_ref->{$addr});
    }
  }
  return 0; #device was not found
}


sub main() {

# the first time I start up, make sure I find the device
  my $start=time();
  my $interval = 0;
  while ($interval<=$starting_delay &&
!defined($timestamp_last_time_device_was_seen)) {
    $interval=time()-$start; # how long have I been waiting?
    $verbose && print "I've been waiting $interval seconds so far\n";
    if (&am_i_near()) {
      $timestamp_last_time_device_was_seen = time();
    } else {
      printf(STDERR "Warning - Cannot find BlueTooth device with address
$address, let me try again\n");
      sleep($interval_between_samples);
    }
  }

  if (defined($timestamp_last_time_device_was_seen)) {
    $verbose && printf("I found the device after %f seconds\n",
($timestamp_last_time_device_was_seen-$start))
  } else {
    $verbose && printf("I waited $interval seconds, which is greater that
%d, and I did not see BlueTooth address $address, so I am quiting\n",
$starting_delay);
    exit(2);
  }

  # I found it. Now we enter the main loop
  printf(STDERR "Entering screenlock mode\n");
  sleep($interval_between_samples); # once
# now start the main loop.
  while (1) {
    # is the device nearby?
    if (&am_i_near()) {
      $timestamp_last_time_device_was_seen = time(); # yes.
      # perhaps unlock the screen - if the unsafe option is on
      $unsafe && system($unlock_command);
    }
    $interval=time()-$timestamp_last_time_device_was_seen; # how long is
the device missing?
    if ($interval > $maximum_interval_before_locking) { # too long.
      system($lock_command);
    } else {
      $verbose && printf(" %f < %f, wait again\n", $interval,
$maximum_interval_before_locking);
    }
    sleep($interval_between_samples);
  }
}

&main();
1; #exit properly
_______________________________________________
Pauldotcom mailing list
Pauldotcom () mail pauldotcom com
http://mail.pauldotcom.com/cgi-bin/mailman/listinfo/pauldotcom
Main Web Site: http://pauldotcom.com

Current thread: