简体   繁体   中英

How to pull out data from overriding class created on the go JAVA

I am sorry for bad English. I'm using Android Studio Chipmunk to figure out how to pull out int rssi from onReadRemoteRssi method. I am getting the value of rssi but it is only output on console of Android Studio. How can I take this value out of the method and into my main code where I am running gatt.readremoterssi().

BluetoothGatt gatt = c.connectGatt(getApplicationContext(), false, new BluetoothGattCallback() {
            @Override
            public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
                super.onReadRemoteRssi(gatt, rssi, status);
                d("BSSI", "rssi is : " + rssi);
            }
        });
        gatt.readRemoteRssi();

Here is the console:

D/BSSI: rssi is : -56

First create a variable name rssi in your Main Activity class and replace your code with this

BluetoothGattCallback temp =  new BluetoothGattCallback() {
    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        super.onReadRemoteRssi(gatt, rssi, status);
        d("BSSI", "rssi is : " + rssi);
        setRssi(rssi);
    }
};

BluetoothGatt gatt = c.connectGatt(getApplicationContext(), false,temp);

And create a method in Main Activity

public void setRsi(int rsi) {
        this.rsi = rsi;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM