It is currently Wed Jul 06, 2011 8:15 pm

All times are UTC - 8 hours




 Page 1 of 1 [ 8 posts ] 
Author Message
 Post subject: Serverside question - test for inPulse presense?
PostPosted: Fri Feb 18, 2011 9:42 am 

Joined: Tue Feb 15, 2011 6:42 pm
Posts: 26
Location: Ontario, Canada
I'm looking at the server side aspects of the SDK, particularily the bt_comm.py script. What I'm wondering is if there's a simple way to query if a given inPulse device is available or not. Eg. in the PingStat example, if you had the py_ping_stat.py script looping, or being called regularily by a daemon, but you had gone out of range or your inPulse was otherwise unavailable, then I assume the script would fail with an error.

So what I'd like to implement is a method of determining, prior to attempting a bluetooth communication, if the watch is actually present.

Looking at the lightblue documentation, what comes to mind is using the finddevicename(address, usecache=True) function. Maybe something like this (pseudocode as I don't know Python yet):
import lightblue
bdaddress="00:00:00:00:00:00"; // the address of our bluetooth device
try:
  if(lightblue.finddevicename(bdaddress, false)) {
    return true; //we got an answer so our bdaddress is valid and in range/connected
  } else {
    return false; // no answer so bdaddress is invalid or out of range/disconnected
  }
except:
  return false; // there was an error so the address is invalid or out of range / not connected
Alternatively, it might be preferable / easier to use the finddevices(getnames=True, length=10) function, then just parse the returned list and see if the desired address is present or not. (My lack of python knowledge prevents me from figuring out which option would be better /easier to implement. For now.)

I'm thinking something like this could be incorporated into the existing bt_comm.py tool as a function, either under the BT_Socket class or by itself.

Any ideas / assistance / feedback would be most welcome!


Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Fri Feb 18, 2011 10:35 am 

Joined: Tue Feb 15, 2011 6:42 pm
Posts: 26
Location: Ontario, Canada
Apologies for replying to my own post, but I've learned enough Python to hack this simple thing together:
import lightblue

addy = "7C:C5:37:00:00:00";
name = "";

try:
   name = lightblue.finddevicename(addy, 0)
except:
   name = ""

if name != "":
  print name + " was found!"
else:
  print addy + " was not found."


It works on my iMac, I can use it to detect whether or not my iPhone is around. It doesn't prove that the device is connected however - my iPhone is not paired with my iMac. But it's a start, I think. Obviously the print lines would be changed to return true / return false, if this were being called as a function.

I also ran a test using the finddevices() function but it runs slower. finddevices takes about 10 seconds (as advertised in the lightblue docs) but the code posted above seems to run in 1 or 2 seconds to return true and about 5 seconds if it can't find the device.

Cheers!


Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Sat Feb 19, 2011 9:46 pm 

Joined: Sat Feb 19, 2011 9:38 pm
Posts: 1
Awesome -- haven't received my inPulse yet, but I was thinking about how to do the same thing.

I just wanted to offer a few suggestions for trimming down your Python code, since you mention that you're just learning it.

import lightblue

addy = "7C:C5:37:00:00:00"

try:
   name = lightblue.finddevicename(addy, 0)
   print name + " was found!"
except:
   print addy + " was not found."

(if you wanted to keep the "if" block instead of putting the prints/returns directly in the try/except blocks, note that in Python you don't need to say if name != "":, you can just say if name: (because things like 0, "", and empty list all effectively evaluate to False in boolean expressions)

Thanks for posting!


Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Sun Feb 20, 2011 7:32 am 

Joined: Tue Feb 15, 2011 6:42 pm
Posts: 26
Location: Ontario, Canada
Thanks for the help jamalex!

I'm familiar with a number of other programming / scripting languages, just haven't had any reason to learn Python till now. :)

Cheers!


Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Sun Feb 20, 2011 12:37 pm 
User avatar

Joined: Mon Feb 14, 2011 7:07 pm
Posts: 172
That's a really cool bit of code, Stephanie. You could use that to detect if your inPulse is in range...and since your watch is usually on your wrist, your comp could sense if you're in front of it! Maybe use it to unlock your user account?



_________________
---

Lead designer of inPulse
Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Sun Feb 20, 2011 2:10 pm 

Joined: Tue Feb 15, 2011 6:42 pm
Posts: 26
Location: Ontario, Canada
Eric wrote:
That's a really cool bit of code, Stephanie. You could use that to detect if your inPulse is in range...and since your watch is usually on your wrist, your comp could sense if you're in front of it! Maybe use it to unlock your user account?


That's exactly the sort of thing I'm thinking... It'd be nice to have my computer fire up my email, browser, open a few console windows, etc. when I get to the office - have it all ready to go as I sit down at my desk. And it'd be able to close them all down again when I leave, incase I've forgotten to log off when I leave.


Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Mon Feb 21, 2011 2:19 am 

Joined: Sat Feb 19, 2011 3:01 pm
Posts: 12
There are a few programs out there that let you lock/unlock your computer when a bluetooth device comes in range. I'd suggest adding a secure challenge/response to the bluetooth device when you're unlocking your computer from it, though. Since the inPulse can run custom code this wouldn't be hard at all.

One very easy thing to do would be rather than unlocking, just lock the computer when you get out of range. Where I work we have a policy of sending funny e-mails company wide when somebody forgets to lock their computer, in the spirit of maintaining security for our machines. Locking the console in MacOSX is as simple as setting the system to lock on screen-saver and then executing '/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine'


Offline
 Profile  
 
 Post subject: Re: Serverside question - test for inPulse presense?
PostPosted: Mon Feb 21, 2011 2:23 am 

Joined: Fri Feb 18, 2011 3:41 pm
Posts: 6
Craig wrote:
There are a few programs out there that let you lock/unlock your computer when a bluetooth device comes in range. I'd suggest adding a secure challenge/response to the bluetooth device when you're unlocking your computer from it, though. Since the inPulse can run custom code this wouldn't be hard at all.

One very easy thing to do would be rather than unlocking, just lock the computer when you get out of range. Where I work we have a policy of sending funny e-mails company wide when somebody forgets to lock their computer, in the spirit of maintaining security for our machines. Locking the console in MacOSX is as simple as setting the system to lock on screen-saver and then executing '/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine'


Are you aware of a good way to unlock a Mac programmatically?


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 8 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: