Android Bluetooth Low Energy readRemoteRssi

前端 未结 3 840
心在旅途
心在旅途 2021-02-10 02:22

I can\'t figure out how to get the \'onReadRemoteRssi\' callback work.

My code is very simple :

final BluetoothManager bluetoothManager = (BluetoothManag         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-10 02:44

    Put readRemoteRssi() in the callback onConnectionStateChange() of BluetoothGattCallback.

    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());
            }
        }
    };
    

    And also put the onReadRemoteRssi in BluetoothGattCallback function   

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

提交回复
热议问题