Exemplo n.º 1
0
 public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
 {
     if (gatt != null)
     {
         if (newState == ProfileState.Connected && status != GattStatus.Success)
         {
             gatt.DiscoverServices();
             client.callback.onConnectToDevice(gatt.Device.Address.ToUpper(), gatt.Device.Name, false);
         }
         else if (newState == ProfileState.Connected)
         {
             gatt.DiscoverServices();
             client._isConnected = true;
         }
         if (newState == ProfileState.Disconnected)
         {
             client.serviceObjects.Clear();
             client.characteristicObjects.Clear();
             client.descriptorObjects.Clear();
             client.callback.onDisconnectFromDevice(gatt.Device.Address.ToUpper(), gatt.Device.Name);
             client._isConnected = false;
         }
     }
 }
Exemplo n.º 2
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);

            switch (newState)
            {
            case ProfileState.Disconnected:
                _MainActivity.AppendLogText("Disconnected");
                break;

            case ProfileState.Connected:
                _MainActivity.AppendLogText("Connected");
                gatt.DiscoverServices();
                break;
            }
        }
Exemplo n.º 3
0
        private void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            if (_connectTaskCompletionSource.Task.IsCanceled)
            {
                return;
            }

            if (status == GattStatus.Success)
            {
                gatt.DiscoverServices();
            }
            else
            {
                _connectTaskCompletionSource.SetResult(false);
            }
        }
Exemplo n.º 4
0
        protected override async Task <IEnumerable <IService> > GetServicesNativeAsync()
        {
            if (_gattCallback == null || _gatt == null)
            {
                return(Enumerable.Empty <IService>());
            }

            return(await TaskBuilder.FromEvent <IEnumerable <IService>, EventHandler <ServicesDiscoveredCallbackEventArgs> >(
                       execute : () => _gatt.DiscoverServices(),
                       getCompleteHandler : (complete, reject) => ((sender, args) =>
            {
                complete(_gatt.Services.Select(service => new Service(service, _gatt, _gattCallback, this)));
            }),
                       subscribeComplete : handler => _gattCallback.ServicesDiscovered += handler,
                       unsubscribeComplete : handler => _gattCallback.ServicesDiscovered -= handler));
        }
Exemplo n.º 5
0
            public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
            {
                base.OnConnectionStateChange(gatt, status, newState);

                if (status == GattStatus.Success)
                {
                    if (newState == ProfileState.Connected)
                    {
                        gatt.DiscoverServices();
                    }
                    else if (newState == ProfileState.Disconnected)
                    {
                        Log.Debug(tag, "GATT DESCONECTADO");
                        gatt.Close();
                        gatt = null;
                    }
                }
            }
Exemplo n.º 6
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState state)
        {
            if (state == ProfileState.Connected)
            {
                Log.Debug(TAG, string.Format("connected to GATT server '{0}'", gattServerName));
                if (gatt != null)
                {
                    Log.Debug(TAG, string.Format("discover services of '{0}'", gattServerName));
                    gatt.DiscoverServices();
                }
            }
            else if (state == ProfileState.Disconnected)
            {
                Log.Debug(TAG, string.Format("disconnected from GATT server '{0}'", gattServerName));
            }

            //base.OnConnectionStateChange(gatt, status, state);
        }
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            switch (newState)
            {
            case ProfileState.Connected:
                gatt.DiscoverServices();
                break;

            case ProfileState.Connecting:
                break;

            case ProfileState.Disconnecting:
                Disconnect();
                break;

            case ProfileState.Disconnected:
                Disconnect();
                break;
            }
        }
