1

I am working with BLE. I am trying to use intend to send rssi value from one activity to another.

Activity 1, when ever there is a change in the rssi signal, I send the values over, shown below:

    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            //first logcat
            Log.d("BLE_Strength_connected", String.format("BluetoothGatt ReadRssi[%d]", rssi));
            Intent i = new Intent(Activity1.this, Activity2.class);
            i.putExtra("key",rssi);
            startActivity(i);              
        }
    }
};

Activity 2: In the oncreate() method, I started a handler, and it reads the data every second, I have the following:

    mIntent = getIntent();
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                mHandler.postDelayed(this, 1000);
                //  if (mIntent != null) {
                int value = mIntent.getIntExtra("key", 0);
                Log.d("retrieve_RSSI", "value:" + value);
                //The key argument here must match that used in the other activity
                //  }
            }catch (Exception e) {

            }
        }
    }, 1000);

My logcat on Activity 1 is constantly prints the rssi signal, but the logcat on activity 2 only prints zero. I am wondering why I can't receive any values in activity 2?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
BOB
  • 151
  • 3
  • 13
  • is getExtras() empty? Set a breakpoint and check what gets returned from mIntent.getExtras() – Jon Apr 29 '18 at 07:49
  • by default it returns zero, so it is empty. Also, i tried to use this method here (https://stackoverflow.com/a/7325248/5186878) and my extra's variable is always null. @Jon – BOB Apr 29 '18 at 08:10

1 Answers1

0

Your mIntent variable will always refer to the same Intent object, so when you extract the "key" extra from it, you will of course always get the same value.

You must make sure you handle new incoming intents. See for example http://m.helloandroid.com/tutorials/communicating-between-running-activities how to do that.

Emil
  • 16,784
  • 2
  • 41
  • 52