Client (Central): PC
Server (Peripheral): Mobile
Connect - Disconnect - Reconnect - Disconnect - Reconnect - ..
works like a charm when I disconnect on the client side. (i.e. I press disconnect btn on UWP app)
However, if I DISCONNECT ON THE SERVER SIDE, (i.e. I press disconnect btn on mobile app)
PC is unable to find the custom service. (i.e.I can only find 0x1801 and 0x1800)
- Sometimes I even find no services at all
Sometimes, it works well again if I disconnect on the PC side. (i.e. ... - Disconnect via mobile app - Connect [SERVICE SEARCH WEIRD] - Disconnect via PC app - Connect [WORKS WELL AGAIN] - ...)
But MOST OF THE TIME, I can never find the custom service even if I close mobile app / PC app. I NEED TO UNPAIR AND PAIR AGAIN.
- I pair w/ the phone before connecting to it, so I can re-use the bluetoothLeDevice.Id for reconnection)
Galaxy S9+, LG X Power: ALWAYS works well again if I disconnect on PC side
LG V40, Galaxy Note5: Most of the time (almost always) I need to unpair -> pair)
*This is another issue but just in case if anyone needs this info as well: V40 and Note5 keep fall into CONNECT state then DISCONNECT state SEVERAL TIMES before they really connect
The code below shows how I disconnect
private void Disconnect()
{
RemoveValueChangedHandler();
customCharacteristic?.Service?.Dispose();
customCharacteristic = null;
customService?.Dispose();
customService = null;
if (bluetoothLeDevice != null)
{
bluetoothLeDevice.ConnectionStatusChanged -= ConnectionStatusChangedHandler;
bluetoothLeDevice?.Dispose();
}
bluetoothLeDevice = null;
if (newSession != null)
{
newSession.MaintainConnection = false;
newSession.Dispose();
}
newSession = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
private async void RemoveValueChangedHandler()
{
if (subscribedForNotifications)
{
registeredCharacteristic.ValueChanged -= Characteristic_ValueChanged;
CCCDresult = await registeredCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None);
registeredCharacteristic = null;
subscribedForNotifications = false;
}
}
// Just in case if anyone needs to know how I subscribe for notification
private void AddValueChangedHandler()
{
if (!subscribedForNotifications)
{
registeredCharacteristic = customCharacteristic;
registeredCharacteristic.ValueChanged += Characteristic_ValueChanged;
subscribedForNotifications = true;
}
}
Does anybody know why is (re)connection working weird when I disconnect on the peripheral side?
Any help I would appreciate!