Exemplo n.º 8
0
 public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
 {
     base.OnConnectionStateChange(gatt, status, newState);
     System.Threading.Tasks.Task.Run(() =>
     {
         if (newState == ProfileState.Connected)
         {
             if (gatt != null)
             {
                 _deviceManager.HandleDeviceConnected(_descriptor, (d) =>
                 {
                     gatt.DiscoverServices();
                 });
             }
         }
         else if (newState == ProfileState.Disconnected)
         {
             _deviceManager.HandleDeviceDisconnected(_descriptor);
         }
     }).FireAndForget();
 }
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);

            if (newState == ProfileState.Disconnected)
            {
                log.Debug("Watchdog disconnected.");
                dataService.DT1HardwareConnectionStatus = BleConnectionStatus.Disconnected;
            }

            if (newState == ProfileState.Connected)
            {
                log.Debug("Watchdog connected.");
                dataService.DT1HardwareConnectionStatus = BleConnectionStatus.Connected;

                while (!gatt.DiscoverServices())
                {
                    Thread.Yield();
                }
            }
        }
Exemplo n.º 10
0
        protected override async Task <IEnumerable <IService> > GetServicesNativeAsync()
        {
            if (_gattCallback == null || _gatt == null)
            {
                return(Enumerable.Empty <IService>());
            }

            var tcs = new TaskCompletionSource <IEnumerable <IService> >();
            EventHandler <ServicesDiscoveredCallbackEventArgs> handler = null;

            handler = (sender, args) =>
            {
                _gattCallback.ServicesDiscovered -= handler;
                tcs.TrySetResult(_gatt.Services.Select(service => new Service(service, _gatt, _gattCallback)));
            };

            _gattCallback.ServicesDiscovered += handler;
            _gatt.DiscoverServices();

            return(await tcs.Task);
        }
        // Overridden from BluetoothGattCallback
        public sealed override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            lock (locker) {
                switch (newState)
                {
                case ProfileState.Connected: {
                    handler.RemoveCallbacksAndMessages(null);
                    connectionState = EConnectionState.Resolving;
                    lastSeen        = DateTime.Now; // We at least know that the device is nearby at this point.

                    if (!gatt.DiscoverServices())
                    {
                        Log.E(this, "Failed to start service discovery for: " + device.Name);
                        Disconnect();
                    }
                    else
                    {
                        connectionState = EConnectionState.Resolving;
                        handler.SendEmptyMessageDelayed(MSG_CHECK_SERVICES, SERVICES_DELAY);
                    }
                    break;
                } // ProfileState.Connected

                case ProfileState.Connecting: {
                    break;
                } // ProfileState.Connecting

                case ProfileState.Disconnected: {
                    // Only attempt a reconnect if the device disconnected unexpectedly
                    bool reconnect = connectionState == EConnectionState.Connected && manager.ion.preferences.device.allowDeviceAutoConnect;
                    Disconnect(reconnect);
                    break;
                } // ProfileState.Disconnected

                case ProfileState.Disconnecting: {
                    break;
                } // ProfileState.Disconnecting
                }
            }
        }
Exemplo n.º 12
0
        protected override async Task <IReadOnlyList <IService> > GetServicesNativeAsync()
        {
            if (_gattCallback == null || _gatt == null)
            {
                return(new List <IService>());
            }

            return(await TaskBuilder.FromEvent <IReadOnlyList <IService>, EventHandler <ServicesDiscoveredCallbackEventArgs>, EventHandler>(
                       execute : () => _gatt.DiscoverServices(),
                       getCompleteHandler : (complete, reject) => ((sender, args) =>
            {
                complete(_gatt.Services.Select(service => new Service(service, _gatt, _gattCallback, this)).ToList());
            }),
                       subscribeComplete : handler => _gattCallback.ServicesDiscovered += handler,
                       unsubscribeComplete : handler => _gattCallback.ServicesDiscovered -= handler,
                       getRejectHandler : reject => ((sender, args) =>
            {
                reject(new Exception($"Device {Name} disconnected while fetching services."));
            }),
                       subscribeReject : handler => _gattCallback.ConnectionInterrupted += handler,
                       unsubscribeReject : handler => _gattCallback.ConnectionInterrupted -= handler));
        }
