(重新)在外围侧断开连接后重新连接-UWP

时间:2019-11-25 04:31:38

标签: uwp bluetooth-lowenergy windows-10-universal gatt bluetooth-gatt

客户端(中部):PC

服务器(外围):移动


连接-断开连接-重新连接-断开连接-重新连接-..

当我在客户端断开连接时,

就像魅力一样工作。 (即我在UWP应用上按断开连接btn)

但是,如果我在服务器端断开连接(即在移动应用程序上按断开btn)

PC无法找到自定义服务。 (即我只能找到0x1801和0x1800)

  • 有时候我什至找不到任何服务

有时候,如果我在PC端断开连接,它可以再次正常工作。 (即...-通过移动应用程序断开连接-连接[SERVICE SEARCH WEIRD]-通过PC应用程序断开连接-重新连接[WORKS WELL]-...)

但是在大​​多数情况下,即使我关闭了移动应用程序/ PC应用程序,我也永远找不到自定义服务。我需要再次配对和配对

  • 在与手机配对之前,我先将其与手机配对,因此可以重新使用bluetoothLeDevice.Id进行重新连接)
  

Galaxy S9 +和LG X Power :如果我在PC端断开连接,则总是可以再次正常工作

     

LG V40,Galaxy Note5 :大多数时候(几乎总是)我需要取消配对->配对)

     

*这是另一个问题,但以防万一,如果有人也需要此信息: V40和Note5在真正连接之前先保持CONNECT状态,然后断开DISPLAY状态几次


下面的代码显示了我如何断开连接

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

有人知道我在外围设备上断开连接时为什么(重新)连接工作很奇怪?

任何帮助,我将不胜感激!

0 个答案:

没有答案