# Copyright 2015 Plugable Technologies
# Author : Ivan Fossa-Ferrari
# License: The MIT License (MIT): http://opensource.org/licenses/MIT
import bluetooth
from array import array
import sys
addr = None
if (len(sys.argv) != 3) or (not sys.argv[2] in ['on', 'off']):
print "USAGE: python " + sys.argv[0] + " [bdaddr] <on/off>"
sys.exit(0)
addr = sys.argv[1]
print "Searching for Plugable PS-BTAPS1 on " + addr
# Search for BT Devices at provided BT Address
matches = bluetooth.find_service(address = addr)
if len(matches) == 0:
print "ERROR: Unable to find a Plugable PS-BTAPS1 on " + addr
sys.exit(0)
# Find the Serial Port Profile service on the BTAPS1 to connect to
for match in matches:
if match['protocol'] == 'RFCOMM' and bluetooth.SERIAL_PORT_PROFILE in match['profiles']:
device = match
break
else:
print "ERROR: Unable to find Serial Port Profile on " + addr + ". Are you sure this is a Plugable PS-BTAPS1?"
sys.exit(0)
port = device["port"]
host = device["host"]
print "Connecting to " + str(host)
# Create the client socket
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((host, port))
print "Connected"
# Create a byte array out of the expected payload
if sys.argv[2] == 'on':
# ON Payload = 0xCCAA03010101
payload = buffer(array('B', [0xCC,0xAA,0x03,0x01,0x01,0x01]))
elif sys.argv[2] == 'off':
# OFF Payload = 0xCCAA03010100
payload = buffer(array('B', [0xCC,0xAA,0x03,0x01,0x01,0x00]))
# Send payload to device, print number of bytes sent(should be 6)
bytes = sock.send(payload)
print "Sent " + str(bytes)
sock.close()