-
Notifications
You must be signed in to change notification settings - Fork 4
/
BluetoothController.py
82 lines (72 loc) · 2.56 KB
/
BluetoothController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from time import sleep
import bluetooth
class BluetoothController(object):
target_name = "HC-05"
target_address = "20:16:07:05:28:54"
nearby_devices = None
is_connected = False
port = 1
sock = None
prevCommand = ""
command = "s"
#def __init__(se, target_name, target_address,target_port):
# target_name = "KAIZEN""
# target_address = target_address
# port = target_port
@staticmethod
def connect():
print 'Bluetooth Controller Searching for devices... '
nearby_devices = bluetooth.discover_devices()
for bluetooth_address in nearby_devices:
if BluetoothController.target_address == bluetooth_address:
BluetoothController.is_connected = True
BluetoothController.target_address = bluetooth_address
if BluetoothController.target_address is not None:
print "Bluetooth Controller found target with address ", BluetoothController.target_address
BluetoothController.connect_to_slave()
else:
print "could not find target bluetooth device nearby"
break
@staticmethod
def connect_to_slave():
BluetoothController.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print "Bluetooth Controller >> Connecting to Slave...."
BluetoothController.sock.connect((BluetoothController.target_address, BluetoothController.port))
print"Bluetooth Controller conected success"
@staticmethod
def disconnect():
BluetoothController.sock.close()
@staticmethod
def send_command(command,message = None):
#send all command
#print message
if command != BluetoothController.prevCommand:
if message == None:
print "Sent command " + command
else:
print message
if BluetoothController.is_connected == True:
BluetoothController.sock.send(command)
BluetoothController.prevCommand = command
# if BluetoothController.is_connected == True:
# BluetoothController.prevCommand = command
# BluetoothController.sock.send(command)
return
def parallelProcess(command):
BluetoothController.connect()
while True:
print "sending command " + command
BluetoothController.send_command(command)
sleep(1)
if __name__ == '__main__':
#bluetoothController = BluetoothController("KAIZEN","B8:27:EB:26:F6:A4",1)
command = "s"
#bluetoothProcess = Process(target=parallelProcess,args=(command))
#bluetoothProcess.start()
#bluetoothProcess.join()
BluetoothController.connect()
while True:
command = raw_input("Enter command: ")
print " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " + command
#BluetoothController.command = command
BluetoothController.send_command(command)