public ServiceList (IAdapter adapter, IDevice device)
		{
			InitializeComponent ();
			this.adapter = adapter;
			this.device = device;
			this.services = new ObservableCollection<IService> ();
			listView.ItemsSource = services;

			// when device is connected
			adapter.DeviceConnected += (s, e) => {
				device = e.Device; // do we need to overwrite this?

				// when services are discovered
				device.ServicesDiscovered += (object se, EventArgs ea) => {
					Debug.WriteLine("device.ServicesDiscovered");
					//services = (List<IService>)device.Services;
					if (services.Count == 0)
						Device.BeginInvokeOnMainThread(() => {
							foreach (var service in device.Services) {
								services.Add(service);
							}
						});
				};
				// start looking for services
				device.DiscoverServices ();

			};
			// TODO: add to IAdapter first
			//adapter.DeviceFailedToConnect += (sender, else) => {};

			DisconnectButton.Activated += (sender, e) => {
				adapter.DisconnectDevice (device);
				Navigation.PopToRootAsync(); // disconnect means start over
			};
		}
 private void DisconnectDevice()
 {
     if (connectedDevice != null)
     {
         adapter.DisconnectDevice(connectedDevice);
     }
 }
Exemple #3
0
 public void DisconnectDevice(IBluetoothLowEnergyDevice device)
 {
     if (device is BleDevice wrap)
     {
         _adapter.DisconnectDevice(wrap.Device);
     }
     else
     {
         throw new InvalidOperationException(
                   $"Only pass {nameof(IBluetoothLowEnergyDevice)} instances that were retrieved from the {nameof(ConnectedDevices)} list");
     }
 }
Exemple #4
0
        public ServiceList(IAdapter adapter, IDevice device)
        {
            InitializeComponent();
            _adapter  = adapter;
            _device   = device;
            _services = new ObservableCollection <IService>();

            listView.ItemsSource = _services;

            // when device is connected
            adapter.DeviceConnected += (s, e) =>
            {
                device = e.Device; // do we need to overwrite this?

                // when services are discovered
                device.ServicesDiscovered += (object sender, EventArgs eventArgs) =>
                {
                    Debug.WriteLine("device.ServicesDiscovered");

                    // _services = (List<IService>)device.Services;
                    if (_services.Count == 0)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            foreach (var service in device.Services)
                            {
                                _services.Add(service);
                            }
                        });
                    }

                    IsBusy = false;
                };

                // IsBusy = true; - causes crash

                // start looking for services
                device.DiscoverServices();
            };

            // TODO: add to IAdapter first
            // adapter.DeviceFailedToConnect += (sender, else) => {};

            DisconnectButton.Clicked += (sender, e) =>
            {
                adapter.DisconnectDevice(device);
                Navigation.PopToRootAsync(); // disconnect means start over
            };
        }
Exemple #5
0
        public static Task DisconnectAsync(this IAdapter adapter, IDevice device)
        {
            if (!adapter.ConnectedDevices.Contains(device))
            {
                Mvx.Trace("Disconnect async: device {0} not in the list of connected devices.", device.Name);
                return(Task.FromResult(false));
            }

            var tcs = new TaskCompletionSource <IDevice>();
            EventHandler <DeviceConnectionEventArgs> h  = null;
            EventHandler <DeviceConnectionEventArgs> he = null;

            h = (sender, e) =>
            {
                Mvx.TaggedTrace("DisconnectAsync", "Disconnected: {0} {1}", e.Device.ID, e.Device.Name);
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceDisconnected    -= h;
                    adapter.DeviceConnectionError -= he;
                    tcs.TrySetResult(e.Device);
                }
            };

            he = (sender, e) =>
            {
                // Would be nice to use C#6.0 null-conditional operators like e.Device?.ID
                Mvx.TaggedWarning("DisconnectAsync", "Disconnect Error: {0} {1}",
                                  (e.Device != null ? e.Device.ID.ToString() : ""),
                                  (e.Device != null ? e.Device.Name : ""));
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnectionError -= he;
                    adapter.DeviceDisconnected    -= h;
                    tcs.TrySetException(new Exception("Disconnect operation exception"));
                }
            };


            adapter.DeviceDisconnected    += h;
            adapter.DeviceConnectionError += he;

            adapter.DisconnectDevice(device);

            return(tcs.Task);
        }
