BluetoothLEDevice.FromIdAsync returning null

本小妞迷上赌 提交于 2019-12-08 04:34:26

问题


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

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

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


回答1:


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;-) ).

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

  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.

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 .




回答2:


You have to add Bluetooth capability in your appxmanifest file.



来源:https://stackoverflow.com/questions/39479747/bluetoothledevice-fromidasync-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!