T-vK / ESP32-BLE-Keyboard

Bluetooth LE Keyboard library for the ESP32 (Arduino IDE compatible)
2.4k stars 402 forks source link

Won't re-connect on Windows 10 after Power Cycle Off-On Release v0.3.0 #168

Open creepscompilation opened 2 years ago

creepscompilation commented 2 years ago

I am in the final phase of testing and I have to use release v0.3.0 to get Android to work at all..

So, with release v0.3.0, Android and Apple iOS work GREAT !!! On Windows 10 however, I can PAIR and Connect OK, and the BLE Keyboard works GREAT, until I reset the ESP32 module either power off-on, or boot button. After that, "isConnected" comes up as connected, but no keyboard data outputs to Windows 10.

I am forced to UNPAIR the BLE keyboard from Windows 10, the PAIR again. This test is repeatable and confirmed. This probem does not exist with Android or Apple iOS, only Windows 10.

creepscompilation commented 2 years ago

Correction: Won't re-connect on Windows 8 also.. Same issue with Windows 10, Windows 8

creepscompilation commented 2 years ago

RESOLVED: I took the advice from SunandMittal..

Starting with release v0.3.2 filename = "BleKeyboard.cpp"

Find this line: pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);

Replace the above line to the following from release v0.3.0 pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);

So, far.. Android connects, re-connects, keyboard codes work Apple iOS connects, re-connects, keyboard codes work Windows 10 connects, re-connects, keyboard codes work

mvermand22 commented 2 years ago

I have version v0.3.0 with this exact same issue. What do I need to do to fix it? In v0.3.0, BleKeyboard.cpp line 123 is "pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);". I tried replacing with 'pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);' but that does not do anything. Any advice please! Thanks :-)

creepscompilation commented 2 years ago

Hello mvermand22,

I started with v0.3.2, then I changed that line in the code, I did not test this on any earlier releases. I have attached the library that I modified, NOTE: I call it v0.3.3 for my own reference, this is NOT an official release from this GIT HUB page.

ESP32-BLE-Keyboard 0.3.3.zip

jnsw commented 1 year ago

Hello mvermand22,

I started with v0.3.2, then I changed that line in the code, I did not test this on any earlier releases. I have attached the library that I modified, NOTE: I call it v0.3.3 for my own reference, this is NOT an official release from this GIT HUB page.

ESP32-BLE-Keyboard 0.3.3.zip

thanks, this worked for me!

ekoslav commented 1 year ago

On Windows 11 issue still remains. On the first connection the keyboard works, however if it disconnects, and connects back, no keys are sent. Does anyone know any tool that will log the messages from BT devices on Win 11? I check the system logs, but they only show when the device was first paired.

mayaku2go commented 1 year ago

I had the same problem and created just an account for this. Here is the solution: https://github.com/nkolban/esp32-snippets/issues/632

Open bleKeyboard.cpp and add the two lines to the onConnect() function.

uint8_t val[] = {0x01, 0x00};
desc->setValue(val, 2);

The whole function should look like this:

void BleKeyboard::onConnect(BLEServer* pServer) {
  this->connected = true;

#if !defined(USE_NIMBLE)

        uint8_t val[] = {0x01, 0x00};
    BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
    desc->setNotifications(true);
        desc->setValue(val, 2);
    desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
    desc->setNotifications(true);
        desc->setValue(val, 2);
#endif // !USE_NIMBLE

}

Update: Sorry my fault. It is the onConnect() function.

vcchm commented 1 year ago

@mayaku2go : you did not add the lines you recommend to add. Where should they be ? At the beginning ? At the end ?

mayaku2go commented 1 year ago

@vcchm My fault. Was the wrong function. I updated the code ^^

vcchm commented 1 year ago

Thanks ! Your correction only changes the code when not in the "Nimble" mode. But the problem happens whatever the mode - Nimble or not. Are you sure you changed the code enough?

vcchm commented 1 year ago

Also are you sur you dont need desc->setValue(val, 2); after the first desc->setNotifications(true); too ???

mayaku2go commented 1 year ago

Haven't tested it yet as I still don't understand BLE completely XD Maybe one of the developers are able to answer this question. It worked for me and I am trying to establish a secure connection with no input or output and static passkey right now. But this is a point worth trying.

mayaku2go commented 1 year ago

@vcchm You are right. Without the second desc->setValue(val, 2); the ESP32 isn't recognized as an HID Keyboard. Code is updated ^^