esp32S3 发送广播

import ubluetooth
from ubluetooth import BLE
from machine import Pin
import time

led = Pin(2, Pin.OUT)  # create LED object from pin2,Set Pin2 to output
import esp

ble = BLE()
ble.active(True)

SERVER_1_UUID = ubluetooth.UUID(0x9011)
CHAR_A_UUID = ubluetooth.UUID(0x9012)
CHAR_B_UUID = ubluetooth.UUID(0x9013)

CHAR_A = (CHAR_A_UUID, ubluetooth.FLAG_READ | ubluetooth.FLAG_NOTIFY,)
CHAR_B = (CHAR_B_UUID, ubluetooth.FLAG_WRITE | ubluetooth.FLAG_NOTIFY,)
SERVER_1 = (SERVER_1_UUID, (CHAR_A, CHAR_B,),)
SERVICES = (SERVER_1, )
((char_a, char_b), ) = ble.gatts_register_services(SERVICES)

ble.gap_advertise(100, adv_data=b'\x02\x01\x05\x05\x09\x42\x69\x62\x69')


def ble_irq(event, data):
    print(event)

    if event == 1:  # 蓝牙已经连接
        led.value(0)
        print(data)
        conn_handle = data
        ble.gap_pair(conn_handle)
        ble.gap_passkey(conn_handle, _PASSKEY_ACTION_INPUT, '1234')
    elif event == 2:  # 蓝牙断开连接
        ble.gap_advertise(100, adv_data=b'\x02\x01\x05\x05\x09\x42\x69\x62\x69')
        led.value(1)
    elif event == 3:  # Write事件
        conn_handle, char_handle = data
        print(data)
        print(char_handle)
        print(conn_handle)
        buffer = ble.gatts_read(char_handle)
        print(buffer)
        if buffer == b'A':
            ble.gatts_notify(conn_handle, char_handle, b'B')


ble.irq(ble_irq)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值