Martin Dusek
Martin Dusek

Reputation: 1292

BluetoothLEDevice.FromIdAsync returning 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);
    }

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.

Upvotes: 5

Views: 3121

Answers (2)

filotn
filotn

Reputation: 566

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 select properties
  2. in the opened tab, select Application in the left then click Package Manifest... button pkg manifest button
  3. A new tab named "package.appxmanifest" is opened. Select, inside it, the Capabilities tab
  4. Check Bluetooth field under capabilities select BT capability
  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 .

Upvotes: 19

Chi Lee
Chi Lee

Reputation: 21

You have to add Bluetooth capability in your appxmanifest file.

Upvotes: 1

Related Questions