【问题标题】:How can I reconnect to BLE device? - UWP如何重新连接到 BLE 设备? - UWP
【发布时间】:2019-10-22 07:45:39
【问题描述】:

设备 ID 和地址都是随机的,我无法将它们重复用于 bleDevice = await BluetoothLEDevice.FromIdAsyncbleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync

我想出了 2 个可能的解决方案:(1)ConnectionStatusChange & (2)Pairing,

但是这两种情况我都有问题。


(1) 主要问题

在断开连接之前,我先将bleDevice 保存为bleDeviceReconnect

然后我取消订阅,dispose features & service & bluetooth le device,并将它们设置为null。

在重新连接的情况下,我尝试使用而不是bleDevice = await BluetoothLEDevice.FromIdAsync(bleDeviceReconnect.DeviceId)

var newSession = GattSession.FromIdAsyn(bleDeviceReconnect.BluetoothDeviceId);
newSession.MaintainConnection = true;
bleDeviceReconnect.ConnectionStatusChanged += ConnectionStatusChangedHandler;

但在这种情况下,事件处理程序永远不会被调用。

处理程序如下所示:

private async void ConnectionStatusChangedHandler(BluetoothLEDevice bluetoothLeDevice, object o)
{
   Debug.WriteLine(bluetoothLeDevice.ConnectionStatus.ToString()) //To check if event comes or not
   if(bluetoothLeDevice.ConnectionStatus.ToString() == "Conncected")
   {
      sList = bluetoothLeDevice.GetGattServicesForUuidAsync(custom service GUID)
      .
      .
      .
   }
}

(2) 如果你也能回答这个问题那就太好了,但如果你不知道或不想回答,则不必这样做

配对是否会使设备 id 或地址不变?

我首先需要使用我上面所说的任何一种方法来获取蓝牙 LE 对象,我可以使用它来查找自定义服务 (sList = bleDevice.GetGattServicesForUuidAsync) 和特性 (cList = sList.Services[0].GetCharacteristicsForUuidAsync),并订阅/写到characteristi等等。

因此,如果id或地址发生变化,配对就没有意义了。


如何正确重新连接到 BLE 设备???

非常感谢任何帮助。


编辑

Richard Zhang - MSFT 要求提供一个最小的可运行演示,我添加了这个:

Microsoft BluetoothLE Code

(1) Scenario2_Client.xaml

添加 2 个按钮:[断开连接] 和 [重新连接]

*断开连接不会立即发生。请等待至少 5 秒,然后再按 [重新连接]

(2) 场景2_Client.xaml.cs

声明private BluetoothLEDevice bluetoothLeDeviceReconnect

private void Disconnect_btn_Click(object sender, RoutedEventArgs e)
{
   bluetoothLeDeviceReconnect = bluetoothLeDevice;

   RemoveValueChangedHandler();
   selectedCharacteristic?.Service?.Dispose();
   selectedCharacteristic = null;
   service.Dispose();
   service = null;
   bluetoothLeDevice?.Dispose();
   bluetoothLeDevice = null;
   GC.Collect();
}

private void Reconnect_btn_Click(object sender, RoutedEventArgs e)
{
   var newSession = GattSession.FromIdAsyn(bluetoothLeDeviceReconnect.BluetoothDeviceId);
   newSession.MaintainConnection = true;
   bluetoothLeDeviceReconnect.ConnectionStatusChanged += ConnectionStatusChangedHandler;
}

private async void ConnectionStatusChangedHandler(BluetoothLEDevice bluetoothLeDevice, object o)
{
   SEE ABOVE
}

目前我没时间测试了。

让我知道这是否像我说的那样有效!我将创建一个 GitHub 帐户,上传我的项目,然后将链接复制并粘贴到此处。

【问题讨论】:

  • 您好,希望您能提供一个最小的可运行演示,以便我们帮助您定位问题。或者你可以参考Microsoft's Bluetooth connection Sample,我测试了一下,可以正常从Id获取设备,至少说明配对后设备的uuid没有变化。
  • 您好,感谢您的评论。我已经阅读了该示例,我遵循完全相同的设备搜索流程。我的代码就像在 Scenario2 中添加两个按钮,[Disconnect]&[Reconnect]。等一下,我会编辑我的问题。
  • 嗨,Scenario2_Client.xaml.cs 包含 ClearBluetoothLEDeviceAsync 方法。在Disconnect中,可以直接调用该方法,但是由于bluetoothLeDevice是Disposed,所以在建立GATT session之前需要重新使用await BluetoothLEDevice.FromIdAsync(selectedBleDeviceId);建立设备连接。
  • 嗨,你的意思是我需要在bleDevice = await.... 行之后添加var newSession .... Handler 行,而不是替换该行,对吗?我试试看!
  • 不幸的是,我试过了,但这并不奏效。处理程序永远不会像以前那样被调用。

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


【解决方案1】:

NVM 有效!

移动应用是配对过程中的问题。

另外,我必须配对并添加处理程序才能使其工作。

我可以通过处理程序获取 BluetoothLEDevice 对象, 但搜索服务现在是个问题。

由于这与我在这篇文章中提出的问题不同,我将发布一个新问题。

【讨论】:

  • 发生了新错误,因为我忘记删除处理程序了!现在完美运行!
猜你喜欢
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-01
  • 2020-09-27
相关资源
最近更新 更多