Example: 10-Bluetooth/ble_blinky.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 蓝牙闪烁灯示例
#
# 使用应用商店中的nRFConnect应用,连接到开发板
# "mpy-blinky", and write bool (True/False) to control the LED.

import struct
import asyncio
import aioble
import bluetooth
from machine import LED
from micropython import const

# UUIDs
_LED_CONTROL_UUID = bluetooth.UUID(0x1815)
_LED_CONTROL_CHAR_UUID = bluetooth.UUID(0x2A56)
_ADV_APPEARANCE_GENERIC_TAG = const(512)
_ADV_INTERVAL_MS = 250_000

# LED setup
led = LED("LED_BLUE")

# GATT server
led_service = aioble.Service(_LED_CONTROL_UUID)
led_characteristic = aioble.Characteristic(led_service, _LED_CONTROL_CHAR_UUID, write=True, capture=True)
aioble.register_services(led_service)


async def led_control_task():
    while True:
        conn, data = await led_characteristic.written()
        led.value(struct.unpack("<B", data)[0])


async def peripheral_task():
    while True:
        async with await aioble.advertise(
            _ADV_INTERVAL_MS,
            name="mpy-blinky",
            services=[_LED_CONTROL_UUID],
            appearance=_ADV_APPEARANCE_GENERIC_TAG,
        ) as connection:
            print("Connection from", connection.device)
            await connection.disconnected()


async def main():
    await asyncio.gather(led_control_task(), peripheral_task())


asyncio.run(main())

results matching ""

    No results matching ""