forked from getsenic/gatt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgattctl.py
More file actions
executable file
·235 lines (196 loc) · 8.01 KB
/
gattctl.py
File metadata and controls
executable file
·235 lines (196 loc) · 8.01 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
from argparse import ArgumentParser
from threading import Thread
import keyboard
from termios import tcflush, TCIFLUSH
import gatt
import time
import sys
device_manager = None
class AnyDeviceManager(gatt.DeviceManager):
"""
An implementation of ``gatt.DeviceManager`` that discovers any GATT device
and prints all discovered devices.
"""
def __init__(self, adapter_name):
super().__init__(adapter_name)
self.nb_device_connected = 0
self.discover_filter = 'True'
def device_discovered(self, device):
if self.discover_filter == 'True':
print("[%s] Discovered, alias = %s" % (device.mac_address, device.alias()))
elif self.discover_filter in device.alias() or self.discover_filter in device.mac_address:
print("[%s] Discovered, alias = %s" % (device.mac_address, device.alias()))
def make_device(self, mac_address):
return AnyDevice(mac_address=mac_address, manager=self)
def run(self):
print("Running Manager")
super().run()
def quit(self):
print("Stopping Manager")
super().stop()
class AnyDevice(gatt.Device):
"""
An implementation of ``gatt.Device`` that connects to any GATT device
and prints all services and characteristics.
"""
def __init__(self, mac_address, manager, auto_reconnect=False):
super().__init__(mac_address=mac_address, manager=manager)
self.auto_reconnect = auto_reconnect
self.message = ''
self.transmition = ""
self.reception = ""
def connect(self):
print("Connecting...")
super().connect()
def connect_succeeded(self):
super().connect_succeeded()
device_manager.nb_device_connected += 1
print("[%s] Connected" % (self.mac_address))
def connect_failed(self, error):
super().connect_failed(error)
print("[%s] Connection failed: %s" % (self.mac_address, str(error)))
def disconnect_succeeded(self):
if device_manager.nb_device_connected:
device_manager.nb_device_connected -= 1
super().disconnect_succeeded()
print("[%s] Disconnected" % (self.mac_address))
if self.auto_reconnect:
self.connect()
def services_resolved(self):
super().services_resolved()
print("[%s] Resolved services" % (self.mac_address))
for service in self.services:
print("[%s] Service [%s]" % (self.mac_address, service.uuid))
for characteristic in service.characteristics:
if characteristic.uuid == '6e400003-b5a3-f393-e0a9-e50e24dcca9e':
characteristic.enable_notifications()
print("[%s] Characteristic [%s]" % (self.mac_address, characteristic.uuid))
print("[%s] Resolved services" % (self.mac_address))
device_service = next(
s for s in self.services
if s.uuid == '6e400001-b5a3-f393-e0a9-e50e24dcca9e')
self.reception = next(
c for c in device_service.characteristics
if c.uuid == '6e400003-b5a3-f393-e0a9-e50e24dcca9e')
self.transmition = next(
c for c in device_service.characteristics
if c.uuid == '6e400002-b5a3-f393-e0a9-e50e24dcca9e')
if self.message:
bar = bytearray(self.message.encode())
self.transmition.write_value(bar)
time.sleep(2)
self.reception.read_value()
def characteristic_value_updated(self, characteristic, value):
print("RX:", (value.decode("utf-8")).replace('\n', ''))
def characteristic_read_value_failed(self, characteristic, error):
print("RX fail : ", error)
def characteristic_enable_notifications_succeeded(self, characteristic):
print("enable notification succeeded for characteristic ", characteristic.uuid)
def characteristic_enable_notifications_failed(self, characteristic, error):
print("enable notification succeeded for characteristic ", characteristic.uuid, ", error, ", error)
def characteristic_write_value_succeeded(self, characteristic):
print("write value succeeded for characteristic ", characteristic.uuid)
def characteristic_write_value_failed(self, characteristic, error):
print("write value fail for characteristic ", characteristic.uuid, ", error, ", error)
def main():
arg_parser = ArgumentParser(description="GATT SDK Demo")
arg_parser.add_argument(
'--adapter',
default='hci0',
help="Name of Bluetooth adapter, defaults to 'hci0'")
arg_parser.add_argument(
'--message',
metavar='string',
type=str,
help="String send by ble, usable with --connect only. All 'n' of the message will be remplace by '\ n' ")
arg_commands_group = arg_parser.add_mutually_exclusive_group(required=True)
arg_commands_group.add_argument(
'--power-on',
action='store_true',
help="Powers the adapter on")
arg_commands_group.add_argument(
'--power-off',
action='store_true',
help="Powers the adapter off")
arg_commands_group.add_argument(
'--powered',
action='store_true',
help="Print the adapter's power state")
arg_commands_group.add_argument(
'--discover',
action='store_true',
help="Lists all nearby GATT devices")
arg_commands_group.add_argument(
'--discover-filter',
metavar='filter',
type=str,
help="Lists all nearby GATT devices containing filter in their MAC address or alias")
arg_commands_group.add_argument(
'--connect',
metavar='address',
type=str,
help="Connect to a GATT device with a given MAC address")
arg_commands_group.add_argument(
'--auto',
metavar='address',
type=str,
help="Connect and automatically reconnect to a GATT device with a given MAC address")
arg_commands_group.add_argument(
'--disconnect',
metavar='address',
type=str,
help="Disconnect a GATT device with a given MAC address")
args = arg_parser.parse_args()
global device_manager
device_manager = AnyDeviceManager(adapter_name=args.adapter)
if args.power_on:
device_manager.is_adapter_powered = True
print("Powered on")
return
elif args.power_off:
device_manager.is_adapter_powered = False
print("Powered off")
return
elif args.powered:
print("Powered: ", device_manager.is_adapter_powered)
return
if args.discover:
device_manager.start_discovery()
if args.discover_filter:
print("discover filter : ", args.discover_filter)
device_manager.discover_filter = args.discover_filter
device_manager.start_discovery()
elif args.connect:
device = AnyDevice(mac_address=args.connect, manager=device_manager)
if args.message:
device.message = (args.message.replace('%n','\n'))
device.connect()
elif args.auto:
device = AnyDevice(mac_address=args.auto, manager=device_manager, auto_reconnect=True)
device.connect()
elif args.disconnect:
device = AnyDevice(mac_address=args.disconnect, manager=device_manager)
device.disconnect()
return
thread = Thread(target=device_manager.run)
thread.start()
print("Terminate with Ctrl+C")
print("Started Thread.")
try:
while True:
if device_manager.nb_device_connected:
# If ESCAPE key is pressed you can start to write the message, ENTER to send it to the device
if keyboard.is_pressed(chr(27)):
tcflush(sys.stdin, TCIFLUSH)
message = bytearray(input(">> ").replace('%n','\n').encode())
device.transmition.write_value(message)
pass
except KeyboardInterrupt:
print('KeyboardInterrupt!')
if device_manager.nb_device_connected:
device.disconnect()
device_manager.quit()
thread.join()
if __name__ == '__main__':
main()