Exemple #6
0
        public ServiceList(IAdapter adapter, IDevice device)
        {
            InitializeComponent();
            this.adapter         = adapter;
            this.device          = device;
            this.services        = new ObservableCollection <IService> ();
            listView.ItemsSource = services;

            // when device is connected
            adapter.DeviceConnected += AdapterDeviceConnected;

            // TODO: add to IAdapter first
            //adapter.DeviceFailedToConnect += (sender, else) => {};

            DisconnectButton.Clicked += (sender, e) =>
            {
                adapter.DisconnectDevice(device);
                Navigation.PopToRootAsync();                 // disconnect means start over
            };
        }
        public ServiceList(IAdapter adapter, IDevice device)
        {
            InitializeComponent();
            this.adapter = adapter;
            this.device  = device;


            // when device is connected
            adapter.DeviceConnected += (s, e) => {
                device = e.Device;                 // do we need to overwrite this?

                // when services are discovered
                device.ServicesDiscovered += (object se, EventArgs ea) => {
                    if (foundScratchflag == false)
                    {
                        Debug.WriteLine("device.ServicesDiscovered");

                        if (foundScratchflag == false)
                        {
                            Device.BeginInvokeOnMainThread(() => {
                                foreach (IService service in device.Services)
                                {
                                    if (service.ID == Scartch_Service && foundScratchflag == false)
                                    {
                                        Service = service;
                                        Debug.WriteLine(Service.ID.ToString());
                                        foundScratchflag = true;
                                    }
                                }
                                if (foundScratchflag == true)
                                {
                                    cm = new CharacteristicManager(adapter, device, Service, Scartch_Service_2);
                                    if (cm.Detail.Characteristic.CanUpdate)
                                    {
                                        cm.Detail.Characteristic.ValueUpdated += (object sender, CharacteristicReadEventArgs eve) => {
                                            Debug.WriteLine("Characteristic.ValueUpdated");
                                            Device.BeginInvokeOnMainThread(() => {
                                                bool diffused = cm.Detail.UpdateValue(cm.Detail.Characteristic, DiffuseArray);
                                                if (diffused == true)
                                                {
                                                    cm.Detail.OnDisappearing();
                                                    adapter.DisconnectDevice(device);
                                                    Navigation.PopToRootAsync();
                                                }
                                            });
                                        };

                                        cm.Detail.Characteristic.StartUpdates();

                                        DiffuseArray[0] = rnd.Next(2);
                                        DiffuseArray[1] = rnd.Next(2);
                                        DiffuseArray[2] = rnd.Next(2);
                                        Debug.WriteLine("Diffuse Red: " + DiffuseArray[0].ToString());
                                        Debug.WriteLine("Diffuse Brown: " + DiffuseArray[1].ToString());
                                        Debug.WriteLine("Diffuse Blue: " + DiffuseArray[2].ToString());
                                        bool is_cut_wire = false;
                                        if (DiffuseArray[0] == 1)
                                        {
                                            red_wire_label.Text = red_wire_label.Text + "CUT";
                                            is_cut_wire         = true;
                                        }
                                        if (DiffuseArray[1] == 1)
                                        {
                                            brown_wire_label.Text = brown_wire_label.Text + "CUT";
                                            is_cut_wire           = true;
                                        }
                                        if (DiffuseArray[2] == 1)
                                        {
                                            blue_wire_label.Text = blue_wire_label.Text + "CUT";
                                            is_cut_wire          = true;
                                        }
                                        if (is_cut_wire == false)
                                        {
                                            DiffuseArray[0]     = 1;
                                            red_wire_label.Text = red_wire_label.Text + "CUT";
                                            //if none are picked for cut wire, default to red needing to be cut
                                        }
                                    }
                                }
                            });
                        }
                        Debug.WriteLine("Start Discovery");
                        cm.service.DiscoverCharacteristics();
                    }
                };
                Device.BeginInvokeOnMainThread(() => {
                    IsBusy = false;
                });
                // start looking for services

                device.DiscoverServices();
            };

            DisconnectButton.Activated += (sender, e) => {
                cm.Detail.OnDisappearing();
                adapter.DisconnectDevice(device);
                Navigation.PopToRootAsync();
            };
        }
Exemple #8
0
 void OnDisconnectDevice(object sender, EventArgs e)
 {
     adapter.DisconnectDevice(this.device);
 }
        public ServiceList(IAdapter adapter, IDevice device)
        {
            InitializeComponent();
            this.adapter = adapter;
            this.device  = device;


            // when device is connected
            adapter.DeviceConnected += (s, e) => {
                device = e.Device;                 // do we need to overwrite this?

                // when services are discovered
                device.ServicesDiscovered += (object se, EventArgs ea) => {
                    if (foundScratchflag == false)
                    {
                        Debug.WriteLine("device.ServicesDiscovered");

                        if (foundScratchflag == false)
                        {
                            Device.BeginInvokeOnMainThread(() => {
                                foreach (IService service in device.Services)
                                {
                                    if (service.ID == Scartch_Service && foundScratchflag == false)
                                    {
                                        Service = service;
                                        Debug.WriteLine(Service.ID.ToString());
                                        foundScratchflag = true;
                                    }
                                }
                                if (foundScratchflag == true)
                                {
                                    cm = new CharacteristicManager(adapter, device, Service, Scartch_Service_2);
                                    if (cm.Detail.Characteristic.CanUpdate)
                                    {
                                        cm.Detail.Characteristic.ValueUpdated += (object sender, CharacteristicReadEventArgs eve) => {
                                            Debug.WriteLine("Characteristic.ValueUpdated");
                                            Device.BeginInvokeOnMainThread(() => {
                                                string heart_rate = cm.Detail.UpdateValue(cm.Detail.Characteristic);
                                                hr_label.Text     = String.Format("HR: {0}", heart_rate);
                                            });
                                        };

                                        cm.Detail.Characteristic.StartUpdates();
                                    }
                                }
                            });
                        }
                        Debug.WriteLine("Start Discovery");
                        cm.service.DiscoverCharacteristics();
                    }
                };
                Device.BeginInvokeOnMainThread(() => {
                    IsBusy = false;
                });
                // start looking for services

                device.DiscoverServices();
            };

            DisconnectButton.Activated += (sender, e) => {
                cm.Detail.OnDisappearing();
                adapter.DisconnectDevice(device);
                Navigation.PopToRootAsync();
            };
        }