Android Bluetooth с низким энергопотреблением readRemoteRssi

Я не могу понять, как заставить работать обратный вызов 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

Обратный вызов никогда не вызывается. У кого-нибудь есть идеи?

Спасибо !


person abecker    schedule 14.10.2013    source источник
comment
Вы решаете эту проблему?   -  person phcaze    schedule 21.01.2014


Ответы (3)


Поместите readRemoteRssi() в функцию обратного вызова onConnectionStateChange() функции 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());
        }
    }
};

А также поставить 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));
    }
}
person Samuel C.    schedule 27.11.2013
comment
Следуя приведенному выше решению, вы все еще не получаете обратный вызов onreadRemoteRssi для получения значения rssi. Пожалуйста, предложите решение. - person Aman; 10.07.2017

http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#readRemoteRssi()

Асинхронный вызов для начала чтения уровня сигнала.

http://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onReadRemoteRssi(android.bluetooth.BluetoothGatt,%20int,%20int)

Обратный вызов после завершения чтения.

Необходимо подключиться перед чтением

ссылка здесь Постоянный уровень сигнала Bluetooth LE на Android

person Wun    schedule 16.10.2013

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
BluetoothGatt bluetoothGatt = getBluetoothGatt(device);

if (bluetoothGatt == null) {
    return false;
}
boolean rdRemoteRssi = bluetoothGatt.readRemoteRssi();
Log.d(FTAG, "BluetoothGatt readRemoteRssi : " + rdRemoteRssi);
return true;

Он вызовет обратный вызов onReadRemoteRssi. Перед вызовом этого API необходимо подключиться.

person vineela    schedule 17.10.2013
comment
что это за метод getBluetoothGatt(), в каком классе он есть? - person AAnkit; 25.10.2013