/// <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 async void OnControllerConnected(EventHandlerForDevice sender, DeviceInformation deviceInformation)
        {
            // Find and select our connected device
            if (isAllDevicesEnumerated)
            {
                SelectDeviceInList(EventHandlerForDevice.controllerEventHandler.DeviceInformation.Id);

                ButtonDisconnectFromDevice.Content = ButtonNameDisconnectFromDevice;
            }

            rootPage.NotifyUser("Connected to - " +
                                EventHandlerForDevice.controllerEventHandler.DeviceInformation.Id, NotifyType.StatusMessage);


            // LOOK HERE FOR R/W Examples
            DataWriter DataWriterObject = new DataWriter(sender.Device.OutputStream);

            DataWriterObject.WriteString("a");
            uint bytesWritten = await DataWriterObject.StoreAsync();

            DataReader DataReaderObject = new DataReader(sender.Device.InputStream);
            uint       ReadBufferLength = 1;

            DataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
            UInt32 bytesRead = await DataReaderObject.LoadAsync(ReadBufferLength);

            if (bytesRead > 0)
            {
                String deviceType = DataReaderObject.ReadString(bytesRead);
                rootPage.NotifyUser("Device sent: " + deviceType, NotifyType.StatusMessage);
            }
        }
        private async void ConnectToController_Click(Object sender, RoutedEventArgs eventArgs)
        {
            var             selection = ConnectDevices.SelectedItems;
            DeviceListEntry entry     = null;

            if (selection.Count > 0)
            {
                var obj = selection[0];
                entry = (DeviceListEntry)obj;

                if (entry != null)
                {
                    // Create an EventHandlerForDevice to watch for the device we are connecting to
                    EventHandlerForDevice eventHandler = new EventHandlerForDevice(DeviceType.Controller);

                    // Get notified when the device was successfully connected to or about to be closed
                    eventHandler.OnDeviceConnected = this.OnControllerConnected;
                    eventHandler.OnDeviceClose     = this.OnControllerClosing;

                    // 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 eventHandler.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector);

                    // Disable connect button if we connected to the device
                    UpdateConnectDisconnectButtonsAndList(!openSuccess);
                }
            }
        }
        public EventHandlerForDevice(DeviceType type)
        {
            watcherStarted         = false;
            watcherSuspended       = false;
            isEnabledAutoReconnect = true;

            switch (type)
            {
            case DeviceType.Controller: controllerEventHandler = this; break;

            case DeviceType.Motor: motorEventHandler = this; break;
            }
        }
 /// <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 OnControllerClosing(EventHandlerForDevice sender, DeviceInformation deviceInformation)
 {
     await rootPage.Dispatcher.RunAsync(
         CoreDispatcherPriority.Normal,
         new DispatchedHandler(() =>
     {
         // 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.controllerEventHandler.IsEnabledAutoReconnect)
         {
             ButtonDisconnectFromDevice.Content = ButtonNameDisableReconnectToDevice;
         }
     }));
 }