private void ProcessConnection()
        {
            if (_device.State == DeviceState.Disconnected)
            {
                _adapter.ConnectToDevice(_device);
                _adapter.DeviceConnected += AdapterOnDeviceConnected;
            }
            else if (_device.State == DeviceState.Connected && Services.Count == 0 && !_isDiscoveringServices)
            {
                _device.ServicesDiscovered += DeviceOnServicesDiscovered;
                _device.DiscoverServices();
                _isDiscoveringServices = true;
            }
            else if (_device.State == DeviceState.Connected && _isDiscoveringServices && _device.Services != null && _device.Services.Count > 0)
            {
                RaisePropertyChanged(() => ServiceCount);
                Services.Clear();

                foreach (var service in _device.Services)
                {
                    Services.Add(new ServiceViewModel()
                    {
                        Id = service.Uuid
                    });
                }
            }
        }
示例#2
0
        /// <summary>
        /// Asynchronously gets the requested service
        /// </summary>
        public static Task <IDevice> ConnectAsync(this IAdapter adapter, IDevice device)
        {
            if (device.State == DeviceState.Connected)
            {
                return(Task.FromResult <IDevice>(null));
            }

            var completionSource = new TaskCompletionSource <IDevice>();

            EventHandler <DeviceConnectionEventArgs> handler = null;

            handler = (sender, e) =>
            {
                Debug.WriteLine("CCC: " + e.Device.ID + " " + e.Device.State);
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnected -= handler;
                    completionSource.SetResult(e.Device);
                }
            };

            adapter.DeviceConnected += handler;

            adapter.ConnectToDevice(device);

            return(completionSource.Task);
        }
示例#3
0
 public BluetoothComInterface(IAdapter adapter, IDevice device)
 {
     Adapter = adapter;
     Device  = device;
     adapter.DeviceConnected    += OnDeviceConnect;
     adapter.DeviceDisconnected += OnDeviceDisconnect;
     Adapter.ConnectToDevice(device);
 }
 private void ConnectToDevice()
 {
     this.services        = new ObservableCollection <IService>();
     this.characteristics = new ObservableCollection <ICharacteristic>();
     adapter.ConnectToDevice(device);
     GetSensor();
     SetEvents();
 }
示例#5
0
        void DeviceSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var device = e.SelectedItem as IDevice;

            if (device != null)
            {
                _adapter.ConnectToDevice(device);
            }
        }
 private void AdapterOnDeviceDiscovered(object sender, DeviceDiscoveredEventArgs deviceDiscoveredEventArgs)
 {
     if (Devices.All(x => x.Device != deviceDiscoveredEventArgs.Device))
     {
         Devices.Add(new DeviceViewModel(deviceDiscoveredEventArgs.Device));
         _adapter.ConnectToDevice(deviceDiscoveredEventArgs.Device);
         RaisePropertyChanged(() => Devices);
     }
 }
示例#7
0
 protected override void OnAppearing()
 {
     base.OnAppearing();
     if (characteristics.Count == 0)
     {
         Debug.WriteLine("No characteristics, attempting to find some");
         // start looking for the device
         adapter.ConnectToDevice(device);
     }
 }
示例#8
0
 protected override void OnAppearing()
 {
     base.OnAppearing();
     if (services.Count == 0)
     {
         Debug.WriteLine("No services, attempting to connect to device");
         // start looking for the device
         adapter.ConnectToDevice(device);
     }
 }
 protected override void OnAppearing()
 {
     base.OnAppearing();
     if (foundScratchflag == false)
     {
         Debug.WriteLine("No services, attempting to connect to device");
         IsBusy = true;
         // start looking for the device
         adapter.ConnectToDevice(device);
     }
 }
示例#10
0
 public void ConnectToDevice(IBluetoothLowEnergyDevice device)
 {
     if (device is BleDevice wrap)
     {
         _adapter.ConnectToDevice(wrap.Device);
     }
     else
     {
         throw new InvalidOperationException(
                   $"Only pass {nameof(IBluetoothLowEnergyDevice)} instances that were retrieved from the {nameof(DiscoveredDevices)} list");
     }
 }
