Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bluetooth Low Energy readRemoteRssi

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

My code is very simple :

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

The callback is never called. Does anyone have any idea ?

Thanks !

like image 526
abecker Avatar asked Oct 14 '13 17:10

abecker


1 Answers

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));
    }
}
like image 79
Samuel C. Avatar answered Oct 15 '22 13:10

Samuel C.