android - Android 上的低功耗蓝牙 : Get event when RSSI changes

标签 android bluetooth bluetooth-lowenergy

我可以通过 startLeScan 或通过新的 getBluetoothLeScanner 扫描蓝牙 LE 设备,效果很好。然而,即使它继续扫描,它也不会两次检测到同一设备。这很不幸,因为我希望在信标的 RSSI 发生变化时接收事件。 Android 支持吗?

最佳答案

实现 BluetoothGattCallback 并重写 onReadRemoteRssi 方法。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                broadcastUpdate(intentAction);

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                broadcastUpdate(intentAction);
            }
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
            // use rssi value here
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    };

关于android - Android 上的低功耗蓝牙 : Get event when RSSI changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29756571/

相关文章:

ios - 葡萄糖概况 - IOS

android - 如何通过Android 4.3将文本数据发送到CC2541 key 卡?

iOS蓝牙后台模式

Android 计费 v3 - BillingClientImpl.java 中的 NullPointerException :668

Android BLE - 如何检测心率传感器

ios - BLE 广告数据中有什么?

android - 如何识别 ArrayAdapter<string> 中的现有项目

bluetooth - FlutterBlue 特性

java - LibGDX - 在对象的前面或后面显示玩家

java - 如何使用 Android 中的加速度计值计算特定轴的旋转速率