搜索蓝牙UWP范围内的设备

时间:2016-12-03 17:25:19

标签: c# windows mobile bluetooth uwp

是否可以扫描蓝牙范围内的设备?我尝试了FindAll方法,但它返回所有配对设备。还试过了与deviceWatcher相同的结果。

2 个答案:

答案 0 :(得分:0)

我在以下代码中提供了BluetoothLE Device Selector字符串。它对我有用。

 DeviceWatcher dWatcher = null;
 var BluetoothDeviceSelector = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" AND ((System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False) OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#False)";

 dWatcher = DeviceInformation.CreateWatcher(BluetoothDeviceSelector);

 dWatcher.Added += DeviceAdded;
 dWatcher.Updated += DeviceUpdated;

 dWatcher.Start();

答案 1 :(得分:0)

GetDeviceSelector和朋友返回包含AQS查询的字符串。你看过他们吗?

已配对

System.Devices.DevObjectType:=5 
AND System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}" 
AND (
  System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True 
  OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#False
)

未配对

System.Devices.DevObjectType:=5 
AND System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}" 
AND (
  System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False 
  OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#True
)

您认为括号中的表达式检验了什么?

如果您从提供给CreateWatcher的字符串中省略此子句,您会怎么想?

有两个蓝牙协议标识符,这可能对您有所帮助,这似乎不太明显。上面显示的是经典蓝牙,查询与BLE(低功耗蓝牙)设备不匹配。

我建议您使用的选择器是

System.Devices.DevObjectType:=5 
AND (
  System.Devices.Aep.ProtocolId:="{BB7BB05E-5972-42B5-94FC-76EAA7084D49}" 
  OR System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}#"
)

我在所有这些查询中都添加了换行符,以帮助您阅读它们。不要在代码中包含它们。为了方便起见,这是我的一个项目中的一些代码。

string BleSelector = "System.Devices.DevObjectType:=5 AND (System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" OR System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\")";
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.ProtocolId" };
deviceWatcher = DeviceInformation.CreateWatcher(BleSelector, requestedProperties, DeviceInformationKind.AssociationEndpoint);

使用观察者具有列表逐渐可用的优点。 FindAllAsync可能是异步的,但要经过一段时间才能返回。

相关问题