0

In my app, I'm trying to react to the situation when my device's Bluetooth (BT) is disabled. The ACTION_BOND_STATE_CHANGED (check this and this answers ) though seems to be reported when another device connected to my phone is disconnected or turned off, at least in my case. If I register ACTION_ACL_DISCONNECTED and then turn my devices BT off, then the onReceive() is called.

init {
    fetchDevices()

    val mBTPairedReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            val device = intent.getParcelableExtra<Parcelable>(BluetoothDevice.EXTRA_DEVICE)
            val previousStates = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothAdapter.ERROR)
            val states = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothAdapter.ERROR)

            when (action) {
                BluetoothDevice.ACTION_FOUND -> if (device != null) {
                    fetchDevices()
                    deviceListener?.let { it(devices) }
                }
                BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {

                    when (states) {
                        BluetoothAdapter.STATE_TURNING_OFF -> {
                            Log.d("MESSAGE", "BT is turning OFF")
                        }
                        BluetoothAdapter.STATE_ON -> Log.d("MESSAGE", "BT is ON now")

                    }
                }
            }

        }
    }


    application.registerReceiver(mBTPairedReceiver, IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))

}

I want to be informed if BT is going to be turned off/disabled on my device so that I can send a final message to another device I am connected to before losing the connection .

msc87
  • 943
  • 3
  • 17
  • 39
  • You can't. If the user disables the bluetooth, then the bluetooth shall be disabled. Otherwise one could easily mess up with an Android device, like preventing the user from uninstalling an app or preventing him from disabling his internet connection etc. – Omar Aflak May 21 '18 at 12:08
  • I am not sure if I understood what you meant. I just want to send a final message to a device I am paired to before my device 's bluetooth is turned off. I am not trying to change any other device's state. – msc87 May 21 '18 at 12:26

1 Answers1

0

User BroadCast Receiver

private final BroadcastReceiver btReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(btAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, btAdapter.ERROR);
                switch(state){
                    case BluetoothAdapter.STATE_OFF:
                        Log.d(TAG, "btReceiver: STATE OFF");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        Log.d(TAG, "btReceiver: STATE TURNING OFF");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        Log.d(TAG, "btReceiver: STATE ON");
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        Log.d(TAG, "btReceiver: STATE TURNING ON");
                        break;
                }
            }
        }
    };
Balu Sangem
  • 712
  • 4
  • 16
  • Hey, thanks for the answer but if you look at the answers mentioned in my question I have already tried your solution... it does not work. I don't know why though!!!! – msc87 May 21 '18 at 12:04
  • You are listening for "ACTION_BOND_STATE_CHANGED", instead listen for "ACTION_STATE_CHANGED". – Balu Sangem May 21 '18 at 12:09
  • well, there is no ACTION_STATE_CHANGE anymore I assume – msc87 May 21 '18 at 12:11
  • where did you see that?It is available . https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_STATE_CHANGED – Balu Sangem May 21 '18 at 12:12
  • In the latest version of android.bluetooth library :) ... I can't seem to be able to find the ACTION_STATE_CHANGED constant. – msc87 May 21 '18 at 12:18
  • 1
    @msc87 make sure you are searching in `BluetoothAdapter` class, not `BluetoothDevice` – Vasyl Glodan May 21 '18 at 12:27
  • 1
    @msc87 can you try to just copy-paste this into your project https://gist.github.com/glodanif/5d0a505e5e76ff845a3092c908801a11 It works perfectly for me. – Vasyl Glodan May 21 '18 at 12:40
  • @BaluSangem: you were right, I was using BluetoothDevice constants with the BluetoothAdaptor, that's why it did not work...If you make it clear in you answer then I can accept it as the correct answer. – msc87 May 22 '18 at 09:31