public async Task RenewDeviceList(string interfaceType)
        {
            connections.Clear();

            Task <DeviceInformationCollection> task = null;

            switch (interfaceType)
            {
            case "Bluetooth":
                task = BluetoothSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>();
                break;

            case "USB":
                task = UsbSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>();
                break;

            case "DfRobot":
                task = DfRobotBleSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>();
                break;
            }

            if (task != null)
            {
                await task.ContinueWith(listTask =>
                {
                    var result = listTask.Result;

                    foreach (DeviceInformation device in result)
                    {
                        connections.Add(device.Name, device);
                    }
                });
            }
        }
Example #2
0
        /// <summary>
        /// Refreshes the connections from the available device sources
        /// </summary>
        /// <returns>Collection of connection objects available to the app</returns>
        public async Task <Connections> RefreshConnections()
        {
            Connections connections = new Connections();

            connections.Clear();

            await BluetoothSerial.listAvailableDevicesAsync().AsTask().ContinueWith(
                listTask =>
            {
                listTask.Result.ForEach(
                    d => connections.Add(new Connection(d.Name, d, ConnectionType.BluetoothSerial)));
            });

            await UsbSerial.listAvailableDevicesAsync().AsTask().ContinueWith(
                listTask =>
            {
                listTask.Result.ForEach(
                    d => connections.Add(new Connection(d.Name, d, ConnectionType.UsbSerial)));
            });

            string previousConnection = App.CurrentAppSettings.PreviousConnectionName;

            if (this.CurrentConnection == null && !string.IsNullOrEmpty(previousConnection) &&
                connections.Any(c => c.DisplayName == App.CurrentAppSettings.PreviousConnectionName))
            {
                await this.Connect(
                    connections.FirstOrDefault(
                        c => c.DisplayName == App.CurrentAppSettings.PreviousConnectionName));
            }

            return(connections);
        }
Example #3
0
        private async void rad_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rad = (RadioButton)sender;

            lstDevices.ItemsSource = null;
            if (rad == radBluetooth)
            {
                var bleutoothDevices = await BluetoothSerial.listAvailableDevicesAsync();

                lstDevices.ItemsSource = bleutoothDevices;
            }
            else if (rad == radUSB)
            {
                var usbDevices = await UsbSerial.listAvailableDevicesAsync();

                List <DeviceInformation> _usbDevices = new List <DeviceInformation>();
                foreach (var itm in usbDevices)
                {
                    if (itm.Name.Contains("COM"))
                    {
                        _usbDevices.Add(itm);
                    }
                }
                lstDevices.ItemsSource = _usbDevices;
            }
        }
Example #4
0
        private void RefreshDeviceList()
        {
            if (comboBox.SelectedValue != null)
            {
                if (comboBox.SelectedValue.Equals("Bluetooth LE"))
                {
                    DfRobotBleSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>().ContinueWith(listTask =>
                    {
                        var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
                        {
                            _connections = new ObservableCollection <Connection>();
                            foreach (DeviceInformation device in listTask.Result)
                            {
                                _connections.Add(new Connection(device.Name, device));
                            }
                            connectList.ItemsSource = _connections;

                            // autoconnect if only 1 device is paired
                            if (_connections.Count == 1)
                            {
                                connectList.SelectedItem = _connections[0];
                                Reconnect_Click(null, null);
                            }
                        }));
                    });
                }
                else if (comboBox.SelectedValue.Equals("Bluetooth"))
                {
                    BluetoothSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>().ContinueWith(listTask =>
                    {
                        var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
                        {
                            _connections = new ObservableCollection <Connection>();
                            foreach (DeviceInformation device in listTask.Result)
                            {
                                _connections.Add(new Connection(device.Name, device));
                            }
                            connectList.ItemsSource = _connections;

                            // autoconnect if only 1 device is paired
                            if (_connections.Count == 1)
                            {
                                connectList.SelectedItem = _connections[0];
                                Reconnect_Click(null, null);
                            }
                        }));
                    });
                }
            }
        }
Example #5
0
        private async void btnList_Click(object sender,
                                         RoutedEventArgs e)
        {
            var a = await BluetoothSerial.
                    listAvailableDevicesAsync();

            //var b = a.First();
            //var c = a.First(x => x.Name == "HC-05");
            if (a.Count < 1)
            {
                Debug.WriteLine("Bad pobierania listy");
                return;
            }
            var c = a.First(x => x.Name == "sowaphone");
            //var d = b.Name;
        }
