Reputation: 304
I am trying to create a Bluetooth manager type class. The class will search through all paired and unpaired Bluetooth devices that are whitelisted in my application, and add them to an internal list of devices.
I do this using the following code:
_deviceWatcher = DeviceInformation.CreateWatcher(_selector,
null, DeviceInformationKind.AssociationEndpoint);
as well as by subscribing to the following events:
_deviceWatcher.Added += _deviceWatcher_Added;
_deviceWatcher.Removed += _deviceWatcher_Removed;
_deviceWatcher.EnumerationCompleted += _deviceWatcher_EnumerationCompleted;
If a Bluetooth device I have whitelisted is on, the device watcher finds the device. But if I turn the device off, I never get a "removed" event.
If the device is off and I wait to receive the "enumeration complete" event, and then turn on the device, I never get the "added" event.
I tried the Microsoft-supplied sample ("DeviceEnumerationAndPairing") but that sample acts the same.
If I keep my program running and open up the Bluetooth settings, as shown below:
then it works as expected and I get the "update" and "removed" events when turning off and powering off the device.
Do I need to activate some sort of scanning routine to be able to detect Bluetooth devices that are available, and if so, how would I do that?
Upvotes: 3
Views: 1308
Reputation: 3286
An app must subscribe to all of the added, removed, and updated events to be notified when there are device additions, removals or updates. If an app handles only the added event, it will not receive an update if a device is added to the system after the initial device enumeration completes.
For more info, see Remark of DeviceInformation.CreateWatcher.
According to the document, we should be able to add the added, removed and updated events of the DeviceWatcher
. When I close the device, the removed event will be fired.
Please note, when the device has paired that if you close it, the Removed
event will not be fired. Also it will not be removed in the Bluetooth settings.
Upvotes: 1