示例#1
0
		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;
		}
示例#2
0
        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 <IDevice> ConnectAsync()
        {
            var device = adapter.DiscoveredDevices.First(x => x.ID == deviceId);

            Debug.WriteLine("Connecting to " + device.Name + "...");
            await adapter.ConnectAsync(device);

            Debug.WriteLine("Trying to read...");
            return(device);
        }