gpt4 book ai didi

c# - Windows UWP 蓝牙应用程序,设备在扫描时显示,即使它们已关闭

转载 作者:可可西里 更新时间:2023-11-01 14:14:20 34 4
gpt4 key购买 nike

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

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

据我了解属性(property)System.Devices.Aep.IsPresent可用于过滤掉当时不可用的缓存设备,但即使我知道该设备不可访问,我也总是得到该属性的“True”。

关于如何解决这个问题有什么想法吗?

设置

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永远为真

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

最佳答案

查看 Microsoft Bluetooth LE Explorer source code of GattSampleContext .您需要获取属性:System.Devices.Aep.IsConnected、System.Devices.Aep.Bluetooth.Le.IsConnectable 并仅过滤可连接的设备。请注意,在调用 DeviceWatcher.Updated 事件后,设备可能会变得可连接。因此,您必须跟踪 unusedDevices

例如我的 IsConnactable 过滤方法是:

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

关于c# - Windows UWP 蓝牙应用程序,设备在扫描时显示,即使它们已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45568345/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com