jpconstantineau / BlueMicro_HID_Arduino_Library

Universal BLE/USB HID Library
Other
21 stars 0 forks source link

Cannot swap BLE connections or trigger manual disconnect. #5

Open bobsupercow opened 2 years ago

bobsupercow commented 2 years ago

With the current setup, I cannot trigger a BLE disconnect to trigger pairing mode, or swap to another, already paired device.

Use case is an NRF52840 that is sealed up in a case and not easily accessible. Ideally, we should be able to trigger either a swap or disconnect/pairing programmatically, or even via a particular keycode.

Can you present the underlying Bluefruit.connection for this.

Similar code using Bluefruit library directly:

uint16_t connectionHandle = 0;
BLEConnection* connection = NULL;

connectionHandle = Bluefruit.connHandle();
connection = Bluefruit.Connection(connectionHandle);

delay(2000);
connection->disconnect();
void bleDisconnectAll(){
  for (int i = 0; i < MAXIMUM_SWAP_CONNECTIONS; ++i) {
    if (conn_handles[i] != INVALID_CONN_HANDLE) {
      if (Bluefruit.connected(conn_handles[i]))Bluefruit.disconnect(conn_handles[i]) ;
    }
  }
}
bobsupercow commented 2 years ago

Tested following addition successfully. Could be significantly more robust since this only handles a single connection handle.

Call from my code: bluemicro_hid.disconnectBLE();

hid_queues.cpp

void HID_Queues::disconnectBLE()
{
  uint16_t connectionHandle = 0;
  BLEConnection* connection = NULL;

  connectionHandle = Bluefruit.connHandle();
  connection = Bluefruit.Connection(connectionHandle);

  delay(2000);
  connection->disconnect();
  Bluefruit.Advertising.stop();
  Bluefruit.Advertising.clearData();
  Bluefruit.ScanResponse.clearData(); // add this
  Bluefruit.Periph.clearBonds();
  Bluefruit.Central.clearBonds();
  delay(2000);
  setupBLE(_manufacturer,_model,_power);
}
jpconstantineau commented 2 years ago

I want to split this in different issues. this one for forcing a disconnect. A new one for pairing to multiple devices.

I have functionality in my BlueMicro_BLE firmware here I want to move to this library so that multiple profiles can be used.

Note that clearbonds completely disconnects and "forgets" the connection details on the device but not on the computer. Reconnecting to the computer might need to go through the process of "forgetting" on the computer and re-connecting/re-pairing. Using profiles, you don't loose the pairing information, you just create a new pair to use with another computer. This would be a better experience for most people.