Android Bluetooth Low Energy readRemoteRssi

前端 未结 3 839
心在旅途
心在旅途 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:38

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

    Async call to start reading signal strength.

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

    Callback after the read finishes.

    Need to connect before read

    reference here Continual Bluetooth LE Signal Strength on Android

    0 讨论(0)
  • 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));
        }
    }
    
    0 讨论(0)
  • 2021-02-10 02:46
    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;
    

    It will call onReadRemoteRssi call back.Need to connect before call this API.

    0 讨论(0)
提交回复
热议问题