示例#11
0
        private Task <IDevice> Connect(Guid deviceAddress)
        {
            var device = _adapter
                         .ConnectedDevices
                         .FirstOrDefault(d => d.ID == deviceAddress);

            if (device != null)
            {
                return(Task.FromResult(device));
            }

            var taskCompletionSource = new TaskCompletionSource <IDevice>();

            _adapter.DeviceDiscovered += (sender, args) => {
                if (args.Device.ID == deviceAddress &&
                    !_connectingAddresses.Contains(deviceAddress))
                {
                    _adapter.StopScanningForDevices();
                    _connectingAddresses.Add(deviceAddress);
                    Platform.Current.RunOnMainThread(() => {
                        _adapter.ConnectToDevice(args.Device);
                    });
                }
            };

            _adapter.ScanTimeoutElapsed += (sender, args) =>
            {
                Platform.Current.RunOnMainThread(() => {
                    _adapter.StopScanningForDevices();
                    if (_connectingAddresses.Count == 0)
                    {
                        taskCompletionSource.SetResult(null);
                    }
                });
            };

            _adapter.DeviceConnected += (sender, args) =>
            {
                Platform.Current.RunOnMainThread(() =>
                {
                    _connectingAddresses.Remove(deviceAddress);
                    taskCompletionSource.SetResult(args.Device);
                });
            };

            _adapter.StartScanningForDevices(Guid.Empty);

            return(taskCompletionSource.Task);
        }
示例#12
0
        /// <summary>
        /// Asynchronously gets the requested service
        /// </summary>
        public static Task <IDevice> ConnectAsync(this IAdapter adapter, IDevice device)
        {
            if (device.State == DeviceState.Connected)
            {
                return(Task.FromResult <IDevice>(device));
            }

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

            h = (sender, e) =>
            {
                Mvx.TaggedTrace("ConnectAsync", "Connected: {0} {1}", e.Device.ID, e.Device.Name);
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnected       -= 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("ConnectAsync", "Connection 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.DeviceConnected       -= h;
                    tcs.TrySetException(new Exception("Connect operation exception"));
                }
            };

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

            adapter.ConnectToDevice(device);

            return(tcs.Task);
        }
        //Code voor het scannen van naar services van de censortag
        public void ScanForServices()
        {
            Debug.WriteLine("***Scan for Services***");

            if (services.Count == 0)
            {
                Debug.WriteLine("No services, attempting to connect to device");
                Ready = "No services, attempting to connect to device";
                // start looking for the device
                adapter.ConnectToDevice(device);
            }


            adapter.DeviceConnected += (s, e) =>
            {
                device = e.Device; // do we need to overwrite this?
                //when services are discovered
                device.ServicesDiscovered += Device_ServicesDiscovered;
                device.DiscoverServices();
            };
        }
示例#14
0
        public static Task <IDevice> ConnectAsync(this IAdapter adapter, IDevice device)
        {
            if (device.State == DeviceState.Connected)
            {
                return(Task.FromResult <IDevice>(null));
            }
            var tcs = new TaskCompletionSource <IDevice>();

            void NewDeviceConnected(object sender, BluetoothDeviceConnectionEventArgs e)
            {
                Debug.WriteLine("CCC: " + e.Device.ID + " " + e.Device.State);
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnected -= NewDeviceConnected;
                    tcs.SetResult(e.Device);
                }
            }

            adapter.DeviceConnected += NewDeviceConnected;
            adapter.ConnectToDevice(device);
            return(tcs.Task);
        }
示例#15
0
        /// <summary>
        /// Asynchronously gets the requested service
        /// </summary>
        public static Task ConnectAsync(this IAdapter adapter, IDevice device)
        {
            if (device.State == DeviceState.Connected)
            {
                return(Task.FromResult <object> (null));
            }

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

            h = (sender, e) => {
                Debug.WriteLine("CCC: " + e.Device.ID + " " + e.Device.State);
                if (e.Device.ID == device.ID && device.State == DeviceState.Connected)
                {
                    adapter.DeviceConnected -= h;
                    tcs.SetResult(null);
                }
            };
            adapter.DeviceConnected += h;

            adapter.ConnectToDevice(device);

            return(tcs.Task);
        }
示例#16
0
 private void ConnectDevice(IDevice device)
 {
     adapter.ConnectToDevice(device);
 }
示例#17
0
 /// <summary>
 ///     Connects to wand.
 /// </summary>
 /// <param name="device">Device.</param>
 public void ConnectToWand(IDevice device)
 {
     SelectedDevice = device;
     Adapter.ConnectToDevice(SelectedDevice);
 }