Tuesday, April 10, 2012

Playing around with Bluetooth and Python

For those of you who don’t know Python, Python is a relatively new computer language that was quickly adopted by software giants such as Google, Microsoft, Red-Hat. Since Python has a very clear (almost algorithmic) syntax, anyone can learn it quickly, and if you have a little background in computer programing, it’s even easier. Because of those reasons, M.I.T had chosen Python in their introduction to computer programming.

If you’d like to get your hands a bit dirty, IronPython has a great tutorial with some hands on, highly recommended.

Since most of my work around computers involves Python for the past two years or so, I’ve decided to open a new section called the Python Challenge.

This week I was presented with a cool challenge: find a way to make your computer screen lock while you’re away from the computer.

My initial thought, Bluetooth! It has a short range, and Microsoft provides a nice interface to the Bluetooth stack. The only “disadvantage” is that you have to initially pair the device using Windows. This actually works for my advantage since it prevents possible malicious use.

The code below works only for locking the screen while away, and as a security precaution I’ve chosen not to include the section where the computer immediately logs on while you’re close to the computer.

Let’s get busy
  1: #!/usr/bin/env python
  2: 
  3: # Importing modules
  4: from bluetooth import *
  5: import time
  6: import subprocess
  7: 
  8: print "\nPlease wait while discovering devices...\n"
  9: 
 10: # Discovering devices
 11: devices = discover_devices(lookup_names=True)
 12: counter = 0
 13: for i,j in devices:
 14:     counter+=1
 15:     print "%d) %s - %s" % (counter,i,j)
 16: 
 17: device_num = raw_input("\n\nPlease select a device: ")
 18: selected_device =str(devices[int(device_num) - 1][0])
 19: print "Please wait, pairing with %s" % i
 20: 
 21: # Create the client socket
 22: client_socket=BluetoothSocket( RFCOMM )
 23: 
 24: # Attempt to pair
 25: try:
 26:     client_socket.connect((selected_device, 3))
 27: 
 28: except IOError:
 29:     print "Bluetooth error, check your settings"
 30:     raise SystemExit
 31: 
 32: except KeyboardInterrupt:
 33:     raise SystemExit
 34: 
 35: else:
 36:     print "Paired with %s... monitoring" % j
 37:     connected = True
 38:     locked = False
 39: 
 40: # Send Hi every second, and detect when connection is broken
 41: while 1:
 42:     try:
 43:         client_socket.send("Hi")
 44: 
 45:     except IOError:
 46:         if locked !=True:
 47:             locked = True
 48:             connected = False
 49:             subprocess.call("rundll32.exe user32.dll,LockWorkStation")
 50: 
 51:         if connected == False:
 52:             try:
 53:                 client_socket=BluetoothSocket( RFCOMM )
 54:                 client_socket.connect((selected_device, 3))
 55: 
 56:             except IOError:
 57:                 continue
 58: 
 59:             else:
 60:                 connected = True
 61: 
 62:     except KeyboardInterrupt:
 63:         client_socket.close()
 64: 
 65:     else:
 66:         locked = False
 67:         time.sleep(1)

Cheers,


at0m q[^_^]p

1 comment:

  1. i know that you said that you don't have it log back on because of precaution, but could you include the code for it to log back in when the device is near?

    ReplyDelete