Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Android, can I register for a callback that tells me if Bluetooth is turned on or off?

I need to know inside my app whether bluetooth is on or off. Or if bluetooth was turned on or off e.g. from the OS settings pulldown menu. I thought I could do this in the Activity's onResume(). But it turns out that the activity is not paused when the Android OS's settings "pulldown menu" (the one that is accessed by pulling down with a finger from the top edge of the screen) is opened.

I need to update my UI when bluetooth becomes available or unavailable.

Can I register for a callback (e.g. a BroadcastReceiver) or any other callback to let the system tell me when Bluetooth availability changes?

like image 740
treesAreEverywhere Avatar asked Oct 21 '15 13:10

treesAreEverywhere


2 Answers

You can register the receiver with intent filter:

<receiver
        android:name="com.example.BluetoothReceiver"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>

And this is the BroadcastReceiver:

public class BluetoothReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

            switch (state){
                case BluetoothAdapter.STATE_OFF:
                    //Indicates the local Bluetooth adapter is off.
                break;

                case BluetoothAdapter.STATE_TURNING_ON:
                    //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                break;

                case BluetoothAdapter.STATE_ON:
                    //Indicates the local Bluetooth adapter is on, and ready for use.
                break;

                case BluetoothAdapter.STATE_TURNING_OFF:
                    //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                break;
            }
        }
    }
};

Or if you want to add directly in the activity:

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        //Indicates the local Bluetooth adapter is off.
                        break;

                    case BluetoothAdapter.STATE_TURNING_ON:
                        //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                        break;

                    case BluetoothAdapter.STATE_ON:
                        //Indicates the local Bluetooth adapter is on, and ready for use.
                        break;

                    case BluetoothAdapter.STATE_TURNING_OFF:
                        //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                        break;
                }
            }
        }
    };
}
like image 83
Mattia Maestrini Avatar answered Oct 21 '22 10:10

Mattia Maestrini


This is the intent-filter you need:

<intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
like image 30
ligi Avatar answered Oct 21 '22 12:10

ligi