如何使用DeviceInformation.FindAllAsync枚举WindowsPhone可见的所有BLE设备?

How to use DeviceInformation.FindAllAsync to Enumerate all BLE devices visible to Windows Phone?

  1. 我有一个代码可以列出所有 BluetoothLE 设备 Windows Phone 8.1 可以检测到(已配对和未配对):

        var devices = await DeviceInformation.FindAllAsync( BluetoothLEDevice.GetDeviceSelector() );
        foreach ( DeviceInformation di in devices )
            {
            BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync( di.Id );
    
            Debug.WriteLine( bleDevice.Name );
            }
    

但是,行:

var devices = await DeviceInformation.FindAllAsync( BluetoothLEDevice.GetDeviceSelector() );

抛出异常。

知道为什么吗?

  1. 我也试过这个代码:

    var devices = await DeviceInformation.FindAllAsync( GattDeviceService.GetDeviceSelectorFromUuid( GattServiceUuids.GenericAttribute ) );

这段代码没有抛出异常,但是 returns 0 台设备。

知道为什么吗?

在这两种情况下,DeviceCapability 定义如下:

    <m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
        <m2:Device Id="any">
            <m2:Function Type="name:genericAttribute"/>
        </m2:Device>
    </m2:DeviceCapability>

如果您使用的是 Windows Phone 8.1,您将无法枚举未配对的设备。这随着 Windows 10 SDK 版本 1511

而改变