概述
import ubluetooth
from ubluetooth import BLE
from machine import Pin, UART
import time
import esp
from micropython import const
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_IRQ_GATTS_READ_REQUEST = const(4)
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)
_IRQ_PERIPHERAL_CONNECT = const(7)
_IRQ_PERIPHERAL_DISCONNECT = const(8)
_IRQ_GATTC_SERVICE_RESULT = const(9)
_IRQ_GATTC_SERVICE_DONE = const(10)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
_IRQ_GATTC_DESCRIPTOR_DONE = const(14)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)
_IRQ_GATTS_INDICATE_DONE = const(20)
_IRQ_MTU_EXCHANGED = const(21)
_IRQ_L2CAP_ACCEPT = const(22)
_IRQ_L2CAP_CONNECT = const(23)
_IRQ_L2CAP_DISCONNECT = const(24)
_IRQ_L2CAP_RECV = const(25)
_IRQ_L2CAP_SEND_READY = const(26)
_IRQ_CONNECTION_UPDATE = const(27)
_IRQ_ENCRYPTION_UPDATE = const(28)
_IRQ_GET_SECRET = const(29)
_IRQ_SET_SECRET = const(30)
uart1 = UART(1, baudrate=115200, tx=33, rx=32)
led = Pin(2, Pin.OUT) # create LED object from pin2,Set Pin2 to output
ble = BLE()
ble.active(True)
led.value(1)
SPP_SERVER_UUID = ubluetooth.UUID('035892AB-EDCB-5678-4321-A1B2C3D4E5F6')
SPP_CHAR_UUID = ubluetooth.UUID('035892AB-EDCB-5678-4321-A1B2C3D4E5F7')
SPP_CHAR = (SPP_CHAR_UUID, ubluetooth.FLAG_WRITE | ubluetooth.FLAG_NOTIFY,)
SPP_SERVER = (SPP_SERVER_UUID, (SPP_CHAR,),)
SERVICES = (SPP_SERVER,)
((spp_char,),) = ble.gatts_register_services(SERVICES)
ble.gap_advertise(20, adv_data=b'x02x01x05x05x09x42x69x62x69')
ble.gap_advertise(50,
adv_data=b'x02x01x06x1AxFFx4Cx00x02x15xE2xC5x6DxB5xDFxFBx48xD2xB0x60xD0xF5xA7x10x96xE0x88xABx04x01xC3')
def ble_irq(event, data):
if event == _IRQ_CENTRAL_CONNECT: # 蓝牙已经连接
conn_handle, char_handle, addr = data
print(conn_handle)
print(char_handle)
print(addr)
led.value(0)
elif event == _IRQ_CENTRAL_DISCONNECT: # 蓝牙断开连接
conn_handle, char_handle, addr = data
print(conn_handle)
print(char_handle)
print(addr)
ble.gap_advertise(100, adv_data=b'x02x01x05x05x09x42x69x62x69')
led.value(1)
elif event == 3: # Write事件
conn_handle, char_handle = data
print(conn_handle)
print(char_handle)
buffer = ble.gatts_read(char_handle)
uart1.write(buffer)
ble.irq(ble_irq)
while True:
if uart1.any() != 0:
r = uart1.read()
print(r)
ble.gatts_notify(0, spp_char, r)
time.sleep_ms(1000)
print("bili")
ble.gap_advertise(30, adv_data=b'x02x01x05x05x09x42x69x62x69')
time.sleep_ms(1000)
print("iBeacon")
ble.gap_advertise(50,adv_data=
b'x02' # length
b'x01' # type
b'x06' # Flags: LE General Discoverable Mode, BR/EDR is disabled.
# Manufacturer specific data * /
b'x1A' # length of field
b'xFF' # type of field
# The first two data octets shall contain a company identifier code from
# the Assigned Numbers - Company Identifiers document
# 0x004C = Apple
b'x4Cx00'
# Beacon type
# 0x0215 is iBeacon
b'x02x15'
# 128 bit / 16 byte UUID
b'xE2xC5x6DxB5xDFxFBx48xD2xB0x60xD0xF5xA7x10x96xE0'
# Beacon major number
# Set to 34987 and converted to correct format
b'x88xAB'
# Beacon minor number
# Set as 1025 and converted to correct format
b'x04x01'
# The Beacon's measured RSSI at 1 meter distance in dBm
# 0xC3 is -61dBm
b'xC3')
最后
以上就是义气蚂蚁为你收集整理的ESP32-BLE 普通广播和IBeacon交替广播的全部内容,希望文章能够帮你解决ESP32-BLE 普通广播和IBeacon交替广播所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复