/// <summary> /// If all the devices have been enumerated, select the device in the list we connected to. Otherwise let the EnumerationComplete event /// from the device watcher handle the device selection /// </summary> /// <param name="sender"></param> /// <param name="onDeviceConnectedEventArgs"></param> private void OnDeviceConnected(EventHandlerForDevice sender, OnDeviceConnectedEventArgs onDeviceConnectedEventArgs) { // Find and select our connected device if (isAllDevicesEnumerated) { ButtonDisconnectFromDevice.Content = ButtonNameDisconnectFromDevice; if (onDeviceConnectedEventArgs.IsDeviceSuccessfullyConnected) { SelectDeviceInList(EventHandlerForDevice.Current.DeviceInformation.Id); UpdateConnectDisconnectButtonsAndList(false); rootPage.NotifyUser("Currently connected to: " + EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage); } else { // Prevent auto reconnect from occuring EventHandlerForDevice.Current.IsEnabledAutoReconnect = false; UpdateConnectDisconnectButtonsAndList(true); } } }
/// <summary> /// This method opens the device using the WinRT Hid 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<Boolean> OpenDeviceAsync(DeviceInformation deviceInfo, String deviceSelector) { // This sample uses FileAccessMode.ReadWrite to open the device because we do not want other apps opening our device and // changing the state of our device. FileAccessMode.Read can be used instead. device = await HidDevice.FromIdAsync(deviceInfo.Id, FileAccessMode.ReadWrite); Boolean successfullyOpenedDevice = false; NotifyType notificationStatus; String notificationMessage = null; // 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; notificationStatus = NotifyType.StatusMessage; notificationMessage = "Device " + deviceInformation.Id + " opened"; if (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; if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser) { notificationMessage = "Access to the device was blocked by the user : "******"Access to the device was blocked by the system : " + deviceInfo.Id; } else { // Most likely the device is opened by another app, but cannot be sure notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Id; } } MainPage.Current.NotifyUser(notificationMessage, notificationStatus); // Notify registered callback handle that the device has been opened if (deviceConnectedCallback != null) { var deviceConnectedEventArgs = new OnDeviceConnectedEventArgs(successfullyOpenedDevice, deviceInfo); deviceConnectedCallback(this, deviceConnectedEventArgs); } return successfullyOpenedDevice; }
/// <summary> /// This method opens the device using the WinRT Hid 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 <Boolean> OpenDeviceAsync(DeviceInformation deviceInfo, String deviceSelector) { // This sample uses FileAccessMode.ReadWrite to open the device because we do not want other apps opening our device and // changing the state of our device. FileAccessMode.Read can be used instead. device = await HidDevice.FromIdAsync(deviceInfo.Id, FileAccessMode.ReadWrite); Boolean successfullyOpenedDevice = false; NotifyType notificationStatus; String notificationMessage = null; // 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; notificationStatus = NotifyType.StatusMessage; notificationMessage = "Device " + deviceInformation.Id + " opened"; if (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; if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser) { notificationMessage = "Access to the device was blocked by the user : "******"Access to the device was blocked by the system : " + deviceInfo.Id; } else { // Most likely the device is opened by another app, but cannot be sure notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Id; } } MainPage.Current.NotifyUser(notificationMessage, notificationStatus); // Notify registered callback handle that the device has been opened if (deviceConnectedCallback != null) { var deviceConnectedEventArgs = new OnDeviceConnectedEventArgs(successfullyOpenedDevice, deviceInfo); deviceConnectedCallback(this, deviceConnectedEventArgs); } return(successfullyOpenedDevice); }