Windows UWP Bluetooh 多个设备显示为单个设备

问题描述 投票:0回答:2

我目前正在开发一个 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

如果我能得到这些信息将会很有用。

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

c# windows uwp bluetooth
2个回答
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);

然后当观察者事件添加/更新/删除设备时,检查它:

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

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


0
投票

对于系统属性问题的第二部分;

我也有同样的问题。即使在程序集上调试和设置“启用本机代码调试”时也是如此。但后来我意识到简单地使用例如;

var addressStr = e.Properties["System.Devices.Aep.DeviceAddress"].ToString()

这将为您带来财产。如果您不向观察者请求该属性(看起来您已经在这样做),它将失败,即

requestedProperties.Add("System.Devices.Aep.DeviceAddress");
© www.soinside.com 2019 - 2024. All rights reserved.