问题
I created a device watcher:
DeviceInformation.CreateWatcher(BluetoothLEDevice.GetDeviceSelectorFromDeviceName("..."));
If the bluetooth device I'm looking for is powered off, an Added event is raised immediately followed by a Removed event. If it's powered on, I only get an Added event.
Is the device somehow being cached if it was previously found by the watcher, and that's what causes the Added event to be raised despite being powered off? Is there a way to prevent this?
回答1:
Is the device somehow being cached if it was previously found by the watcher, and that's what causes the Added event to be raised despite being powered off? Is there a way to prevent this?
The device would not be cached by the watcher, the enumeration of DeviceWatcher
comes from system level, and it will take a little time to check the Bluetooth device status change. It is not instant response.
If you want test DeviceWatcher
please refer to the following steps.
- Open system Settings --> Devices-->Add Bluetooth or other device-->Bluetooth.
Start your
DeviceWatcher
. You will find that the device info display in the list view quickly. Because the data source of list view comes from system level collection.Stop your
DeviceWatcher
and then stop your bluetooth device until you could not find your bluetooth device info in "Add a device" window.Start your
DeviceWatcher
. You will find the your bluetooth device info would not display again and the application would not raise event as your mentioned
Note
For third step, if you start DeviceWatcher
before the information of bluetooth device disappearing in the "Add a Device" list, then the events execution order will be just like what you mentioned.
来源:https://stackoverflow.com/questions/43956362/uwp-devicewatcher-raises-added-event-for-powered-off-device