Exemplo n.º 13
0
        public Task <List <IBLEService> > GetServicesAsync()
        {
            servicesDiscoveryTCS = new TaskCompletionSource <List <IBLEService> >();

            try
            {
                if (_gatt == null)
                {
                    Debug.WriteLine("Connect to Bluetooth Device first");
                    servicesDiscoveryTCS.TrySetException(new Exception("Connect to Bluetooth Device first"));
                }

                var x = _gatt.DiscoverServices();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                servicesDiscoveryTCS.TrySetException(e);
            }

            return(servicesDiscoveryTCS.Task);
        }
Exemplo n.º 14
0
        private IReadOnlyList <GattDeviceService> GetGattServices()
        {
            List <GattDeviceService> services = new List <GattDeviceService>();

            if (_bluetoothGatt == null)
            {
                _gattCallback  = new GattCallback(this);
                _bluetoothGatt = _device.ConnectGatt(Android.App.Application.Context, true, _gattCallback);
                _discoveryHandle.WaitOne();
            }

            if (_bluetoothGatt.DiscoverServices())
            {
                _discoveryHandle.WaitOne();
            }

            foreach (BluetoothGattService service in _bluetoothGatt.Services)
            {
                services.Add(new GattDeviceService(this, service));
            }

            return(services.AsReadOnly());
        }
 private async Task <IReadOnlyList <Service> > DiscoverServicesInternal()
 {
     return(await TaskBuilder
            .FromEvent <IReadOnlyList <Service>, EventHandler <ServicesDiscoveredCallbackEventArgs>, EventHandler>(
                execute : () =>
     {
         if (!_gatt.DiscoverServices())
         {
             throw new Exception("Could not start service discovery");
         }
     },
                getCompleteHandler : (complete, reject) => ((sender, args) =>
     {
         complete(_gatt.Services.Select(service => new Service(service, _gatt, _gattCallback, this)).ToList());
     }),
                subscribeComplete : handler => _gattCallback.ServicesDiscovered += handler,
                unsubscribeComplete : handler => _gattCallback.ServicesDiscovered -= handler,
                getRejectHandler : reject => ((sender, args) =>
     {
         reject(new Exception($"Device {Name} disconnected while fetching services."));
     }),
                subscribeReject : handler => _gattCallback.ConnectionInterrupted += handler,
                unsubscribeReject : handler => _gattCallback.ConnectionInterrupted -= handler));
 }
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            switch (status)
            {
            case GattStatus.ConnectionCongested:
                break;

            case GattStatus.Failure:
                break;

            case GattStatus.InsufficientAuthentication:
                break;

            case GattStatus.InsufficientEncryption:
                break;

            case GattStatus.InvalidAttributeLength:
                break;

            case GattStatus.InvalidOffset:
                break;

            case GattStatus.ReadNotPermitted:
                break;

            case GattStatus.RequestNotSupported:
                break;

            case GattStatus.Success:
                switch (newState)
                {
                case ProfileState.Connected:
                    break;

                case ProfileState.Connecting:
                    break;

                case ProfileState.Disconnected:
                    if (_record != null)
                    {
                        (_context as GlucoseDeviceActivity)?.RunOnUiThread(() => {
                            DisplayMessageToUi("Citirea s-a efectuat cu success");
                            (_context as GlucoseDeviceActivity)?.UpdateUi(
                                _record?.GlucoseConcentration * 100000);
                        });
                    }
                    else
                    {
                        (_context as GlucoseDeviceActivity)?.RunOnUiThread(() => {
                            DisplayMessageToUi("Nu s-au gasit date");
                            (_context as GlucoseDeviceActivity)?.UpdateUi();
                        });
                    }

                    break;

                case ProfileState.Disconnecting:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(newState), newState, null);
                }

                if (newState == ProfileState.Connected)
                {
                    gatt.DiscoverServices();
                }

                break;

            case GattStatus.WriteNotPermitted:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(status), status, null);
            }
        }
