【问题标题】:Windows UWP Bluetooh multiple devices showing for single deviceWindows UWP Bluetooh 多个设备显示单个设备
【发布时间】:2021-08-06 11:55:21
【问题描述】:

我目前正在开发一个 C#-UWP 应用,该应用需要能够发现网络上的蓝牙设备(非 BLE)以及之前已连接/配对的蓝牙设备。

我相信任何不熟悉此任务的人都会很快找到文档和示例,这些文档和示例几乎没有帮助。我从 Stackoverflow 关于人们实验的问题中学到的东西比从文档和示例中学到的更多,但无论如何。

我的主要问题/问题是这样的:在设置设备观察器以查找蓝牙设备后,我发现我始终获得多个相同设备的添加但具有不同的蓝牙地址(这是之前已配对但不在网络上的设备)。经过大量调查和头脑风暴,我们发现每个设备 id 实际上是设备 MAC 地址和 BT 接收器 MAC 地址的配对。

我每 1 个物理设备增加 3 个设备的原因是因为我过去曾使用 3 个不同的 BT 接收器加密狗连接到同一设备。所以我的问题是,有没有办法让设备观察者返回与当前活动的BT接收器加密狗对应的设备?

否则我将需要找到当前活动的 BT 接收器 MAC 地址并过滤掉没有此地址的设备,否则用户将看到 3 个相同的设备可供选择,其中只有 1 个会通过,而另外 2 个会通过失败。

关于这个主题,我还想提一下,设备属性似乎不起作用。在创建观察者之前,我有一个这样的属性列表,例如:

requestedProperties.Add("System.Devices.Aep.DeviceAddress");
requestedProperties.Add("System.Devices.Aep.IsConnected");
requestedProperties.Add("System.Devices.Aep.Bluetooth.Le.IsConnectable");
requestedProperties.Add("System.Devices.Aep.IsPresent");
requestedProperties.Add("System.Devices.Aep.ContainerId");
requestedProperties.Add("System.Devices.Aep.ModelId");
requestedProperties.Add("System.Devices.Aep.Manufacturer");
requestedProperties.Add("System.Devices.Aep.ProtocolId");
requestedProperties.Add("System.Devices.Aep.SignalStrength");

但是当我调试添加的设备时,它没有任何属性: debug info showing no properties for added device

如果有这些信息,我会很有用。

感谢您的任何意见或建议。

【问题讨论】:

  • 以下内容可以帮助您更好地理解问题:docs.microsoft.com/en-us/previous-versions//…
  • 使用蓝牙 API(发现)而不是 DeviceWatcher。
  • @MikePetrichenko 您指的是哪些蓝牙 API?你能发个链接吗?我看过的所有地方,每个人都建议使用设备观察器,没有看到其他替代品。
  • 我们使用这个docs.microsoft.com/en-us/windows/win32/bluetooth/… 来处理经典蓝牙设备。
  • 感谢您的建议,但据我了解,这是一个 C++ API,我们正在制作一个 C# UWP 应用程序。我认为在 UWP C# 中使用 C++ 代码是可能的,但我们想要的只是能够看到潜在的设备,而且我们的期限很紧。设备观察者类型适用于简单的快乐路径场景,但肯定看起来不可信。也许如果我们将来还有一些时间,我们可能会回来使用这个 API。

标签: c# windows uwp bluetooth


【解决方案1】:

更新

我找到了解决这个问题的快速解决方案(虽然我确实发现了设备观察器的更多问题,但这可能是另一个问题的主题)。

现在我只需获取当前的 BT 适配器 MAC 地址,然后检查每个传入设备是否在其配对中具有此 MAC 地址。如果不是,则意味着设备与旧的/未使用的 BT 适配器配对。

/*  NOTE:
Windows allows only 1 BT adapter to be active on 1 machine at a time
Therefore this function will either return the active dongle or a null if 
there is no BT adapter on the machine or its disabled.
*/
BluetoothAdapter BTAdaptor = await BluetoothAdapter.GetDefaultAsync();

if (BTAdaptor == null)
{
   // Log error
   // return
}

//
// Code block to check if the BT adaptor can support the BT tech stack you are interested in.
//

// Format into hex with 12 characters (12 being the number of characters for MAC address)
string tempMac = BTAdaptor.BluetoothAddress.ToString("X12").ToLower();

// Pattern for MAC address.
string pattern = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
string replace = "$1:$2:$3:$4:$5:$6";

m_strBTAdapterMAC = Regex.Replace(tempMac, pattern, replace);

然后当设备被watcher事件添加/更新/删除时,检查一下:

// If device is not paired with the currently used BT adapter. 
if (deviceInfo.Id.Contains(m_strBTAdapterMAC) == false)
{
     // Device paired with old dongle, dont want to show the user.
     return;
}

如果有人想出如何让设备观察者不提供旧设备,请告诉我,这可能是一个更好的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2019-10-13
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多