本文介绍了BluetoothLEDevice.FromIdAsync返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用于获取BLE设备的UWP代码.为什么某些设备会出现bleDevice == null?我没有找到任何说明该问题的文档.

This is UWP code for getting BLE devices. Why I get bleDevice == null for some devices? I didn't find any documentation which explains that.

    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.

I noticed that I get bleDevice != null for BLE devices that HAVE exclamation mark with STATUS_DEVICE_POWER_FAILURE in device manager in Windows.

对于在设备管理器中没有感叹号的BLE设备,我得到bleDevice == null.

I get bleDevice == null for BLE devices that DO NOT HAVE exclamation mark in device manager.

推荐答案

我遇到了同样的问题.以前的来自李智的答案确实不错,但没有足够详细的说明(对于非专家;- )).

I got the same problem. the previous answer from Chi Lee is in fact good but not enough detailed on how to do it (for non-experts;-) ).

详细过程(假设您在Microsoft Visual Studio下有一个c#项目):

Here the detailed process (assuming you have a c# project under Microsoft visual studio):

  1. 双击项目下的属性字段:这将打开一个新选项卡
  2. 在打开的标签中,选择左侧的 Application ,然后单击 Package Manifest ... 按钮
  3. 将打开一个名为" package.appxmanifest "的新标签.在其中选择 功能 标签
  4. 检查功能"下的蓝牙"字段
  5. 保存并重新编译您的项目.
  1. Double click the properties field under your project: this will open a new tab
  2. in the opened tab, select Application in the left then click Package Manifest... button
  3. A new tab named "package.appxmanifest" is opened. Select, inside it, the Capabilities tab
  4. Check Bluetooth field under capabilities
  5. Save and recompile your project.

BluetoothLEDevice.FromIdAsync(di.Id)将不再返回null,前提是您已经配对了设备(在程序中或在Windows中手动配对.

The BluetoothLEDevice.FromIdAsync(di.Id) will no more return null, provided that you already have paired devices (either in your program or manually in Windows .

这篇关于BluetoothLEDevice.FromIdAsync返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:31