Android蓝牙低功耗readRemoteRssi

abe*_*ker 4 android bluetooth-lowenergy

我无法弄清楚如何获得'onReadRemoteRssi'回调工作.

我的代码非常简单:

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
    BluetoothGatt gatt;

    mBluetoothAdapter.startLeScan(new LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] record) {
            gatt = device.connectGatt(getApplicationContext(), false, new BluetoothGattCallback() {
                @Override
                public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
                    super.onReadRemoteRssi(gatt, rssi, status);
                    Log.d(TAG, "rssi is : " + rssi);
                }
            });
        }
    });

    gatt.readRemoteRssi(); //returns true
Run Code Online (Sandbox Code Playgroud)

永远不会调用回调.有谁有想法吗 ?

谢谢 !

小智 6

将readRemoteRssi()放在BluetoothGattCallback的回调onConnectionStateChange()中.

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            boolean rssiStatus = mBluetoothGatt.readRemoteRssi();
            broadcastUpdate(intentAction);
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

并且还将onReadRemoteRssi放在BluetoothGattCallback函数中

@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, String.format("BluetoothGatt ReadRssi[%d]", rssi));
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 按照上述解决方案仍然无法获得 onreadRemoteRssi 回调来获取 rssi 值。请提出解决方案。 (2认同)