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; } }
public MainPage() { this.InitializeComponent(); if( useBluetooth ) { /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ bluetooth = new BluetoothSerial( "RNBT-E072" ); arduino = new RemoteDevice( bluetooth ); bluetooth.ConnectionEstablished += OnConnectionEstablished; //these parameters don't matter for bluetooth bluetooth.begin( 0, 0 ); } else { /* * I've written my Arduino device VID and PID as a parameter to the BluetoothSerial constructor. You should change this to your * device VID and PID if using USB. You can also use the UsbSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ usb = new UsbSerial( "VID_2341", "PID_0043" ); //I've written in my device D directly arduino = new RemoteDevice( usb ); usb.ConnectionEstablished += OnConnectionEstablished; //SerialConfig.8N1 is the default config for Arduino devices over USB usb.begin( 115200, SerialConfig.SERIAL_8N1 ); } }
public MainPage() { this.InitializeComponent(); if (useBluetooth) { /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ bluetooth = new BluetoothSerial("RNBT-E072"); arduino = new RemoteDevice(bluetooth); bluetooth.ConnectionEstablished += OnConnectionEstablished; //these parameters don't matter for bluetooth bluetooth.begin(0, 0); } else { /* * I've written my Arduino device VID and PID as a parameter to the BluetoothSerial constructor. You should change this to your * device VID and PID if using USB. You can also use the UsbSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ usb = new UsbSerial("VID_2341", "PID_0043"); //I've written in my device D directly arduino = new RemoteDevice(usb); usb.ConnectionEstablished += OnConnectionEstablished; //SerialConfig.8N1 is the default config for Arduino devices over USB usb.begin(115200, SerialConfig.SERIAL_8N1); } }
public ControlPage() { this.InitializeComponent(); turn = Turn.none; direction = Direction.none; accelerometer = App.accelerometer; bluetooth = App.bluetooth; arduino = App.arduino; if (accelerometer == null || bluetooth == null || arduino == null) { Frame.Navigate(typeof(MainPage)); return; } startButton.IsEnabled = true; stopButton.IsEnabled = true; disconnectButton.IsEnabled = true; keepScreenOnRequest = new DisplayRequest(); keepScreenOnRequest.RequestActive(); App.arduino.pinMode(enableA, PinMode.OUTPUT); App.arduino.pinMode(MotorA1, PinMode.OUTPUT); App.arduino.pinMode(MotorA2, PinMode.OUTPUT); App.arduino.pinMode(enableB, PinMode.OUTPUT); App.arduino.pinMode(MotorB1, PinMode.OUTPUT); App.arduino.pinMode(MotorB2, PinMode.OUTPUT); arduino.digitalWrite(enableA, PinState.HIGH); arduino.digitalWrite(enableB, PinState.HIGH); }
private void MainPage_Loaded(object sender, RoutedEventArgs e) { var connection = new BluetoothSerial("RNBT-A297"); _arduino = new RemoteDevice(connection); connection.ConnectionEstablished += Connection_ConnectionEstablished; connection.begin(0, 0); }
public ControlPageMaisto() { this.InitializeComponent(); turn = Turn.none; direction = Direction.none; accelerometer = App.accelerometer; bluetooth = App.bluetooth; arduino = App.arduino; if( accelerometer == null || bluetooth == null || arduino == null ) { Frame.Navigate( typeof( MainPage ) ); return; } startButton.IsEnabled = true; stopButton.IsEnabled = true; disconnectButton.IsEnabled = true; bluetooth.ConnectionLost += Bluetooth_ConnectionLost; keepScreenOnRequest = new DisplayRequest(); keepScreenOnRequest.RequestActive(); App.arduino.pinMode( FORWARD_CONTROL_PIN, PinMode.OUTPUT ); App.arduino.pinMode( REVERSE_CONTROL_PIN, PinMode.OUTPUT ); App.arduino.pinMode( LEFT_CONTROL_PIN, PinMode.OUTPUT ); App.arduino.pinMode( RIGHT_CONTROL_PIN, PinMode.OUTPUT ); }
/// <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); }
public ControlPageMaisto() { this.InitializeComponent(); turn = Turn.none; direction = Direction.none; accelerometer = App.accelerometer; bluetooth = App.bluetooth; arduino = App.arduino; if (accelerometer == null || bluetooth == null || arduino == null) { Frame.Navigate(typeof(MainPage)); return; } startButton.IsEnabled = true; stopButton.IsEnabled = true; disconnectButton.IsEnabled = true; bluetooth.ConnectionLost += Bluetooth_ConnectionLost; keepScreenOnRequest = new DisplayRequest(); keepScreenOnRequest.RequestActive(); App.arduino.pinMode(FORWARD_CONTROL_PIN, PinMode.OUTPUT); App.arduino.pinMode(REVERSE_CONTROL_PIN, PinMode.OUTPUT); App.arduino.pinMode(LEFT_CONTROL_PIN, PinMode.OUTPUT); App.arduino.pinMode(RIGHT_CONTROL_PIN, PinMode.OUTPUT); }
public ControlPage() { this.InitializeComponent(); turn = Turn.none; direction = Direction.none; accelerometer = App.accelerometer; bluetooth = App.bluetooth; arduino = App.arduino; if( accelerometer == null || bluetooth == null || arduino == null ) { Frame.Navigate( typeof( MainPage ) ); return; } startButton.IsEnabled = true; stopButton.IsEnabled = true; disconnectButton.IsEnabled = true; bluetooth.ConnectionLost += Bluetooth_ConnectionLost; keepScreenOnRequest = new DisplayRequest(); keepScreenOnRequest.RequestActive(); App.arduino.pinMode( LR_DIRECTION_CONTROL_PIN, PinMode.OUTPUT ); App.arduino.pinMode( FB_DIRECTION_CONTROL_PIN, PinMode.OUTPUT ); App.arduino.pinMode( LR_MOTOR_CONTROL_PIN, PinMode.PWM ); App.arduino.pinMode( FB_MOTOR_CONTROL_PIN, PinMode.PWM ); App.arduino.pinMode(HEARTBEAT_LED_PIN, PinMode.OUTPUT); }
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); } }); } }
/// <summary> /// Connect to arduino over bloetooth /// </summary> /// <param name="DeviceID">Device name or ID</param> /// <returns></returns> public static ArduinoController BluetoothConnection(string DeviceID, uint baudRate = 115200, SerialConfig serialConfig = SerialConfig.SERIAL_8N1) { BluetoothSerial Connection1 = new BluetoothSerial(DeviceID); var res = new ArduinoController(Connection1); Connection1.begin(baudRate, serialConfig); res.bluetoothConnection = Connection1; res._connType = ConnectionType.Bluetooth; return(res); }
private void InitWRA() { connection = new BluetoothSerial("Dev B"); // HC-06 arduino = new RemoteDevice(connection); connection.ConnectionEstablished += Connection_ConnectionEstablished; connection.ConnectionFailed += Connection_ConnectionFailed; connection.ConnectionLost += Connection_ConnectionLost; connection.begin(9600, 0); }
public PinControl(string btName, uint BaudRate) { BTConnection = new BluetoothSerial("FixHelp"); baudRate = BaudRate; isBtAvailable = false; arduino = new RemoteDevice(BTConnection); BTConnection.begin(baudRate, SerialConfig.SERIAL_8N1); isBtAvailable = BTConnection.connectionReady(); }
public MainPage() { this.InitializeComponent(); if (USE_VBB_CONNECTION) { /* * VirtualBreadboard Virtual Firmata device (www.virtualbreadboard.com) * - is the server, so you must run VBB first * - connection port 5000 as set in the VBB firmata port property * - connects using the local IP address, 127.0.0.1 * - You must enable Private Networks ( Cient & Server ) in thePackage : Capabilities manifest */ LocalSerialSocket localSocket = new LocalSerialSocket(5000); if (USE_CUSTOM_PROTOCOL) { myRemoteProtocol = new CustomRemoteProtocol(localSocket); } else { arduino = new RemoteDevice(localSocket); } localSocket.ConnectionEstablished += OnConnectionEstablished; connection = localSocket; } else { /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ BluetoothSerial bluetooth = new BluetoothSerial("RNBT-E072"); if (USE_CUSTOM_PROTOCOL) { myRemoteProtocol = new CustomRemoteProtocol(bluetooth); } else { arduino = new RemoteDevice(bluetooth); } bluetooth.ConnectionEstablished += OnConnectionEstablished; connection = bluetooth; } //these parameters don't matter for localSocket/bluetooth connection.begin(115200, SerialConfig.SERIAL_8N1); }
public MainPage() { this.InitializeComponent(); if (USE_VBB_CONNECTION) { /* * VirtualBreadboard Virtual Firmata device (www.virtualbreadboard.com) * - is the server, so you must run VBB first * - connection port 5000 as set in the VBB firmata port property * - connects using the local IP address, 127.0.0.1 * - You must enable Private Networks ( Cient & Server ) in thePackage : Capabilities manifest */ LocalSerialSocket localSocket = new LocalSerialSocket(5000); if (USE_CUSTOM_PROTOCOL) { myRemoteProtocol = new CustomRemoteProtocol(localSocket); } else { arduino = new RemoteDevice(localSocket); } arduino.DeviceReady += OnConnectionEstablished; connection = localSocket; } else { /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ BluetoothSerial bluetooth = new BluetoothSerial("RNBT-E072"); if (USE_CUSTOM_PROTOCOL) { myRemoteProtocol = new CustomRemoteProtocol(bluetooth); } else { arduino = new RemoteDevice(bluetooth); } bluetooth.ConnectionEstablished += OnConnectionEstablished; connection = bluetooth; } //these parameters don't matter for localSocket/bluetooth connection.begin(115200, SerialConfig.SERIAL_8N1); }
public async Task CaptureReadings() { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.bluetooth = new BluetoothSerial("RNBT-9E86"); this.firmata = new UwpFirmata(); this.firmata.FirmataConnectionReady += Firmata_FirmataConnectionReady; this.firmata.StringMessageReceived += Firmata_StringMessageReceived; this.device = new RemoteDevice(firmata); this.firmata.begin(bluetooth); bluetooth.begin(9600, SerialConfig.SERIAL_8N1); }); }
private void btnStart_Click(object sender, RoutedEventArgs e) { //_bluetooth = new BluetoothSerial("HC-05"); _bluetooth = new BluetoothSerial("sowaphone"); _arduino = new RemoteDevice(_bluetooth); _bluetooth.ConnectionLost += _bluetooth_ConnectionLost; _bluetooth.ConnectionFailed += _bluetooth_ConnectionFailed; _bluetooth.ConnectionEstablished += OnConnectionEstablished; _bluetooth.begin(0, SerialConfig.SERIAL_8N1); }
private async void connect() { dt = new DispatcherTimer() { Interval = new TimeSpan(500) }; var dev = await BluetoothSerial.listAvailableDevicesAsync(); foreach (var x in dev) { System.Diagnostics.Debug.WriteLine("Found " + x.Name); } bluetoothcomm = new BluetoothSerial(dev[0]); arduino = new RemoteDevice(bluetoothcomm); bluetoothcomm.ConnectionEstablished+= Comm_ConnectionEstablished; bluetoothcomm.begin(57600, SerialConfig.SERIAL_8N1); }
public MainPage() { this.InitializeComponent(); var keepScreenOnRequest = new Windows.System.Display.DisplayRequest(); keepScreenOnRequest.RequestActive(); //ApplicationView.GetForCurrentView().TryEnterFullScreenMode(); DispatcherHelper.Dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; DispatcherHelper.SyncContext = TaskScheduler.FromCurrentSynchronizationContext(); _usb = new UsbSerial("VID_1A86", "PID_7523"); _bluetooth = new BluetoothSerial("TinBot"); _body = new Body(_usb); //_body = new Body(_bluetooth); _commands = new Operations.Commands(media, _body, Ear, new Dictionary <Storyboard, int>() { [bravo] = 200, [feliz] = 100, [feliz_verde] = 100, [normal] = 0, [piscadela] = 0, [piscando] = 0, [piscando_duplo] = 0, [triste] = 200 }, new Dictionary <ETinBotFaces, Storyboard>() { [ETinBotFaces.Angry] = bravo, [ETinBotFaces.Happy] = feliz, [ETinBotFaces.HappyGreen] = feliz_verde, [ETinBotFaces.Normal] = normal, [ETinBotFaces.UniBlink] = piscadela, [ETinBotFaces.Blink] = piscando, [ETinBotFaces.BlinkDouble] = piscando_duplo, [ETinBotFaces.Sad] = triste }, new Dictionary <ETinBotToggle, int>() { [ETinBotToggle.Green] = 1, [ETinBotToggle.Red] = 2, [ETinBotToggle.Blue] = 3, [ETinBotToggle.Laser] = 4 } ); _body.ConnectionNotify += (sender, s) => ExecuteOnMainThread(() => Label.Text = s); _body.Setup(); }
public MainPage() { this.InitializeComponent(); Connection = new BluetoothSerial("RN42-D924"); Arduino = new RemoteDevice(Connection); DataContext = this; Connection.ConnectionEstablished += Connection_ConnectionEstablished; IsConnected = false; Connection.begin(115200, SerialConfig.SERIAL_8N1); }
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); } })); }); } } }
public MainPage() { this.InitializeComponent(); var keepScreenOnRequest = new Windows.System.Display.DisplayRequest(); keepScreenOnRequest.RequestActive(); //ApplicationView.GetForCurrentView().TryEnterFullScreenMode(); DispatcherHelper.Dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; DispatcherHelper.SyncContext = TaskScheduler.FromCurrentSynchronizationContext(); _usb = new UsbSerial("VID_1A86", "PID_7523"); _bluetooth = new BluetoothSerial("TinBot"); _body = new Body(_usb); //_body = new Body(_bluetooth); _commands = new Operations.Commands(media, _body, Ear, new Dictionary<Storyboard, int>() { [bravo] = 200, [feliz] = 100, [feliz_verde] = 100, [normal] = 0, [piscadela] = 0, [piscando] = 0, [piscando_duplo] = 0, [triste] = 200 }, new Dictionary<ETinBotFaces, Storyboard>() { [ETinBotFaces.Angry] = bravo, [ETinBotFaces.Happy] = feliz, [ETinBotFaces.HappyGreen] = feliz_verde, [ETinBotFaces.Normal] = normal, [ETinBotFaces.UniBlink] = piscadela, [ETinBotFaces.Blink] = piscando, [ETinBotFaces.BlinkDouble] = piscando_duplo, [ETinBotFaces.Sad] = triste }, new Dictionary<ETinBotToggle, int>() { [ETinBotToggle.Green] = 1, [ETinBotToggle.Red] = 2, [ETinBotToggle.Blue] = 3, [ETinBotToggle.Laser] = 4 } ); _body.ConnectionNotify += (sender, s) => ExecuteOnMainThread(() => Label.Text = s); _body.Setup(); }
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; }
public MainPage() { this.InitializeComponent(); /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ bluetooth = new BluetoothSerial("RNBT-E072"); arduino = new RemoteDevice(bluetooth); bluetooth.ConnectionEstablished += OnConnectionEstablished; //these parameters don't matter for bluetooth bluetooth.begin(115200, SerialConfig.SERIAL_8N1); }
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 MainPage() { this.InitializeComponent(); /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ bluetooth = new BluetoothSerial( "RNBT-E072" ); arduino = new RemoteDevice( bluetooth ); bluetooth.ConnectionEstablished += OnConnectionEstablished; //these parameters don't matter for bluetooth bluetooth.begin( 115200, SerialConfig.SERIAL_8N1 ); }
public Body(BluetoothSerial bluetoothSerial) { _bluetoothSerial = bluetoothSerial; _bluetoothSerial.ConnectionEstablished += () => ConnectionNotify?.Invoke(this, "Bluetooth estabelecido"); _bluetoothSerial.ConnectionFailed += async message => { ConnectionNotify?.Invoke(this, "Bluetooth falhou: " + message); await Task.Delay(5000); Setup(); }; _bluetoothSerial.ConnectionLost += message => { ConnectionNotify?.Invoke(this, "Bluetooth perdido: " + message); Setup(); }; _checkConnectiontimer.Interval = TimeSpan.FromSeconds(30); _checkConnectiontimer.Tick += CheckConnectiontimerOnTick; }
public MainPage() { this.InitializeComponent(); /* * I've written my bluetooth device name as a parameter to the BluetoothSerial constructor. You should change this to your previously-paired * device name if using Bluetooth. You can also use the BluetoothSerial.listAvailableDevicesAsync() function to list * available devices, but that is not covered in this sample. */ bluetooth = new BluetoothSerial("FireFly-748B"); arduino = new RemoteDevice(bluetooth); bluetooth.ConnectionEstablished += OnConnectionEstablished; //this.pbPolltimer = new DispatcherTimer(); //this.pbPolltimer.Interval = TimeSpan.FromMilliseconds(250); //this.pbPolltimer.Tick += PBTimer_Tick; //this.pbPolltimer.Stop(); //Start with off color for ellipse this.PBStatusLED.Fill = grayBrush; }
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); }
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; })); }); } }