Exemple #1
0
        public Task <int> GetRssiAsync()
        {
            rssiTCS = new TaskCompletionSource <int>();
            try
            {
                if (_gatt == null)
                {
                    Debug.WriteLine("Connect to Bluetooth Device first");
                    rssiTCS.TrySetException(new Exception("Connect to Bluetooth Device first"));
                }

                var successfullRequest = _gatt.ReadRemoteRssi();

                if (!successfullRequest)
                {
                    rssiTCS.TrySetException(new Exception("Cannot request RSSI"));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                rssiTCS.TrySetException(e);
            }

            return(rssiTCS.Task);
        }
        public override async Task <bool> UpdateRssiAsync()
        {
            if (_gatt == null || _gattCallback == null)
            {
                Trace.Message("You can't read the RSSI value for disconnected devices except on discovery on Android. Device is {0}", State);
                return(false);
            }

            return(await TaskBuilder.FromEvent <bool, EventHandler <RssiReadCallbackEventArgs>, EventHandler>(
                       execute : () => _gatt.ReadRemoteRssi(),
                       getCompleteHandler : (complete, reject) => ((sender, args) =>
            {
                if (args.Error == null)
                {
                    Trace.Message("Read RSSI for {0} {1}: {2}", Id, Name, args.Rssi);
                    Rssi = args.Rssi;
                    complete(true);
                }
                else
                {
                    Trace.Message($"Failed to read RSSI for device {Id}-{Name}. {args.Error.Message}");
                    complete(false);
                }
            }),
                       subscribeComplete : handler => _gattCallback.RemoteRssiRead += handler,
                       unsubscribeComplete : handler => _gattCallback.RemoteRssiRead -= handler,
                       getRejectHandler : reject => ((sender, args) =>
            {
                reject(new Exception($"Device {Name} disconnected while updating rssi."));
            }),
                       subscribeReject : handler => _gattCallback.ConnectionInterrupted += handler,
                       unsubscribeReject : handler => _gattCallback.ConnectionInterrupted -= handler));
        }
Exemple #3
0
        public override async Task <bool> UpdateRssiAsync()
        {
            if (_gatt == null || _gattCallback == null)
            {
                Trace.Message("You can't read the RSSI value for disconnected devices except on discovery on Android. Device is {0}", State);
                return(false);
            }

            var tcs = new TaskCompletionSource <bool>();
            EventHandler <RssiReadCallbackEventArgs> handler = null;

            handler = (sender, args) =>
            {
                if (args.Device.Id != Id)
                {
                    return;
                }

                Trace.Message("Read RSSI async for {0} {1}: {2}", Id, Name, args.Rssi);
                _gattCallback.RemoteRssiRead -= handler;

                var success = args.Error == null;
                if (success)
                {
                    Rssi = args.Rssi;
                }

                tcs.TrySetResult(success);
            };

            _gattCallback.RemoteRssiRead += handler;
            _gatt.ReadRemoteRssi();

            return(await tcs.Task);
        }
        public void RequestRSSIUpdate()
        {
            if (_updatingRSSI)
            {
                return;
            }

            _updatingRSSI = true;
            _bluetoothGatt?.ReadRemoteRssi();
        }
            public override void OnReadRemoteRssi(BluetoothGatt gatt, int rssi, [GeneratedEnum] GattStatus status)
            {
                base.OnReadRemoteRssi(gatt, rssi, status);

                if (status == GattStatus.Success)
                {
                    gatt.ReadRemoteRssi();
                    textRSSI = rssi.ToString();

                    Log.Debug(tag, "Remote RSSI: " + rssi);
                }
            }
Exemple #6
0
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            logger.TraceInformation("Event: Characteristic change");

            if (SampleGattAttributes.PRESSURE_NOTIFICATION_HANDLE.Equals(characteristic.Uuid.ToString()))
            {
                sensorService.BroadcastUpdate(SensorService.ACTION_DATA_AVAILABLE, characteristic);
                logger.TraceInformation("Broadcast Pressure Characteristic");
                logger.TraceInformation(characteristic.Uuid.ToString());
                bool rssiStatus = gatt.ReadRemoteRssi();
            }
        }
Exemple #7
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
        {
            String intentAction;

            logger.TAG = "ServiceBluetothGattCallback";

            if (newState == ProfileState.Connected)
            {
                intentAction = SensorService.ACTION_GATT_CONNECTED;
                sensorService.connectionState = ProfileState.Connected;
                if (SensorDataPage.Instance != null)
                {
                    SensorDataPage.Instance.connectionStopwatch.Stop();
                    SensorDataPage.Instance.SensorView.ConnectionTimeSpan = SensorDataPage.Instance.connectionStopwatch.Elapsed;
                    SensorDataPage.Instance.connectionStopwatch.Reset();
                    SensorDataPage.Instance.firstDataStopwatch.Reset();
                    SensorDataPage.Instance.firstDataStopwatch.Start();
                    SensorDataPage.Instance.SensorView.ConnectionState = "Connected";
                    SensorDataPage.Instance.SensorView.Connected       = true;
                }
                bool rssiStatus = gatt.ReadRemoteRssi();
                sensorService.BroadcastUpdate(intentAction);

                logger.TraceInformation("Connected to GATT server.");
                // Attempts to discover services after successful connection.
                logger.TraceInformation("Attempting to start service discovery:" +
                                        gatt.DiscoverServices());
            }
            else if (newState == ProfileState.Disconnected)
            {
                intentAction = SensorService.ACTION_GATT_DISCONNECTED;
                sensorService.connectionState = ProfileState.Disconnected;

                logger.TraceInformation("Disconnected from GATT server.");
                sensorService.BroadcastUpdate(intentAction);
                sensorService.Close();

                if (SensorDataPage.Instance != null)
                {
                    SensorDataPage.Instance.disconnectionStopwatch.Stop();
                    SensorDataPage.Instance.SensorView.DisconnectionTimeSpan = SensorDataPage.Instance.disconnectionStopwatch.Elapsed;
                    SensorDataPage.Instance.disconnectionStopwatch.Reset();
                    SensorDataPage.Instance.firstData = false;
                    SensorDataPage.Instance.SensorView.ConnectionState = "Disconnected";
                    SensorDataPage.Instance.SensorView.Connected       = false;
                }
                sensorService.HideProgressDialog();
            }
        }
 /// <summary>
 /// Refresh RSSI value from the device.
 /// </summary>
 public void RefreshRssi()
 {
     _gatt?.ReadRemoteRssi();
 }
Exemple #9
0
 private void PlatformReadRSSI()
 => _gatt.ReadRemoteRssi();
Exemple #10
0
 public override void ReadRssi()
 {
     _gattCallback.RemoteRssiRead += OnRemoteRssiRead;
     _gatt.ReadRemoteRssi();
 }