|
发表于 2024-3-11 22:14:50
|
显示全部楼层
本帖最后由 fengqiluoye 于 2024-3-11 22:22 编辑
fengqiluoye 发表于 2024-3-11 22:12
大佬,你好,我现在发送关闭命令的时候显示包是{0x02,0x01,0x2e,0x00,0x0a,0x00,0x04,0x00,0x1b,0x1b,0x00, ...
from machine import Pin
from machine import Timer
from time import sleep_ms
import bluetooth
BLE_MSG = ""
KEYBOARD_REPORT = bytes([ # Report Description: describes what we communicate
0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x01, 0x19, 0x00, 0x2a, 0x9c,
0x02, 0x15, 0x00, 0x26, 0x9c, 0x02, 0x95, 0x01, 0x75, 0x10, 0x81, 0x00,
0x09, 0x02, 0xa1, 0x02, 0x05, 0x09, 0x19, 0x01, 0x29, 0x0a, 0x15, 0x01,
0x25, 0x0a, 0x95, 0x01, 0x75, 0x08, 0x81, 0x40, 0xc0, 0xc0, 0x06, 0x01,
0xff, 0x09, 0x01, 0xa1, 0x02, 0x85, 0x05, 0x09, 0x14, 0x75, 0x08, 0x95,
0x14, 0x15, 0x80, 0x25, 0x7f, 0x81, 0x22, 0x85, 0x04, 0x09, 0x04, 0x75,
0x08, 0x95, 0x01, 0x91, 0x02, 0xc0, 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01,
0x85, 0x0a, 0x75, 0x01, 0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7,
0x15, 0x00, 0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01,
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02,
0x95, 0x01, 0x75, 0x03, 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00,
0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 0x00, 0x29, 0xff, 0x81, 0x00, 0xc0,
])
class ESP32_BLE():
def __init__(self, name):
self.led = Pin(2, Pin.OUT)
self.timer1 = Timer(0)
self.name = name
self.ble = bluetooth.BLE()
self.ble.active(True)
self.ble.config(gap_name=name)
self.disconnected()
self.ble.irq(self.ble_irq)
self.register()
self.advertiser()
def connected(self):
self.led.value(1)
self.timer1.deinit()
def disconnected(self):
self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.value(not self.led.value()))
def ble_irq(self, event, data):
global BLE_MSG
if event == 1: #_IRQ_CENTRAL_CONNECT 手机链接了此设备
self.connected()
elif event == 2: #_IRQ_CENTRAL_DISCONNECT 手机断开此设备
self.advertiser()
self.disconnected()
elif event == 3: #_IRQ_GATTS_WRITE 手机发送了数据
pass
#buffer = self.ble.gatts_read(self.rx)
#BLE_MSG = buffer.decode('UTF-8').strip()
def register(self):
HIDS = (
bluetooth.UUID(0x1812), # Human Interface Device
(
(bluetooth.UUID(0x2A4A), bluetooth.FLAG_READ), # HID information
(bluetooth.UUID(0x2A4B), bluetooth.FLAG_READ), # HID report map
(bluetooth.UUID(0x2A4C), bluetooth.FLAG_WRITE), # HID control point
(bluetooth.UUID(0x2A4D), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY, ((bluetooth.UUID(0x2908), 1),)), # HID report / reference
(bluetooth.UUID(0x2A4D), bluetooth.FLAG_READ | bluetooth.FLAG_WRITE, ((bluetooth.UUID(0x2908), 1),)), # HID report / reference
(bluetooth.UUID(0x2A4E), bluetooth.FLAG_READ | bluetooth.FLAG_WRITE), # HID protocol mode
),
)
self.services = (HIDS,)
self.handles = self.ble.gatts_register_services(self.services)
def send(self, data):
self.ble.gatts_notify(0, self.h_repin, data)
def advertiser(self):
#设置BLE广播数据并开始广播
self.ble.gap_advertise(100, adv_data = b'\x02\x01\x06'
+ b'\x03\x03\x12\x18' #HID UUID
+ b'\x03\x19\xC1\x03' #设备外观为键盘
+ b'\x0D\x09' + "ESP Keyboard".encode("UTF-8"))
(self.h_info, self.h_map, _, self.h_repin, self.h_d1, self.h_repout, self.h_d2, self.h_model,) = self.handles[0]
# Write service characteristics
self.ble.gatts_write(self.h_info, b"\x11\x01\x00\x01") # HID info: ver=1.1, country=0, flags=normal
self.ble.gatts_write(self.h_map, KEYBOARD_REPORT) # HID input report map
self.ble.gatts_write(self.h_d1, b"\x01\x01") # HID reference: id=1, type=input
self.ble.gatts_write(self.h_d2, b"\x01\x01") # HID reference: id=1, type=output
self.ble.gatts_write(self.h_model, b"\x01") # HID Protocol Model: 0=Boot Model, 1=Report Model
if __name__ == "__main__":
ble = ESP32_BLE("ESP32BLE")
key = Pin(0,Pin.IN)#IO 0 用作按键
while True:
if key.value() == 0:
while key.value() == 0:
pass
print("11111")
ble.send(b'\x00\x00\x66\x00\x00\x00\x00')
sleep_ms(750)
ble.send(b'\x00\x00\x00\x00\x00\x00\x00')
这是我使用thony编写的代码,感觉都一样的,但是抓到的数据确实有问题的 |
|