-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiBluetooth.py
83 lines (70 loc) · 1.94 KB
/
PiBluetooth.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
83
from bluetooth import *
import logging
import threading
class PiBluetooth:
def __init__(self):
self.UUID = "a1513d56-2b38-421d-bee3-4286f12f9866"
self.client_sock = BluetoothSocket ( RFCOMM )
self.btAdd = "08:60:6E:A5:88:E4"
self.mutex = threading.Lock()
def connect(self):
self.mutex.acquire()
try:
logging.debug("bluetooth finding service..")
service_match = find_service(uuid=self.UUID, address=self.btAdd)
while len(service_match) == 0:
service_match = find_service(uuid=self.UUID, address=self.btAdd)
first_match = service_match[0]
port = first_match["port"]
host = first_match["host"]
self.client_sock.setblocking(True)
self.client_sock.connect((host,port))
self.isConnected = True
finally:
self.mutex.release()
def close(self):
self.mutex.acquire()
try:
self.client_sock.setblocking(True)
self.client_sock.close()
self.isConnected = False
logging.info('Bluetooth disconnected')
finally:
self.mutex.release()
def send(self,data):
self.mutex.acquire()
try:
if self.isConnected == False:
logging.warning('No bluetooth connection, abort sending data')
return None
self.client_sock.setblocking(True)
self.client_sock.send(str(data))
logging.info('Bluetooth sent data: '+str(data))
finally:
self.mutex.release()
def receive(self):
self.mutex.acquire()
try:
if self.isConnected == False:
logging.warning('No bluetooth connection, abort sending data')
return None
self.client_sock.setblocking(False)
result = ''
data = self.client_sock.recv(1)
result += data
while(data!='}'):
data = self.client_sock.recv(1)
result += data
logging.info('Bluetooth received data: '+result)
return result
except IOError:
logging.log(5,'Bluetooth non-blocking receive')
return ''
finally:
self.mutex.release()
def connected(self):
self.mutex.acquire()
try:
return self.isConnected
finally:
self.mutex.release()