/// <summary> /// Creates a DeviceListEntry for a device and adds it to the list of devices in the UI /// </summary> /// <param name="deviceInformation">DeviceInformation on the device to be added to the list</param> /// <param name="deviceSelector">The AQS used to find this device</param> private void AddDeviceToList(DeviceInformation deviceInformation, String deviceSelector) { // search the device list for a device with a matching interface ID var match = FindDevice(deviceInformation.Id); // Add the device if it's new if (match == null) { // Create a new element for this device interface, and queue up the query of its // device information match = new DeviceListEntry(deviceInformation, deviceSelector); // Add the new element to the end of the list of devices listOfDevices.Add(match); } }
private void ButtonDisconnectFromDevice_Click(object sender, RoutedEventArgs e) { var selection = COMcombobox.SelectedItem; DeviceListEntry entry = null; // Prevent auto reconnect because we are voluntarily closing it // Re-enable the ConnectDevice list and ConnectToDevice button if the connected/opened device was removed. EventHandlerForDevice.Current.IsEnabledAutoReconnect = false; if (selection != null) { var obj = selection; entry = (DeviceListEntry)obj; if (entry != null) { EventHandlerForDevice.Current.CloseDevice(); comText.IsReadOnly = true; } } UpdateConnectDisconnectButtonsAndList(true); }
private async void ButtonConnectToDevice_Click(object sender, RoutedEventArgs e) { var selection = COMcombobox.SelectedItem; DeviceListEntry entry = null; if (selection != null) { var obj = selection; entry = (DeviceListEntry)obj; if (entry != null) { // 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, when present, // can only be displayed on the UI thread. Since this method is invoked by the UI, we are already in the UI thread. Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); // Disable connect button if we connected to the device UpdateConnectDisconnectButtonsAndList(!openSuccess); if (openSuccess) { comText.IsReadOnly = false; ResetReadCancellationTokenSource(); ResetWriteCancellationTokenSource(); IsReadTaskPending = true; DataReaderObject = new DataReader(EventHandlerForDevice.Current.Device.InputStream); await ReadAsync(ReadCancellationTokenSource.Token); } } } }