I am trying to use a DeviceWatcher to listen for a usb device. If the device is plugged in when my app starts, the Added event fires just fine and I can connect. But if I plug the device in after my app starts, or if I unplug the device, no other events fire. I have added callbacks to Added, Removed, and Updated, which is what the documentation said needed to have callbacks for the thing to work. What am I missing?
private void Watcher_DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
_resultCollection.Add(deviceInfo);
RaiseDeviceChanged(sender, deviceInfo.Id);
}
}
private void Watcher_DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
// Find the corresponding updated DeviceInformation in the collection and pass the update object
// to the Update method of the existing DeviceInformation. This automatically updates the object
// for us.
foreach (var deviceInfoDisp in _resultCollection) {
if (deviceInfoDisp.Id == deviceInfoUpdate.Id) {
deviceInfoDisp.Update(deviceInfoUpdate);
RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
break;
}
}
}
}
private void Watcher_DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
// Find the corresponding DeviceInformation in the collection and remove it
foreach (var deviceInfoDisp in _resultCollection) {
if (deviceInfoDisp.Id == deviceInfoUpdate.Id) {
_resultCollection.Remove(deviceInfoDisp);
break;
}
}
RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
}
}