1

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!

Jess
  • 51
  • 7
  • Can you follow this [guidance](https://github.com/Microsoft/busiotools/tree/master/bluetooth/tracing) and use the tool to collect the log and share it with me? This will be more helpful to find the problem. – Faywang - MSFT Nov 25 '19 at 07:18
  • Hi, thank you for the comment! I traced 3 phones, but unfortunately, my company blocked github, dropbox, google drive and so on that I cannot upload the files... :( Can I know your e-mail address please? – Jess Nov 25 '19 at 08:57
  • Based on our policy, we can't share you about the private information including e-mail address. So you need to find other shareable platforms to upload your log. – Faywang - MSFT Nov 25 '19 at 09:22
  • oh ok I'll then wait till I get back home and upload the files! – Jess Nov 25 '19 at 09:27
  • OK, I will check it as soon as possible after you uploading it. – Faywang - MSFT Nov 25 '19 at 09:29
  • Thank you! I just got back home and here's the [link](https://github.com/Jess103/Debug-BthTracing) – Jess Nov 25 '19 at 11:17
  • [Here](https://stackoverflow.com/questions/59062163/automatic-ble-disconnect-connect-windows10-android) I opened up a new session for V40 and Note5 traces. I explained the circumstance in more detail there :) – Jess Nov 27 '19 at 04:52
  • I have replied you in that thread, and this failure sounds like a side effect of the same issue. – Faywang - MSFT Nov 28 '19 at 06:16
  • Thank you! I checked it and replied there! :) – Jess Nov 28 '19 at 06:37
  • OK, I will check it. – Faywang - MSFT Nov 28 '19 at 06:42

0 Answers0