-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtconnection.py
More file actions
82 lines (57 loc) · 1.99 KB
/
btconnection.py
File metadata and controls
82 lines (57 loc) · 1.99 KB
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
#!/usr/bin/env python
import bluetooth
def find_devices():
print('Looking for nearby devices...')
devices_list = []
devices = bluetooth.discover_devices(duration=5, lookup_names=True)
for d in devices:
devices_list.append(d + (1,))
return devices_list
def find_services():
print('Looking for nearby services...')
services = bluetooth.find_service()
services_list = []
for s in services:
services_list.append((s['host'], str(s['name']), s['port']))
return services_list
def print_table(*args):
# table header
print("\n{:2} {:<18} {:<4} {}".format('ID', 'ADDRESS', 'PORT', 'NAME'))
# create a table with all devices
i = 0 # counter
for devices in args:
if len(devices):
for device in devices:
i += 1 # increment
addr, name, port = device
print('{:>2} {:<18} {:<4} {}'.format(i, addr, port, name))
print(' ------------------------------ ') # separator
else:
print(' ----------- EMPTY ------------ ')
else:
print('{:2} Find Devices'.format(i + 1))
print('{:2} Find Services'.format(i + 2))
print(' ------------------------------ ')
def choose(devices):
print("\nType device ID or action:", end=" ")
choice = int(input().lower())
diff = choice - len(devices)
if diff > 0:
if diff == 1:
devices_nearby = find_devices()
print_table(devices_nearby)
return choose(devices_nearby)
else:
services_nearby = find_services()
print_table(services_nearby)
return choose(services_nearby)
return devices[choice - 1]
def do_connection(addr, name, port):
# show message
print("Connecting to {} on {} port {}".format(name, addr, port))
# Create the client socket
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((addr, port))
# connection ok
print("Connected.\n")
return sock