-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth devices.py
executable file
·176 lines (100 loc) · 3.19 KB
/
Bluetooth devices.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding: utf-8
# In[1]:
#find my phone
import bluetooth
nearby_devices = bluetooth.discover_devices()
nearby_devices
# In[34]:
target_name = "ASUS_X008DA"
target_address = None
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print ("found target bluetooth device with address ", target_address)
else:
print ("could not find target bluetooth device nearby")
# In[1]:
# Asynchronous device discovery
import bluetooth
import select
class MyDiscoverer(bluetooth.DeviceDiscoverer):
def pre_inquiry(self):
self.done = False
def device_discovered(self, address, device_class, name):
print ("%s - %s" % (address, name))
def inquiry_complete(self):
self.done = True
# In[5]:
d = MyDiscoverer()
d.find_devices(lookup_names = True)
d
# In[6]:
readfiles = [ d, ]
readfiles
while True:
rfds = select.select( readfiles, [], [] )[0]
if d in rfds:
d.process_event()
if d.done: break
# In[12]:
# d.device_discovered
d.names_found
# In[1]:
# bluetooth low energy scan
from bluetooth.ble import DiscoveryService
service = DiscoveryService()
devices = service.discover(2)
for address, name in devices.items():
print("name: {}, address: {}".format(name, address))
# In[5]:
# simple inquiry example
import bluetooth
from pprint import pprint
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print (nearby_devices)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
print(" %s - %s" % (addr, name))
# In[6]:
# find services of the device
service = find_service(address=nearby_devices [0] [0])
pprint(service)
# In[8]:
# Example 3-6. rfcomm-server-sdp.py - Dynamically allocating port numbers and using the Service Discovery
# Protocol (SDP) to search for and advertise services is a simple process in PyBluez. The get_available_port
# method finds available L2CAP and RFCOMM ports, advertise_service advertises a service with the local
# SDP server, and find_service searches Bluetooth devices for a specific service.
from bluetooth import *
server_sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
# port = bluetooth.get_available_port(bluetooth.RFCOMM)
port = 3
server_sock.bind(("",port))
server_sock.listen(1)
print ("listening on port %d" % port)
uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
bluetooth.advertise_service( server_sock, "FooBar Service", uuid )
client_sock,address = server_sock.accept()
print ("Accepted connection from ",address)
data = client_sock.recv(1024)
print ("received [%s]" % data)
client_sock.close()
# In[ ]:
# Example 3-7. rfcomm-client-sdp.py
import sys
import bluetooth
uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
service_matches = bluetooth.find_service( uuid = uuid )
if len(service_matches) == 0:
print "couldn't find the FooBar service"
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print "connecting to \"%s\" on %s" % (name, host)
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((host, port))
sock.send("hello!!")
sock.close()