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)
esp32S3 发送广播
最新推荐文章于 2024-11-08 14:34:47 发布