我正在制作BLE鼠标。与ESP32 我正在使用的代码可以工作,但是非常精巧。 要成功连接,这是您必须要做的。
如果您没有在正确的时机按按钮,那将不起作用。
如果先断开连接再重新连接,鼠标将无法工作,甚至在Windows中也认为鼠标已连接。
这是我用于BLE服务器的代码
void taskServer(void*){
BLEDevice::init("ESP32_Omni_Device");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyCallbacks());
hid = new BLEHIDDevice(pServer);
input = hid->inputReport(1); // <-- input REPORTID from report map
std::string name = "Assist_IoT";
hid->manufacturer()->setValue(name);
hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
hid->hidInfo(0x00,0x01);
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setKeySize();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
hid->setBatteryLevel(5);
hid->startServices();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
// The setAppearance function only changes what is passed in the adv packet...it does not set the communication protocol
//pAdvertising->setAppearance(HID_KEYBOARD);
//pAdvertising->setAppearance(HID_JOYSTICK);
pAdvertising->setAppearance(HID_MOUSE);
pAdvertising->addServiceUUID(hid->hidService()->getUUID());
BLEDevice::startAdvertising();
Serial.print("Advertising started!");
vTaskDelay(portMAX_DELAY);
};
我在设置函数的开头调用此代码
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
xTaskCreate(taskServer, "server", 20000, NULL, 5, NULL);
/*..... Uninmportant code ommited*/
}
此代码完成后,主循环开始。 我对此很陌生,甚至不知道从哪里开始,所以任何建议都值得赞赏。 过去2天中的大部分时间都花在了使用HID描述符以及如何使用它们上。