ESP32-BLE-Mouse icon indicating copy to clipboard operation
ESP32-BLE-Mouse copied to clipboard

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

Open pjenks-accudyne opened this issue 2 years ago • 4 comments

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

pjenks-accudyne avatar Apr 28 '22 15:04 pjenks-accudyne

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

jellybins avatar Apr 29 '22 03:04 jellybins

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
};

pjenks-accudyne avatar Apr 29 '22 17:04 pjenks-accudyne

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 .....

Apofus2018 avatar Jun 01 '23 19:06 Apofus2018

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?

SmuleFlash avatar Jan 12 '24 19:01 SmuleFlash