ESP32-BLE 普通广播和IBeacon交替广播

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'\x02\x01\x05\x05\x09\x42\x69\x62\x69')
ble.gap_advertise(50,
                  adv_data=b'\x02\x01\x06\x1A\xFF\x4C\x00\x02\x15\xE2\xC5\x6D\xB5\xDF\xFB\x48\xD2\xB0\x60\xD0\xF5\xA7\x10\x96\xE0\x88\xAB\x04\x01\xC3')


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'\x02\x01\x05\x05\x09\x42\x69\x62\x69')
        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'\x02\x01\x05\x05\x09\x42\x69\x62\x69')
    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'\x4C\x00'
								#  Beacon type 
								#  0x0215 is iBeacon 
    b'\x02\x15'
								#  128 bit / 16 byte UUID 
    b'\xE2\xC5\x6D\xB5\xDF\xFB\x48\xD2\xB0\x60\xD0\xF5\xA7\x10\x96\xE0'
								#  Beacon major number 
								#  Set to 34987 and converted to correct format 
    b'\x88\xAB'
								#  Beacon minor number 
								#  Set as 1025 and converted to correct format 
    b'\x04\x01'
								#  The Beacon's measured RSSI at 1 meter distance in dBm 
								#  0xC3 is -61dBm 
    b'\xC3')


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值