Ejemplo n.º 1
0
        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);
                    }
                });
            }
        }
Ejemplo n.º 2
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);
                            }
                        }));
                    });
                }
            }
        }
Ejemplo n.º 3
0
        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);
        }