Task <ControlClient> ConnectAsync(IAdapter adapter) { var tcs = new TaskCompletionSource <ControlClient> (); adapter.DeviceDiscovered += (object sender, DeviceDiscoveredEventArgs e) => { Device.BeginInvokeOnMainThread(async() => { // Look for a specific device if (e.Device.ID.ToString().StartsWith("af18", StringComparison.OrdinalIgnoreCase)) { // Connect to the device await adapter.ConnectAsync(e.Device); // Establish the control client using (var stream = new LEStream(e.Device)) { var client = new ControlClient(stream); client.RunAsync(CancellationToken.None); // Don't await to run in background tcs.SetResult(client); } // Update the UI connectLabel.Text = "Yay " + e.Device + "!"; } }); }; adapter.StartScanningForDevices(); return(tcs.Task); }
async Task RunControlAsync() { var cts = new CancellationTokenSource(); try { await adapter.ConnectAsync(Robot); using (var s = new LEStream(Robot)) { client = new ControlClient(s); await client.RunAsync(cts.Token); } } catch (Exception ex) { Debug.WriteLine("Stream failed"); Debug.WriteLine(ex); InvokeOnMainThread(() => { new UIAlertView("Unable to connect", "Please try to select a device again.", null, "OK").Show(); }); } finally { //client = null; } }
async Task RunControlAsync() { var cts = new CancellationTokenSource(); try { var device = await ConnectAsync(); using (var s = new LEStream(device)) { client = new ControlClient(s); variablesList.ItemsSource = client.Variables; commandsList.ItemsSource = client.Commands; await client.RunAsync(cts.Token); } } catch (Exception ex) { Debug.WriteLine("Stream failed"); Debug.WriteLine(ex); } finally { client = null; } }