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.
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):
- 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
- A new tab named "package.appxmanifest" is opened. Select, inside it, the Capabilities tab
- Check Bluetooth field under capabilities
- 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 .
You have to add Bluetooth capability in your appxmanifest file.
来源:https://stackoverflow.com/questions/39479747/bluetoothledevice-fromidasync-returning-null