-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect_bluetooth.py
46 lines (32 loc) · 1.01 KB
/
connect_bluetooth.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
__author__ = 'Jenny'
import bluetooth
class ConnectBluetooth:
def __init__(self):
print "Connecting to bluetooth..."
target_name = "Onetouch Idol 3"
#target_name = "Galaxy Mega"
target_address = None
nearby_devices = bluetooth.discover_devices()
print nearby_devices
for addr in nearby_devices:
print bluetooth.lookup_name(addr)
if target_name == bluetooth.lookup_name(addr):
target_address = addr
break
print target_address
uuid = "fa87c0d0-afac-11de-8a39-0800200c9a66"
service_matches = bluetooth.find_service(uuid = uuid, address = target_address)
if len(service_matches) == 0:
print "Could not find service"
sys.exit(0)
port = service_matches[0]["port"]
name = service_matches[0]["name"]
host = service_matches[0]["host"]
self.socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.socket.connect((host, port))
def send(self, message):
self.socket.send(str(message))
if(message == "exit"):
self.socket.close()
def close(self):
self.socket.close()