Example: 50-Arduino-Boards/Nano-33-BLE-Sense/10-Bluetooth/ble_blinky.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 使用App Store中的nRF Connect,连接到Nano并写入1/0以控制LED。

import time
from board import LED
from ubluepy import Service, Characteristic, UUID, Peripheral, constants


def event_handler(id, handle, data):
    global periph
    global service
    if id == constants.EVT_GAP_CONNECTED:
        pass
    elif id == constants.EVT_GAP_DISCONNECTED:
        # 重新启动广告
        periph.advertise(device_name="Nano Blinky", services=[service])
    elif id == constants.EVT_GATTS_WRITE:
        LED(1).on() if int(data[0]) else LED(1).off()


# 开始时LED(1)关闭
LED(1).off()

notif_enabled = False
uuid_service = UUID("0x1523")
uuid_led = UUID("0x1525")

service = Service(uuid_service)
char_led = Characteristic(uuid_led, props=Characteristic.PROP_WRITE)
service.addCharacteristic(char_led)

periph = Peripheral()
periph.addService(service)
periph.setConnectionHandler(event_handler)
periph.advertise(device_name="Nano Blinky", services=[service])

while True:
    time.sleep_ms(500)

results matching ""

    No results matching ""