这是用于获取 BLE 设备的 UWP 代码。为什么我在某些设备上得到 bleDevice == null?我没有找到任何解释这一点的文档。
var devices = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector());
foreach (DeviceInformation di in devices)
{
Debug.WriteLine(di.Name);
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id);
if (bleDevice == null) {
Debug.WriteLine("--- NULL ----");
continue;
}
Debug.WriteLine(bleDevice.Name);
}
我注意到,对于在 Windows 设备管理器中带有 STATUS_DEVICE_POWER_FAILURE 的感叹号的 BLE 设备,我得到 bleDevice != null 。
对于设备管理器中没有感叹号的 BLE 设备,我得到 bleDevice == null 。
我遇到了同样的问题。以前的来自 Chi Lee 的回答 https://stackoverflow.com/a/41429019/5011596事实上很好,但关于如何做到这一点还不够详细(对于非专家;-))。
这里是详细过程(假设你在Microsoft Visual Studio下有一个C#项目):
- Double click the properties field under your project: this will open a new tab
- in the opened tab, select Application in the left then click Package Manifest... button
- 名为“的新选项卡包.appxmanifest”已打开。在其中选择能力 tab
- Check Bluetooth field under capabilities
- 保存并重新编译您的项目。
BluetoothLEDevice.FromIdAsync(di.Id) 将不再返回 null,前提是您已经有配对设备(在您的程序中或在 Windows 中手动) .
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)