Exemplo n.º 17
0
 public override void OnMtuChanged(BluetoothGatt gatt, int mtu, GattStatus status)
 {
     _parent.OnStatusUpdated("Discovering services");
     gatt.DiscoverServices();
 }
Exemplo n.º 18
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
        {
            switch (newState)
            {
            case ProfileState.Connecting:
                break;

            case ProfileState.Connected:
                lock (_lock)
                {
                    if (State == BluetoothLEDeviceState.Connecting && status == GattStatus.Success)
                    {
                        State = BluetoothLEDeviceState.Discovering;
                        Task.Run(async() =>
                        {
                            await Task.Delay(750);
                            lock (_lock)
                            {
                                if (State == BluetoothLEDeviceState.Discovering && _bluetoothGatt != null)
                                {
                                    if (!_bluetoothGatt.DiscoverServices())
                                    {
                                        Disconnect();
                                        _connectCompletionSource?.TrySetResult(null);
                                    }
                                }
                            }
                        });
                    }
                    else
                    {
                        Disconnect();
                        _connectCompletionSource?.TrySetResult(null);
                    }
                }

                break;

            case ProfileState.Disconnecting:
                break;

            case ProfileState.Disconnected:
                lock (_lock)
                {
                    switch (State)
                    {
                    case BluetoothLEDeviceState.Connecting:
                    case BluetoothLEDeviceState.Discovering:
                        Disconnect();
                        _connectCompletionSource?.TrySetResult(null);
                        break;

                    case BluetoothLEDeviceState.Connected:
                        _writeCompletionSource?.TrySetResult(false);

                        // Copy the _onDeviceDisconnected callback to call it
                        // in case of an unexpected disconnection
                        var onDeviceDisconnected = _onDeviceDisconnected;

                        Disconnect();
                        onDeviceDisconnected?.Invoke(this);
                        break;

                    default:
                        break;
                    }
                }

                break;
            }
        }
Exemplo n.º 19
0
 public override void DiscoverServices()
 {
     gatt.DiscoverServices();
 }
Exemplo n.º 20
0
 private void PlatformDiscoverServices()
 => _gatt.DiscoverServices();
Exemplo n.º 21
0
 public void DiscoverServices()
 {
     gatt.DiscoverServices();
 }
Exemplo n.º 22
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
        {
            System.Diagnostics.Debug.WriteLine($"OnConnectionStateChanged - status: {status}, newState: {newState}");

            switch (newState)
            {
            case ProfileState.Connecting:
                break;

            case ProfileState.Connected:
                lock (_lock)
                {
                    if (State == BluetoothLEDeviceState.Connecting && status == GattStatus.Success)
                    {
                        State = BluetoothLEDeviceState.Discovering;
                        Task.Run(async() =>
                        {
                            await Task.Delay(750);
                            lock (_lock)
                            {
                                if (State == BluetoothLEDeviceState.Discovering && _bluetoothGatt != null)
                                {
                                    if (!_bluetoothGatt.DiscoverServices())
                                    {
                                        Disconnect();
                                        _connectCompletionSource?.SetResult(null);
                                    }
                                }
                            }
                        });
                    }
                    else
                    {
                        Disconnect();
                        _connectCompletionSource?.SetResult(null);
                    }
                }

                break;

            case ProfileState.Disconnecting:
                break;

            case ProfileState.Disconnected:
                lock (_lock)
                {
                    switch (State)
                    {
                    case BluetoothLEDeviceState.Connecting:
                    case BluetoothLEDeviceState.Discovering:
                        Disconnect();
                        _connectCompletionSource?.SetResult(null);
                        break;

                    case BluetoothLEDeviceState.Connected:
                        _writeCompletionSource?.SetResult(false);
                        Disconnect();
                        Disconnected?.Invoke(this, EventArgs.Empty);
                        break;

                    default:
                        break;
                    }
                }

                break;
            }
        }