Windows UWP 蓝牙应用程序,即使在关闭电源的情况下,设备也会在扫描时显示 [英] Windows UWP bluetooth app, devices showing up when scanning even when they are powered off

查看:8
本文介绍了Windows UWP 蓝牙应用程序,即使在关闭电源的情况下,设备也会在扫描时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用蓝牙连接到不同设备的 UWP 应用.

I am developing a UWP app that is using bluetooth to connect to different devices.

我的问题是,一些已配对或先前发现的设备显示在我的设备列表中,即使它们已关闭或不在范围内.

My problem is that some devices that has been paired or previously discovered is showing up in my device list even though they are turned off or not in range.

据我所知,系统.Devices.Aep.IsPresent 可用于过滤掉当时不可用的缓存设备,但即使我知道该设备无法访问,我也总是为该属性设置为True".

As of my understanding the property System.Devices.Aep.IsPresent can be used to filter out cached devices that are not available at the time but I always get "True" for that property even though I know the device is not reachable.

关于如何解决这个问题的任何想法?

Any ideas on how this can be solved?

string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.IsPresent", "System.Devices.Aep.ContainerId", "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.Manufacturer", "System.Devices.Aep.ModelId", "System.Devices.Aep.ProtocolId", "System.Devices.Aep.SignalStrength"};
        _deviceWatcher = DeviceInformation.CreateWatcher("{REMOVED, NOT IMPORTANT}", requestedProperties, DeviceInformationKind.AssociationEndpoint);
        _deviceWatcher.Added += DeviceAdded;
        _deviceWatcher.Updated += DeviceUpdated;
        _deviceWatcher.Removed += DeviceRemoved;
        _deviceWatcher.EnumerationCompleted += DeviceEnumerationCompleted;

添加设备时的回调

这里 isPresent 总是正确的

Callback for when device is added

Here isPresent is always true

private void DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo)
{
    Device device = new Device(deviceInfo);
    bool isPresent = (bool)deviceInfo.Properties.Single(p => p.Key == "System.Devices.Aep.IsPresent").Value;
    Debug.WriteLine("*** Found device " + deviceInfo.Id + " / " + device.Id + ", " + "name: " + deviceInfo.Name + " ***");
    Debug.WriteLine("RSSI = " + deviceInfo.Properties.Single(d => d.Key == "System.Devices.Aep.SignalStrength").Value);
    Debug.WriteLine("Present: " + isPresent);
    var rssi = deviceInfo.Properties.Single(d => d.Key == "System.Devices.Aep.SignalStrength").Value;
    if (rssi != null)
        device.Rssi = int.Parse(rssi.ToString());
    if (DiscoveredDevices.All(x => x.Id != device.Id) && isPresent)
    {
        DiscoveredDevices.Add(device);
        DeviceDiscovered(this, new DeviceDiscoveredEventArgs(device));
    }
}

推荐答案

查看 GattSampleContext 的 Microsoft 蓝牙 LE Explorer 源代码.您需要获取属性:System.Devices.Aep.IsConnected, System.Devices.Aep.Bluetooth.Le.IsConnectable 并仅过滤可连接的设备.请注意,在调用 DeviceWatcher.Updated 事件后,设备可能会变得可连接.所以你必须跟踪unusedDevices.

Look into the Microsoft Bluetooth LE Explorer source code of GattSampleContext. You need to get properties: System.Devices.Aep.IsConnected, System.Devices.Aep.Bluetooth.Le.IsConnectable and filter only devices, which are connectable. Take care, that the device may become connectable after the DeviceWatcher.Updated event is called. So you have to keep some track of unusedDevices.

例如我的 IsConnactable 过滤器方法是:

E.g. my IsConnactable filter method is:

private static bool IsConnectable(DeviceInformation deviceInformation)
{
    if (string.IsNullOrEmpty(deviceInformation.Name))
        return false;
    // Let's make it connectable by default, we have error handles in case it doesn't work
    bool isConnectable = (bool?)deviceInformation.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"] == true;
    bool isConnected = (bool?)deviceInformation.Properties["System.Devices.Aep.IsConnected"] == true;
    return isConnectable || isConnected;
}

这篇关于Windows UWP 蓝牙应用程序,即使在关闭电源的情况下,设备也会在扫描时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