Advertisement
IvanF

Plugable PS-BTAPS1 Sample Python App

Dec 19th, 2014
2,557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. # Copyright 2015 Plugable Technologies
  2. # Author : Ivan Fossa-Ferrari
  3. # License: The MIT License (MIT): http://opensource.org/licenses/MIT
  4. import bluetooth
  5. from array import array
  6. import sys
  7.  
  8. addr = None
  9.  
  10. if (len(sys.argv) != 3) or (not sys.argv[2] in ['on', 'off']):
  11.     print "USAGE: python " + sys.argv[0] + " [bdaddr] <on/off>"
  12.     sys.exit(0)
  13.  
  14. addr = sys.argv[1]
  15. print "Searching for Plugable PS-BTAPS1 on " + addr
  16.  
  17. # Search for BT Devices at provided BT Address
  18. matches = bluetooth.find_service(address = addr)
  19.  
  20. if len(matches) == 0:
  21.     print "ERROR: Unable to find a Plugable PS-BTAPS1 on " + addr
  22.     sys.exit(0)
  23.  
  24. # Find the Serial Port Profile service on the BTAPS1 to connect to
  25. for match in matches:
  26.     if match['protocol'] == 'RFCOMM' and bluetooth.SERIAL_PORT_PROFILE in match['profiles']:
  27.         device = match
  28.         break
  29. else:
  30.     print "ERROR: Unable to find Serial Port Profile on " + addr + ". Are you sure this is a Plugable PS-BTAPS1?"
  31.     sys.exit(0)
  32.  
  33. port = device["port"]
  34. host = device["host"]
  35.  
  36. print "Connecting to " + str(host)
  37.  
  38. # Create the client socket
  39. sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  40. sock.connect((host, port))
  41.  
  42. print "Connected"
  43.  
  44. # Create a byte array out of the expected payload
  45. if sys.argv[2] == 'on':
  46.     # ON Payload = 0xCCAA03010101
  47.     payload = buffer(array('B', [0xCC,0xAA,0x03,0x01,0x01,0x01]))
  48. elif sys.argv[2] == 'off':
  49.     # OFF Payload = 0xCCAA03010100
  50.     payload = buffer(array('B', [0xCC,0xAA,0x03,0x01,0x01,0x00]))
  51.  
  52. # Send payload to device, print number of bytes sent(should be 6)
  53. bytes = sock.send(payload)
  54. print "Sent " + str(bytes)
  55.  
  56. sock.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement