T-vK / ESP32-BLE-Mouse

Bluetooth LE Mouse library for the ESP32 (Arduino IDE compatible)
681 stars 134 forks source link

BLE won't reconnect after bluetooth is stopped and restarted. #39

Open pjenks-accudyne opened 2 years ago

pjenks-accudyne commented 2 years ago

Board: ESP32-C3

If I use the code template below I am unable to reconnect to the board with my PC. The board auto-connects fine with the first instance of bleMouse.begin() but does not reconnect after bluetooth is stopped and restarted. If I use deep sleep or reset the board it will reconnect without issues. The mouse commands are still sent and bleMouse.isConnected returns true after bluetooth is restarted, but there is no connection shown on my PC and it does not receive the mouse commands. I also receive the error message shown below after bluetooth is restarted.

Code template:

bleMouse.begin();
while(bleMouse.isConnected) {
  // send mouse commands
}
bleMouse.end();
btStop();
// do something else
btStart();
bleMouse.begin();
while(bleMouse.isConnected) {
  // send mouse commands
}

Error message: E (80831) BT_HCI: command_timed_out hci layer timeout waiting for response to a command. opcode: 0x2008

jellybins commented 2 years ago

the bleMouse.end() is an empty func; i have same problem and resolved it by soft restart the dev board ;detail in #37

pjenks-accudyne commented 2 years ago

I noticed that too and updated the bleMouse.end() function to kill the task created by bleMouse.begin(), but that didn't fix the problem either. I'd like to avoid a soft restart if possible.

From BleMouse.cpp:

void BleMouse::begin(void)
{
  xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, &this->taskHandle);
}

void BleMouse::end(void)
{
  vTaskDelete(this->taskHandle);
}

From BleMouse.h:

class BleMouse {
private:
  // some declarations
public:
  // some declarations
  TaskHandle_t taskHandle;
protected:
  // some declarations
};
Apofus2018 commented 1 year ago

I used the solution from above and added the following if(bleMouse.isConnected()) { //blah //blah } else { bleMouse.end(); delay(1000); bleMouse.begin(); } Mouse connected fine .....

SmuleFlash commented 6 months ago

I tired the solution above, but it nonstop doing bleMouse.begin() after I cycle the iPhone Bluetooth on/off. Therefore I add isConnect() while loop to wait for connection after bleMouse.begin() but it never get out of the isConnected loop. Although Bluetooth connection indicate connected on iPhone after switch off, this solution still unable to reconnect properly. Anyone can help?