繁体   English   中英

如何使用Micropython直接连接ESP32 BLE和手机蓝牙?

[英]How Can I Directly Connect ESP32 BLE with Mobile Bluetooth using Micropython?

我正在尝试在没有蓝牙串行终端应用程序帮助的情况下直接将 esp32 BLE 与手机连接。 我使用 Thonny IDE 进行编码。 当我尝试直接与移动设备连接时,出现的是弹出窗口。

https://i.stack.imgur.com/fISYr.jpg

这是我使用的代码

 from machine import Pin, Timer, SoftI2C from time import sleep_ms import ubluetooth from esp32 import raw_temperature from hdc1080 import HDC1080 class BLE(): def __init__(self, name): self.name = name self.ble = ubluetooth.BLE() self.ble.active(True) self.led = Pin(2, Pin.OUT) self.timer1 = Timer(0) self.timer2 = Timer(1) self.disconnected() self.ble.irq(self.ble_irq) self.register() self.advertiser() def connected(self): self.timer1.deinit() self.timer2.deinit() def disconnected(self): self.timer1.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(1)) sleep_ms(200) self.timer2.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(0)) def ble_irq(self, event, data): if event == 1: '''Central disconnected''' self.connected() self.led(1) elif event == 2: '''Central disconnected''' self.advertiser() self.disconnected() elif event == 3: '''New message received''' buffer = self.ble.gatts_read(self.rx) message = buffer.decode('UTF-8').strip() print(message) if message == 'red_led': red_led.value(not red_led.value()) print('red_led', red_led.value()) ble.send('red_led' + str(red_led.value())) if message == 'read_temp': print(sensor.read_temperature(True)) ble.send(str(sensor.read_temperature(True))) if message == 'read_hum': print(sensor.read_humidity()) ble.send(str(sensor.read_humidity())) def register(self): # Nordic UART Service (NUS) NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E' RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E' TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E' BLE_NUS = ubluetooth.UUID(NUS_UUID) BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE) BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_NOTIFY) BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,)) SERVICES = (BLE_UART, ) ((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES) def send(self, data): self.ble.gatts_notify(0, self.tx, data + '\n') def advertiser(self): name = bytes(self.name, 'UTF-8') self.ble.gap_advertise(100, bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name) # test i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) sensor = HDC1080(i2c) red_led = Pin(2, Pin.OUT) ble = BLE("ESP32")

我可以使用蓝牙串行终端应用程序连接但我的问题是如何在不使用蓝牙串行终端应用程序的情况下直接连接

我相信我也在互联网上的某个地方看到了这段代码。 不幸的是,这是一个过时的代码,它不适用于当前版本的 ubluetooth。 这是来自 MicroPython 固件“esp32-20220618-v1.19.1.bin”的 ubluetooth.BLE 的快速列表

>>> import ubluetooth

>>> dir(ubluetooth.BLE)

['__class__', '__name__', '__bases__', '__dict__', 'active', 'config', 'gap_advertise', 'gap_connect', 'gap_disconnect', 'gap_scan', 'gattc_discover_characteristics', 'gattc_discover_descriptors', 'gattc_discover_services', 'gattc_exchange_mtu', 'gattc_read', 'gattc_write', 'gatts_indicate', 'gatts_notify', 'gatts_read', 'gatts_register_services', 'gatts_set_buffer', 'gatts_write', 'irq']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM