DeviceWatcher与BluetoothLEAdvertisementWatcher的区别及超时属性疑问
DeviceWatcher and BluetoothLEAdvertisementWatcher for BLE Apps Hey there! Let me break down the key differences between these two classes, since you're building a Windows 10 BLE app focused on listening to specific broadcasts and managing connections.
Core Purpose & Focus
DeviceWatcher
This is part of Windows' general device enumeration system. Its main job is to discover and track BLE devices that are recognized by the Windows system (either paired or discoverable). It returns fullDeviceInformationobjects, which makes it easy to initiate a GATT connection once you've found your target device. This is why the official "BluetoothLE" sample uses it—it's built for device discovery leading to connection.BluetoothLEAdvertisementWatcher
This class is laser-focused on BLE advertisement packets themselves, not the device as a system-recognized entity. It listens directly to the BLE advertising frames transmitted over the air, even if the device hasn't been paired or isn't yet in Windows' device list. It lets you parse all the data in the ad (like custom manufacturer data or service UUIDs) and filter ads to only those you care about.
Timeout & Disconnection Capabilities
DeviceWatcher
It doesn't have built-in timeout properties likeOutOfRangeTimeout. Its event triggers (likeRemoved) depend on when the Windows Bluetooth stack detects the device is no longer available—this can be inconsistent and isn't configurable with a specific duration. If you want to add a timeout-based disconnection usingDeviceWatcher, you'd need to implement custom timer logic (e.g., start a timer after connecting, then disconnect if no device activity is detected within your target window).BluetoothLEAdvertisementWatcher
As you noticed, this class hasOutOfRangeTimeoutandInRangeTimeout—these are exactly what you need for your use case. You can set a specific duration; if the watcher stops receiving advertisements from your target device within that time, it will trigger a status change event. You can hook into this event to programmatically disconnect your active BLE connection, perfectly handling your "disconnect after a specific period" requirement.
Which Fits Your Scenario?
If your primary goal is just to find and connect to a BLE device, DeviceWatcher works fine—but you'll need custom code for timeout disconnection. However, since you want to rely on broadcast presence to trigger disconnection, BluetoothLEAdvertisementWatcher is the better fit. You can even combine both: use the advertisement watcher to filter and identify your target device quickly, then use DeviceWatcher (or directly BluetoothLEDevice.FromIdAsync()) to initiate the connection, while keeping the advertisement watcher running to monitor for out-of-range timeouts.
内容的提问来源于stack exchange,提问作者Ashii

