2016-12-03 99 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();