/// <summary> /// Unregister from App events and DeviceWatcher events because this page will be unloaded. /// </summary> /// <param name="eventArgs"></param> //protected override void OnNavigatedFrom(NavigationEventArgs eventArgs) //{ // StopDeviceWatchers(); // StopHandlingAppEvents(); // // We no longer care about the device being connected // EventHandlerForDevice.Current.OnDeviceConnected = null; // EventHandlerForDevice.Current.OnDeviceClose = null; //} public async void ConnectToDevice_Click() { var selection = ConnectDevicesSelectedItems; 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. await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync( CoreDispatcherPriority.Normal, async() => { Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); UpdateConnectDisconnectButtonsAndList(!openSuccess); }); // Disable connect button if we connected to the device } } }
/// <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); } }
public void DisconnectFromDevice_Click() { var selection = ConnectDevicesSelectedItems; 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(); } } UpdateConnectDisconnectButtonsAndList(true); }