-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble_data_transfer.py
45 lines (38 loc) · 1.61 KB
/
ble_data_transfer.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
import bluetooth
import ubinascii
from ble_advertising import advertising_payload
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_INDICATE_DONE = const(20)
class BLEManager:
def __init__(self, ble, name, service):
self._ble = ble
self._ble.active(True)
self._ble.irq(self._irq)
((self._handle,),) = self._ble.gatts_register_services((service,))
self._connections = set()
if len(name) == 0:
name = 'Pico %s' % ubinascii.hexlify(self._ble.config('mac')[1],':').decode().upper()
print('Device name %s' % name)
self._payload = advertising_payload(name=name, services=(service[0],))
self._advertise()
def _irq(self, event, data):
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _ = data
self._connections.add(conn_handle)
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _ = data
self._connections.remove(conn_handle)
self._advertise()
elif event == _IRQ_GATTS_INDICATE_DONE:
conn_handle, value_handle, status = data
def update_data(self, data, notify=False, indicate=False):
self._ble.gatts_write(self._handle, data)
if notify or indicate:
for conn_handle in self._connections:
if notify:
self._ble.gatts_notify(conn_handle, self._handle)
if indicate:
self._ble.gatts_indicate(conn_handle, self._handle)
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)