2019-10-15 07:45:21 +01:00
|
|
|
# This example demonstrates a peripheral implementing the Nordic UART Service (NUS).
|
|
|
|
|
2023-08-24 14:28:53 +01:00
|
|
|
# This example demonstrates the low-level bluetooth module. For most
|
|
|
|
# applications, we recommend using the higher-level aioble library which takes
|
|
|
|
# care of all IRQ handling and connection management. See
|
|
|
|
# https://github.com/micropython/micropython-lib/tree/master/micropython/bluetooth/aioble
|
|
|
|
|
2019-10-15 07:45:21 +01:00
|
|
|
import bluetooth
|
|
|
|
from ble_advertising import advertising_payload
|
|
|
|
|
|
|
|
from micropython import const
|
|
|
|
|
2020-05-11 12:20:07 +01:00
|
|
|
_IRQ_CENTRAL_CONNECT = const(1)
|
|
|
|
_IRQ_CENTRAL_DISCONNECT = const(2)
|
|
|
|
_IRQ_GATTS_WRITE = const(3)
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2020-08-27 00:13:25 +01:00
|
|
|
_FLAG_WRITE = const(0x0008)
|
|
|
|
_FLAG_NOTIFY = const(0x0010)
|
|
|
|
|
2019-10-15 07:45:21 +01:00
|
|
|
_UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
|
|
|
|
_UART_TX = (
|
|
|
|
bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"),
|
2020-08-27 00:13:25 +01:00
|
|
|
_FLAG_NOTIFY,
|
2019-10-15 07:45:21 +01:00
|
|
|
)
|
|
|
|
_UART_RX = (
|
|
|
|
bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"),
|
2020-08-27 00:13:25 +01:00
|
|
|
_FLAG_WRITE,
|
2019-10-15 07:45:21 +01:00
|
|
|
)
|
|
|
|
_UART_SERVICE = (
|
|
|
|
_UART_UUID,
|
|
|
|
(_UART_TX, _UART_RX),
|
2020-02-27 04:36:53 +00:00
|
|
|
)
|
2019-10-15 07:45:21 +01:00
|
|
|
|
|
|
|
# org.bluetooth.characteristic.gap.appearance.xml
|
|
|
|
_ADV_APPEARANCE_GENERIC_COMPUTER = const(128)
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2019-10-15 07:45:21 +01:00
|
|
|
class BLEUART:
|
|
|
|
def __init__(self, ble, name="mpy-uart", rxbuf=100):
|
|
|
|
self._ble = ble
|
|
|
|
self._ble.active(True)
|
2020-09-24 18:15:22 +01:00
|
|
|
self._ble.irq(self._irq)
|
2019-10-15 07:45:21 +01:00
|
|
|
((self._tx_handle, self._rx_handle),) = self._ble.gatts_register_services((_UART_SERVICE,))
|
2019-10-28 06:33:29 +00:00
|
|
|
# Increase the size of the rx buffer and enable append mode.
|
|
|
|
self._ble.gatts_set_buffer(self._rx_handle, rxbuf, True)
|
2019-10-15 07:45:21 +01:00
|
|
|
self._connections = set()
|
|
|
|
self._rx_buffer = bytearray()
|
|
|
|
self._handler = None
|
2019-10-22 02:31:40 +01:00
|
|
|
# Optionally add services=[_UART_UUID], but this is likely to make the payload too large.
|
2019-10-15 07:45:21 +01:00
|
|
|
self._payload = advertising_payload(name=name, appearance=_ADV_APPEARANCE_GENERIC_COMPUTER)
|
|
|
|
self._advertise()
|
|
|
|
|
|
|
|
def irq(self, handler):
|
|
|
|
self._handler = handler
|
|
|
|
|
|
|
|
def _irq(self, event, data):
|
|
|
|
# Track connections so we can send notifications.
|
|
|
|
if event == _IRQ_CENTRAL_CONNECT:
|
|
|
|
conn_handle, _, _ = data
|
|
|
|
self._connections.add(conn_handle)
|
|
|
|
elif event == _IRQ_CENTRAL_DISCONNECT:
|
|
|
|
conn_handle, _, _ = data
|
|
|
|
if conn_handle in self._connections:
|
|
|
|
self._connections.remove(conn_handle)
|
|
|
|
# Start advertising again to allow a new connection.
|
|
|
|
self._advertise()
|
|
|
|
elif event == _IRQ_GATTS_WRITE:
|
|
|
|
conn_handle, value_handle = data
|
|
|
|
if conn_handle in self._connections and value_handle == self._rx_handle:
|
|
|
|
self._rx_buffer += self._ble.gatts_read(self._rx_handle)
|
|
|
|
if self._handler:
|
|
|
|
self._handler()
|
|
|
|
|
|
|
|
def any(self):
|
|
|
|
return len(self._rx_buffer)
|
|
|
|
|
|
|
|
def read(self, sz=None):
|
|
|
|
if not sz:
|
|
|
|
sz = len(self._rx_buffer)
|
|
|
|
result = self._rx_buffer[0:sz]
|
|
|
|
self._rx_buffer = self._rx_buffer[sz:]
|
|
|
|
return result
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
for conn_handle in self._connections:
|
|
|
|
self._ble.gatts_notify(conn_handle, self._tx_handle, data)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
for conn_handle in self._connections:
|
|
|
|
self._ble.gap_disconnect(conn_handle)
|
|
|
|
self._connections.clear()
|
|
|
|
|
|
|
|
def _advertise(self, interval_us=500000):
|
|
|
|
self._ble.gap_advertise(interval_us, adv_data=self._payload)
|
|
|
|
|
|
|
|
|
2019-10-21 15:04:48 +01:00
|
|
|
def demo():
|
|
|
|
import time
|
|
|
|
|
|
|
|
ble = bluetooth.BLE()
|
|
|
|
uart = BLEUART(ble)
|
|
|
|
|
|
|
|
def on_rx():
|
|
|
|
print("rx: ", uart.read().decode().strip())
|
|
|
|
|
|
|
|
uart.irq(handler=on_rx)
|
|
|
|
nums = [4, 8, 15, 16, 23, 42]
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
uart.write(str(nums[i]) + "\n")
|
|
|
|
i = (i + 1) % len(nums)
|
|
|
|
time.sleep_ms(1000)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
uart.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
demo()
|