/// <summary> /// Closes the device, stops the device watcher, stops listening for app events, and resets object state to before a device /// was ever connected. /// /// When the UsbDevice is closing, it will cancel all IO operations that are still pending (not complete). /// The close will not wait for any IO completion callbacks to be called, so the close call may complete before any of /// the IO completion callbacks are called. /// The pending IO operations will still call their respective completion callbacks with either a task /// canceled error or the operation completed. /// </summary> private void CloseCurrentlyConnectedDevice() { if (device != null) { // Notify callback that we're about to close the device deviceCloseCallback?.Invoke(this, deviceInformation); Debug.WriteLine($"Closing device {deviceInformation.Id}"); // This closes the handle to the device device.Dispose(); device = null; } }
/// <summary> /// This method opens the device using the WinRT Usb API. After the device is opened, save the device /// so that it can be used across scenarios. /// /// It is important that the FromIdAsync call is made on the UI thread because the consent prompt can only be displayed /// on the UI thread. /// /// This method is used to reopen the device after the device reconnects to the computer and when the app resumes. /// </summary> /// <param name="deviceInfo">Device information of the device to be opened</param> /// <param name="deviceSelector">The AQS used to find this device</param> /// <returns>True if the device was successfully opened, false if the device could not be opened for well known reasons. /// An exception may be thrown if the device could not be opened for extraordinary reasons.</returns> public async Task <bool> OpenDeviceAsync(DeviceInformation deviceInfo, String deviceSelector) { device = await Windows.Devices.Usb.UsbDevice.FromIdAsync(deviceInfo.Id); bool successfullyOpenedDevice = false; try { // Device could have been blocked by user or the device has already been opened by another app. if (device != null) { successfullyOpenedDevice = true; deviceInformation = deviceInfo; this.deviceSelector = deviceSelector; Debug.WriteLine($"Device {deviceInformation.Id} opened"); // Notify registered callback handle that the device has been opened deviceConnectedCallback?.Invoke(this, deviceInformation); // Background tasks are not part of the app, so app events will not have an affect on the device if (!isBackgroundTask && (appSuspendEventHandler == null || appResumeEventHandler == null)) { RegisterForAppEvents(); } // User can block the device after it has been opened in the Settings charm. We can detect this by registering for the // DeviceAccessInformation.AccessChanged event if (deviceAccessEventHandler == null) { RegisterForDeviceAccessStatusChange(); } // Create and register device watcher events for the device to be opened unless we're reopening the device if (deviceWatcher == null) { deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector); RegisterForDeviceWatcherEvents(); } if (!watcherStarted) { // Start the device watcher after we made sure that the device is opened. StartDeviceWatcher(); } } else { successfullyOpenedDevice = false; //notificationStatus = NotifyType.ErrorMessage; var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInfo.Id).CurrentStatus; switch (deviceAccessStatus) { case DeviceAccessStatus.DeniedByUser: Debug.WriteLine($"Access to the device was blocked by the user : {deviceInfo.Id}"); break; case DeviceAccessStatus.DeniedBySystem: // This status is most likely caused by app permissions (did not declare the device in the app's package.appxmanifest) // This status does not cover the case where the device is already opened by another app. Debug.WriteLine($"Access to the device was blocked by the system : {deviceInfo.Id}"); break; default: // Most likely the device is opened by another app, but cannot be sure Debug.WriteLine($"Unknown error, possibly opened by another app : {deviceInfo.Id}"); break; } } } // catch all because the device open might fail for a number of reasons catch { } return(successfullyOpenedDevice); }