-
Notifications
You must be signed in to change notification settings - Fork 19
/
example_ble_2.py
281 lines (226 loc) · 9.44 KB
/
example_ble_2.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
################################################################################
# COPYRIGHT(c) 2018 STMicroelectronics #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are met: #
# 1. Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# 2. Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# 3. Neither the name of STMicroelectronics nor the names of its #
# contributors may be used to endorse or promote products derived from #
# this software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" #
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE #
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE #
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE #
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR #
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF #
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS #
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN #
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) #
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
# POSSIBILITY OF SUCH DAMAGE. #
################################################################################
################################################################################
# Author: Davide Aliprandi, STMicroelectronics #
################################################################################
# DESCRIPTION
#
# This application example shows how to perform a Bluetooth Low Energy (BLE)
# scan, connect to a number of devices handled though dedicated threads, and get
# push notifications from all their features.
# IMPORT
from __future__ import print_function
import sys
import os
import time
import threading
from abc import abstractmethod
from blue_st_sdk.manager import Manager
from blue_st_sdk.manager import ManagerListener
from blue_st_sdk.node import NodeListener
from blue_st_sdk.feature import FeatureListener
from blue_st_sdk.features.audio.adpcm.feature_audio_adpcm import FeatureAudioADPCM
from blue_st_sdk.features.audio.adpcm.feature_audio_adpcm_sync import FeatureAudioADPCMSync
# PRECONDITIONS
#
# In case you want to modify the SDK, clone the repository and add the location
# of the "BlueSTSDK_Python" folder to the "PYTHONPATH" environment variable.
#
# On Linux:
# export PYTHONPATH=/home/<user>/BlueSTSDK_Python
# CONSTANTS
# Presentation message.
INTRO = """##################
# BlueST Example #
##################"""
# Bluetooth Scanning time in seconds (optional).
SCANNING_TIME_s = 5
# FUNCTIONS
#
# Printing intro.
#
def print_intro():
print('\n' + INTRO + '\n')
# INTERFACES
#
# Implementation of the interface used by the Manager class to notify that a new
# node has been discovered or that the scanning starts/stops.
#
class MyManagerListener(ManagerListener):
#
# This method is called whenever a discovery process starts or stops.
#
# @param manager Manager instance that starts/stops the process.
# @param enabled True if a new discovery starts, False otherwise.
#
def on_discovery_change(self, manager, enabled):
print('Discovery %s.' % ('started' if enabled else 'stopped'))
if not enabled:
print()
#
# This method is called whenever a new node is discovered.
#
# @param manager Manager instance that discovers the node.
# @param node New node discovered.
#
def on_node_discovered(self, manager, node):
print('New device discovered: %s.' % (node.get_name()))
#
# Implementation of the interface used by the Node class to notify that a node
# has updated its status.
#
class MyNodeListener(NodeListener):
#
# To be called whenever a node connects to a host.
#
# @param node Node that has connected to a host.
#
def on_connect(self, node):
print('Device %s connected.' % (node.get_name()))
#
# To be called whenever a node disconnects from a host.
#
# @param node Node that has disconnected from a host.
# @param unexpected True if the disconnection is unexpected, False otherwise
# (called by the user).
#
def on_disconnect(self, node, unexpected=False):
print('Device %s disconnected%s.' % \
(node.get_name(), ' unexpectedly' if unexpected else ''))
#
# Implementation of the interface used by the Feature class to notify that a
# feature has updated its data.
#
class MyFeatureListener(FeatureListener):
#
# To be called whenever the feature updates its data.
#
# @param feature Feature that has updated.
# @param sample Data extracted from the feature.
#
def on_update(self, feature, sample):
print('[%s %s]' % (feature.get_parent_node().get_name(), \
feature.get_parent_node().get_tag()))
print(feature)
class DeviceThread(threading.Thread):
"""Class to handle a device in a thread."""
def __init__(self, device, *args, **kwargs):
"""Constructor.
Args:
device (:class:`blue_st_sdk.node.Node`): The device to handle.
"""
super(DeviceThread, self).__init__(*args, **kwargs)
self._device = device
def run(self):
"""Run the thread."""
# Connecting to the device.
self._device.add_listener(MyNodeListener())
print('Connecting to %s...' % (self._device.get_name()))
if not self._device.connect():
print('Connection failed.\n')
return
# Getting features.
features = self._device.get_features()
# Enabling notifications.
for feature in features:
# For simplicity let's skip audio features.
if not isinstance(feature, FeatureAudioADPCM) and \
not isinstance(feature, FeatureAudioADPCMSync):
feature.add_listener(MyFeatureListener())
self._device.enable_notifications(feature)
# Getting notifications.
while True:
if self._device.wait_for_notifications(0.05):
pass
def get_device(self):
"""Get the handled device."""
return self._device
# MAIN APPLICATION
#
# Main application.
#
def main(argv):
# Printing intro.
print_intro()
try:
# Creating Bluetooth Manager.
manager = Manager.instance()
manager_listener = MyManagerListener()
manager.add_listener(manager_listener)
# Synchronous discovery of Bluetooth devices.
print('Scanning Bluetooth devices...\n')
manager.discover(SCANNING_TIME_s)
# Getting discovered devices.
discovered_devices = manager.get_nodes()
# Listing discovered devices.
if not discovered_devices:
print('No Bluetooth devices found. Exiting...\n')
sys.exit(0)
print('Available Bluetooth devices:')
i = 1
for device in discovered_devices:
print('%d) %s: [%s]' % (i, device.get_name(), device.get_tag()))
i += 1
# Selecting devices to connect.
selected_devices = []
while True:
while True:
choice = int(
input('\nSelect a device to connect to (\'0\' to finish): '))
if choice >= 0 and choice <= len(discovered_devices):
break
if choice == 0:
break
else:
device = discovered_devices[choice - 1]
selected_devices.append(device)
print('Device %s added.' % (device.get_name()))
device_threads = []
if len(selected_devices) > 0:
# Starting threads.
print('\nConnecting to selected devices and getting notifications '
'from all their features ("CTRL+C" to exit)...\n')
for device in selected_devices:
device_threads.append(DeviceThread(device).start())
else:
# Exiting.
manager.remove_listener(manager_listener)
print('Exiting...\n')
sys.exit(0)
# Getting notifications.
while True:
pass
except KeyboardInterrupt:
try:
# Exiting.
print('\nExiting...\n')
sys.exit(0)
except SystemExit:
os._exit(0)
if __name__ == "__main__":
main(sys.argv[1:])