java – BroadcastReceiver does not detect enabled Bluetooth when turned on from the notification area

Question:

There is a code:

brEnableBt = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
            int scanStatus = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, 0);
            Log.d(TAG, String.valueOf(scanStatus));
            if (scanStatus == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
                checkBluetooth.setChecked(true);
                checkBluetooth.setClickable(false);
            } else {
                checkBluetooth.setChecked(false);
                checkBluetooth.setClickable(true);
            }
        }
    }
};
IntentFilter edBtBrIntent = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
registerReceiver(brEnableBt, edBtBrIntent);

It should detect the "on, visible" Bluetooth state.
If you turn on Bluetooth through the settings, then everything is fine.
But if I turn on Bluetooth from the notification area, then nothing happens. Nothing is written to the logs. But when disabled, the status "disabled" is recorded (20). What to do about it?

Answer:

After a short googling, I came up with the following solution:

brEnableBt = 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, 0);
            Log.d(TAG, String.valueOf(state));
            if (state == BluetoothAdapter.STATE_ON) {
                adapter = BluetoothAdapter.getDefaultAdapter();
                if (adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
                    checkBluetooth.setChecked(true);
                    checkBluetooth.setClickable(false);
                } else {
                    checkBluetooth.setChecked(false);
                    checkBluetooth.setClickable(true);
                }
                //Toast.makeText(context, "Bluetooth is enabled", Toast.LENGTH_LONG).show();
            } else {
                checkBluetooth.setChecked(false);
                checkBluetooth.setClickable(true);
                //Toast.makeText(context, "Bluetooth is disabled", Toast.LENGTH_LONG).show();
            }
        }
    }
};

The solution seems to work. But there is one but. When the visibility state of an already switched on bluetooth changes, the checkbox mark does not change again.
UPD: It's an interesting thing. If you use the code from the question, then it will work just when the visibility of the included bluetooth changes. If you use the code from my answer, then it will determine the visibility when the bluetooth is turned on.
Therefore, these two solutions must be combined.
UPD2: Understood. I tried different methods, it worked like this: register two different receivers to turn on and change the status. True, it seems that they will eat the RAM for a sweet soul …

Scroll to Top