Example #6
0
 private void RefreshDeviceList()
 {
     //invoke the listAvailableDevicesAsync method of BluetoothSerial. Since it is Async, we will wrap it in a Task and add a llambda to execute when finished
     BluetoothSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>().ContinueWith(listTask =>
     {
         //store the result and populate the device list on the UI thread
         var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
         {
             _connections = new Connections();
             foreach (DeviceInformation device in listTask.Result)
             {
                 _connections.Add(new Connection(device.Name, device));
             }
             connectList.ItemsSource = _connections;
         }));
     });
 }
        public async Task <ObservableCollection <string> > GetDeviceList(string interfaceType, CancellationToken token)
        {
            ObservableCollection <string> output = new ObservableCollection <string>();

            connections.Clear();

            Task <DeviceInformationCollection> task = null;

            switch (interfaceType)
            {
            case "Bluetooth":
                task = BluetoothSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>(token);
                break;

            case "USB":
                task = UsbSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>(token);
                break;

            case "DfRobot":
                task = DfRobotBleSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>(token);
                break;
            }


            if (task != null)
            {
                await task.ContinueWith(listTask =>
                {
                    var result = listTask.Result;
                    foreach (DeviceInformation device in result)
                    {
                        output.Add(device.Name);
                        connections.Add(device.Name, device);
                    }
                });
            }

            return(output);
        }
Example #8
0
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            Speed           = 0;
            this.dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

            //cancelTokenSource = new CancellationTokenSource();
            //cancelTokenSource.Token
            task = BluetoothSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>();
            if (true)
            {
                task.ContinueWith(listTask =>
                {
                    //store the result and populate the device list on the UI thread
                    var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
                    {
                        Connections connections = new Connections();

                        var result = listTask.Result;
                        if (result == null || result.Count == 0)
                        {
                            DebugInfo.Text            = "No items found.";
                            ConnectionList.Visibility = Visibility.Collapsed;
                        }
                        else
                        {
                            foreach (DeviceInformation device in result)
                            {
                                connections.Add(new Connection(device.Name, device));
                            }
                            DebugInfo.Text = "Select an device and press \"Connect\" to connect.";
                        }

                        ConnectionList.ItemsSource = connections;
                    }));
                });
            }
        }
        private void RefreshDeviceList()
        {
            //invoke the listAvailableDevicesAsync method of the correct Serial class. Since it is Async, we will wrap it in a Task and add a llambda to execute when finished
            Task <DeviceInformationCollection> task = null;

            if (ConnectionMethodComboBox.SelectedItem == null)
            {
                ConnectMessage.Text = "Select a connection method to continue.";
                return;
            }

            switch (ConnectionMethodComboBox.SelectedItem as String)
            {
            default:
            case "Bluetooth":
                ConnectionList.Visibility        = Visibility.Visible;
                DevicesText.Visibility           = Visibility.Visible;
                NetworkHostNameTextBox.IsEnabled = false;
                NetworkPortTextBox.IsEnabled     = false;
                BaudRateComboBox.IsEnabled       = true;
                NetworkHostNameTextBox.Text      = "";
                NetworkPortTextBox.Text          = "";

                //create a cancellation token which can be used to cancel a task
                cancelTokenSource = new CancellationTokenSource();
                cancelTokenSource.Token.Register(() => OnConnectionCancelled());

                task = BluetoothSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>(cancelTokenSource.Token);
                break;

            case "USB":
                ConnectionList.Visibility        = Visibility.Visible;
                DevicesText.Visibility           = Visibility.Visible;
                NetworkHostNameTextBox.IsEnabled = false;
                NetworkPortTextBox.IsEnabled     = false;
                BaudRateComboBox.IsEnabled       = true;
                NetworkHostNameTextBox.Text      = "";
                NetworkPortTextBox.Text          = "";

                //create a cancellation token which can be used to cancel a task
                cancelTokenSource = new CancellationTokenSource();
                cancelTokenSource.Token.Register(() => OnConnectionCancelled());

                task = UsbSerial.listAvailableDevicesAsync().AsTask <DeviceInformationCollection>(cancelTokenSource.Token);
                break;

            case "Network":
                ConnectionList.Visibility        = Visibility.Collapsed;
                DevicesText.Visibility           = Visibility.Collapsed;
                NetworkHostNameTextBox.IsEnabled = true;
                NetworkPortTextBox.IsEnabled     = true;
                BaudRateComboBox.IsEnabled       = false;
                ConnectMessage.Text = "Enter a host and port to connect.";
                task = null;
                break;
            }

            if (task != null)
            {
                //store the returned DeviceInformation items when the task completes
                task.ContinueWith(listTask =>
                {
                    //store the result and populate the device list on the UI thread
                    var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
                    {
                        Connections connections = new Connections();

                        var result = listTask.Result;
                        if (result == null || result.Count == 0)
                        {
                            ConnectMessage.Text       = "No items found.";
                            ConnectionList.Visibility = Visibility.Collapsed;
                        }
                        else
                        {
                            foreach (DeviceInformation device in result)
                            {
                                connections.Add(new Connection(device.Name, device));
                            }
                            ConnectMessage.Text = "Select an item and press \"Connect\" to connect.";
                        }

                        ConnectionList.ItemsSource = connections;
                    }));
                });
            }
        }