/// <summary> /// The device was closed. If we will autoreconnect to the device, reflect that in the UI /// </summary> /// <param name="sender"></param> /// <param name="deviceInformation"></param> private async void OnDeviceClosing(EventHandlerForDevice sender, DeviceInformation deviceInformation) { await this.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { _r1Timer?.Stop(); _r2Timer?.Stop(); //// We were connected to the device that was unplugged, so change the "Disconnect from device" button //// to "Do not reconnect to device" //if (ButtonDisconnectFromDevice.IsEnabled && EventHandlerForDevice.Current.IsEnabledAutoReconnect) //{ // ButtonDisconnectFromDevice.Content = ButtonNameDisableReconnectToDevice; //} })); }
/// <summary> /// Notify the UI whether or not we are connected to a device /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private async void OnDeviceEnumerationComplete(DeviceWatcher sender, Object args) { await this.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(async() => { _areAllDevicesEnumerated = true; // If we finished enumerating devices and the device has not been connected yet, the OnDeviceConnected method // is responsible for selecting the device in the device list (UI); otherwise, this method does that. if (EventHandlerForDevice.Current.IsDeviceConnected) { //SelectDeviceInList(EventHandlerForDevice.Current.DeviceInformation.Id); //ButtonDisconnectFromDevice.Content = ButtonNameDisconnectFromDevice; //rootPage.NotifyUser("Currently connected to: " + EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage); } else if (EventHandlerForDevice.Current.IsEnabledAutoReconnect && EventHandlerForDevice.Current.DeviceInformation != null) { //// We will be reconnecting to a device //ButtonDisconnectFromDevice.Content = ButtonNameDisableReconnectToDevice; //rootPage.NotifyUser("Waiting to reconnect to device: " + EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage); } else { var entry = _listOfDevices[0]; // Create an EventHandlerForDevice to watch for the device we are connecting to EventHandlerForDevice.CreateNewEventHandlerForDevice(); // Get notified when the device was successfully connected to or about to be closed EventHandlerForDevice.Current.OnDeviceConnected = this.OnDeviceConnected; EventHandlerForDevice.Current.OnDeviceClose = this.OnDeviceClosing; // 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. Since this method is invoked by the UI, we are already in the UI thread. var openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); } })); }
/// <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="deviceInformation"></param> private void OnDeviceConnected(EventHandlerForDevice sender, DeviceInformation deviceInformation) { // Find and select our connected device if (_areAllDevicesEnumerated) { if (_r1Timer == null) { _r1Timer = new DispatcherTimer(); _r1Timer.Interval = Report1Interval; _r1Timer.Tick += R1TimerOnTick; } if (_r2Timer == null) { _r2Timer = new DispatcherTimer(); _r2Timer.Interval = Report2Interval; _r2Timer.Tick += R2TimerOnTick; } _r1Timer.Start(); _r2Timer.Start(); } }
/// <summary> /// Creates a new instance of EventHandlerForDevice, enables auto reconnect, and uses it as the Current instance. /// </summary> public static void CreateNewEventHandlerForDevice() { eventHandlerForDevice = new EventHandlerForDevice(); }