使用DevicePicker枚举Bluetooth LE设备异常问题求助
Hey there, let's fix those frustrating DevicePicker problems you're facing with your BLE enumeration app. I've run into similar quirks before, so here's how to tackle both issues:
1. Empty DevicePicker Until Another App Scans
The root cause here is that DevicePicker relies on the system's cached device list and doesn't automatically trigger a fresh BLE scan on its own. When you use another app like Bluetooth LE Explorer, it initiates a scan that populates the system cache—hence why your picker works afterward.
Fix:
First, make sure your app has the required Bluetooth permissions. Add this to your Package.appxmanifest:
<Capabilities> <DeviceCapability Name="bluetooth" /> </Capabilities>
Then, replace your current device selectors with a more comprehensive one that targets all BLE devices (not just paired/unpaired states):
picker = new DevicePicker(); picker.DeviceSelected += ConnectToDevice; // Clear existing selectors and use a universal BLE selector picker.Filter.SupportedDeviceSelectors.Clear(); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelector());
Finally, trigger a quick temporary scan before showing the picker to populate the system cache:
// Start a short-lived DeviceWatcher to refresh the system's BLE device cache var tempWatcher = DeviceInformation.CreateWatcher(BluetoothLEDevice.GetDeviceSelector()); tempWatcher.Start(); // Give it 500ms to scan (adjust based on your needs) await Task.Delay(500); tempWatcher.Stop(); // Now show the picker—it should have devices! picker.Show(new Rect(0, 0, 200, 500), Windows.UI.Popups.Placement.Below);
2. Disconnected BLE Devices Still Appear in the List
DevicePicker will show cached devices until the system updates its list. The temporary scan above helps, but you should also validate the device's availability when the user selects it:
Fix:
Update your ConnectToDevice handler to check if the device is still reachable before attempting to connect:
private async void ConnectToDevice(DevicePicker sender, DevicePickerSelectedDeviceEventArgs args) { // Try to retrieve the BLE device object var bleDevice = await BluetoothLEDevice.FromIdAsync(args.SelectedDevice.Id); if (bleDevice == null) { // Device is no longer available—alert the user var dialog = new Windows.UI.Popups.MessageDialog("This device is no longer reachable. Please select another."); await dialog.ShowAsync(); return; } // Proceed with your existing connection logic here }
Bonus Tip
If you want more control over which devices appear, you can use BluetoothLEDevice.GetDeviceSelectorForBluetoothLEDevice() to filter by appearance categories, service UUIDs, or names. For example:
// Filter for devices with a specific service UUID var selector = BluetoothLEDevice.GetDeviceSelectorForBluetoothLEDevice( null, new Guid[] { Guid.Parse("0000FFE0-0000-1000-8000-00805F9B34FB") }, BluetoothLEDeviceAppearanceCategories.Uncategorized ); picker.Filter.SupportedDeviceSelectors.Add(selector);
内容的提问来源于stack exchange,提问作者Sha-1

