nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.36k stars 710 forks source link

BLECharacteristics notification settings trouble with multiple characteristics #1108

Open sporniket opened 2 years ago

sporniket commented 2 years ago

Context

I am programming a BLE device to be seen as a HID device combining Keyboard, mouse and gamepads. For now I am trying to get keyboard and mouse. I manage several BLECharacteristics created using BLEHIDDevice->inputReport(reportId)

I started my work using the sample keyboard demo.

The problem

I struggled for a time to get the mouse working, until I noticed in the traces that on calling BLECharacteristic->notify, the characteristic sending the mouse report claimed that notifications were disabled and thus abort ; the characteristic sending the keyboard report does not claim this an send normaly the report.

A call to BLECharacteristic->setNotifyProperty(true) does not solves the problem.

Workaround (partial)

A partial workaround : in BLEHIDDevice.cpp->inputReport(...), force the notification flag of characteristic p2902. It then works only on the first pairing after uploading the firmware

diff --git a/lib/cpp_utils/BLEHIDDevice.cpp b/lib/cpp_utils/BLEHIDDevice.cpp
index 46770b7..7cbd8b8 100644
--- a/lib/cpp_utils/BLEHIDDevice.cpp
+++ b/lib/cpp_utils/BLEHIDDevice.cpp
@@ -115,6 +115,7 @@ BLECharacteristic* BLEHIDDevice::inputReport(uint8_t reportID) {
        inputReportCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
        inputReportDescriptor->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
        p2902->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
+       p2902->setNotifications(true) ; //DSP : force notification 

        uint8_t desc1_val[] = { reportID, 0x01 };
        inputReportDescriptor->setValue((uint8_t*) desc1_val, 2);
sporniket commented 2 years ago

I will post my full code in a few days, since I know that my poc is "kind of" working. I need to prepare before publishing...

sporniket commented 2 years ago

My code can be browsed here : https://github.com/sporniket/kimchi-on-the-beach