1

i am learning how to use Bluetooth with android, and I registered a BroadCastReceiver for BluetoothAdapter.ACTION_STATE_CHANGED. in the docs, it say

Broadcast Action: The state of the local Bluetooth adapter has been changed. For example, Bluetooth has been turned on or off. Always contains the extra fields EXTRA_STATE and EXTRA_PREVIOUS_STATE
containing the new and old states respectively.

now, how can i use EXTRA_STATE and EXTRA_PREVIOUS_STATE?

rmaik
  • 1,076
  • 3
  • 15
  • 48
  • [Example implicit intent](http://developer.android.com/guide/components/intents-filters.html#ExampleSend) – Onik Aug 23 '15 at 15:18

1 Answers1

1

From here:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                             BluetoothAdapter.ERROR);
        switch (state) {
        case BluetoothAdapter.STATE_OFF:
            //Bluetooth off
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
            //Turning Bluetooth off...
            break;
        case BluetoothAdapter.STATE_ON:
            //Bluetooth on
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
            //Turning Bluetooth on...
            break;
        }
    }
}

};

Community
  • 1
  • 1
Templerschaf
  • 142
  • 1
  • 8
  • your answer is helpful, but in my case, i registered for many actions almost 7 actions, do u think it is better to use on broad cast receiver for all or on a broad cast receiver for each action i wish to listen to? – rmaik Aug 23 '15 at 15:25
  • In most cases one BroadcastReceiver is what you want. If this cleared up your question please make sure to mark this as the answer. – Templerschaf Aug 23 '15 at 18:47