-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble.py
75 lines (58 loc) · 1.68 KB
/
ble.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
import sys
import select
import bluetooth
class BLE:
SERVER_UUID = "56e8a14a-80b3-11e5-8bcf-feff819cdc9f"
SERVER_ADDR = None
SERVER_PORT = None
SERVER_NAME = None
SERVER_HOST = None
SOCKET = None
BUFFER_SIZE = 1024
def connect(self):
print("BLE: Looking for the Bluetooth server")
# Scan for bluetooth devices
#nearby_devices = bluetooth.discover_devices(lookup_names=True)
#print("found %d devices" % len(nearby_devices))
#for addr, name in nearby_devices:
#print(" %s - %s" % (addr, name))
serviceMatches = bluetooth.find_service( uuid = self.SERVER_UUID, address = self.SERVER_ADDR )
if len(serviceMatches) == 0:
print("BLE: Couldn't find the specified server")
sys.exit(0)
firstMatch = serviceMatches[0]
self.SERVER_PORT = firstMatch["port"]
self.SERVER_NAME = firstMatch["name"]
self.SERVER_HOST = firstMatch["host"]
print("BLE: Connecting to \"%s\" on %s" % (self.SERVER_NAME, self.SERVER_HOST))
# Create the client socket
self.SOCKET = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.SOCKET.connect((self.SERVER_HOST, self.SERVER_PORT))
self.SOCKET.setblocking(0)
print("BLE: Connected")
#self.input()
def input(self):
while True:
data = input()
if len(data) == 0: break
self.SOCKET.send(data)
def send(self, message):
try:
self.SOCKET.send(message)
return
except:
return
def receive(self):
#ready = select.select([self.SOCKET], [], [])
#if ready[0]:
#message = self.SOCKET.recv(self.BUFFER_SIZE)
#print(message)
#return message
#return
try:
message = self.SOCKET.recv(self.BUFFER_SIZE)
return message
except:
return
def disconnect(self):
self.SOCKET.close()