2016-12-03 100 views
0

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

+0

https://32feet.codeplex.com/ –

+0

你好,问题是,方法返回范围内或不在范围内的所有配对设备。我想创建一个蓝牙扫描仪来查找范围内的设备。 – Yoki

+0

一旦蓝牙设备与系统配对,设备就被缓存。在此之后,设备将在查询时自动发现。 AFAIK,我们无法在枚举设备时获取当前可用的配对设备。 –

回答

2

是否可以扫描在蓝牙范围内的设备?

这是可能的,我们可以使用DeviceInformation.FindAllAsync方法或DeviceWatcher类。但要获得所有蓝牙设备,我们需要注意的是,当前的蓝牙API不提供选择器来获得所有已配对和未配对的设备BluetoothDevice.GetDeviceSelector方法实际返回的值与BluetoothDevice.GetDeviceSelectorFromPairingState(true)相同。因此,当您使用此选择器(高级查询语法(AQS)字符串)时,您将始终获得所有配对的设备。

要获得所有的设备,我们可以用这个选择会首先配对的设备,然后使用BluetoothDevice.GetDeviceSelectorFromPairingState(false)获得其余未配对设备。

或者我们可以指定AQS字符串类似如下:

//The AQS string for getting all Bluetooth devices 
var BluetoothDeviceSelector = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\""; 

//Using DeviceInformation.FindAllAsync method 
var deviceInfoCollection = await DeviceInformation.FindAllAsync(BluetoothDeviceSelector); 

//Using DeviceWatcher class 
var deviceWatcher = DeviceInformation.CreateWatcher(BluetoothDeviceSelector); 

PS:FindAllAsync方法,多用通过当前连接或与系统配对的设备看。要获得配对和非配对设备,最好使用DeviceWatcher类。

有关如何使用FindAllAsync方法或DeviceWatcher类的更多信息,请参阅Enumerate devices和官方Device enumeration and pairing sample

此外Bluetooth devices,也有Bluetooth LE devices你可能会想。而对于蓝牙LE设备,AQS字符串将是System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:="{BB7BB05E-5972-42B5-94FC-76EAA7084D49}"

+0

周杰伦,我试图做的一样你所描述的,但保持有一个问题 - 为配对设备,我无法理解一个设备是否真的可用 - 在最初扫描System.Devices.Aep.IsPresent被返回true,只有连接尝试失败后将变为false。我该怎么办?甚至System.Devices.Aep.SignalStrength也显示了设备的缓存值在几周内还没有看到。 –

0

我提供了下面的代码BluetoothLE设备选择字符串。这个对我有用。

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