T-vK / ESP32-BLE-Mouse

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

Resume advertising after the disconnect #57

Open windmaomao opened 9 months ago

windmaomao commented 9 months ago

It occurred to me, sometimes the mouse gets disconnected. And afterwards we can't initialize the connection anymore. So I dug into the code and learned from another bluetooth server code. Can I suggest to add the following lines to the disconnect code.

  void onDisconnect(BLEServer *pServer)
  {
    deviceConnected = false;

    BLEAdvertising *pAdvertising;
    pAdvertising = pServer->getAdvertising();
    pAdvertising->start();
  }

In our repo, it'll be inside BleConnectionStatus. I can raise a PR if you think this is not a bad idea, I can even add a flag to enable/disable it.

alextrical commented 8 months ago

Hi, I'm having the same issue as you have mentioned here. However I'm struggling to see how to apply the fixed code that you have quoted.

the contents of BleConnectionStatus.cpp are the following

#include "BleConnectionStatus.h"

BleConnectionStatus::BleConnectionStatus(void) {
}

void BleConnectionStatus::onConnect(BLEServer* pServer)
{
  this->connected = true;
  BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
  desc->setNotifications(true);
}

void BleConnectionStatus::onDisconnect(BLEServer* pServer)
{
  this->connected = false;
  BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
  desc->setNotifications(false);
}
windmaomao commented 8 months ago

i created this PR to address this, and I have used this for a while, it's quite stable after this change.

https://github.com/T-vK/ESP32-BLE-Mouse/pull/62/files

alextrical commented 8 months ago

Awesome, thank you for clarifying. I think i was close but was missing the final piece to get it working reliably.

Its also helped me solve connecting one "Mouse" to multiple clients, by appending the same snippet into the onConnect block

coofercat commented 2 months ago

I just tried your patch, and also found things work much better this way - random and intentional disconnects all seem to be handled gracefully with it in place.