【发布时间】:2019-01-15 10:40:12
【问题描述】:
我正在尝试(像许多其他人一样)制作一个应用程序,该应用程序最初应该能够检测所有可见的蓝牙 (RFComm) 设备(未配对和已配对、已连接和未连接)并在屏幕上列出它们。 之后,它还可以与选定的设备配对。
我目前正试图了解蓝牙在 Windows 10 中的工作原理。我发现蓝牙设备被视为连接到 PC 的设备,如键盘、鼠标、USB 集线器等,如果我想查看蓝牙设备,最方便的方法之一是使用 Deviceinformation.FindAllAsync() 方法。
我使用了一些示例代码来查找设备,首先尝试的是:
selector = BluetoothDevice.GetDeviceSelector();
var devices = await DeviceInformation.FindAllAsync(selector);
foreach (var device in devices)
{
var bluetoothDevice = await BluetoothDevice.FromIdAsync(device.Id);
if (bluetoothDevice != null)
{
Debug.WriteLine(bluetoothDevice.BluetoothAddress);
}
Debug.WriteLine(device.Id);
foreach (var property in device.Properties)
{
Debug.WriteLine(" " + property.Key + " " + property.Value);
}
}
此方法找不到我设备附近的任何蓝牙设备。
如果我改变第一行:
selector = BluetoothDevice.GetDeviceSelector();
到这样的事情:
selector = BluetoothDevice.GetDeviceSelectorFromPairingState(false);
它最终能够找到所有可见的未配对设备,而 FindAllAsync 需要 30 秒才能找到所有这些设备。
这里出现了一个问题:如何找到所有可见的设备,而不管它们的配对状态如何,以及如何将 30 秒的搜索时间加快到更少?
最后我必须找到 1 个特定的蓝牙 2.0 设备并连接到它。在我使用 FindAllAsync 找到它之后如何做到这一点?
【问题讨论】:
标签: bluetooth uwp windows-10-universal rfcomm