36

I am trying to make an app that shows a toast when the device's Bluetooth turned on. I wanna do that even when my app is not running. So I should use a broadcast receiver, add some permissions, an intent-filter to android manifest and make a java class but I don't know the details.

What should I do? Which permissions should I use?

Mohammad H
  • 785
  • 2
  • 10
  • 25

3 Answers3

79

AS far as permissions go, to detect the state change of bluetooth you need to add this to your AndroidManifest.xml.

<uses-permission android:name="android.permission.BLUETOOTH" />

An example receiver would look like this, you add this code to where you want to handle the broadcast, for example an activity:

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

                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
    == BluetoothAdapter.STATE_OFF)
    // Bluetooth is disconnected, do handling here
}

}

        };

To use the receiver, you need to register it. Which you can do as follows. I register the receiver in my main activity.

registerReceiver(this, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

You could also decide to add all of it to your AndroidManifest.xml. This way you can make a special class for the receiver and handle it there. No need to register the receiver, just make the class and add the below code to the AndroidManifest

<receiver
        android:name=".packagename.NameOfBroadcastReceiverClass"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>
Hanuman
  • 958
  • 13
  • 35
paNji
  • 806
  • 7
  • 4
23

You have to take following permission.

<uses-permission android:name="android.permission.BLUETOOTH" />

and you have to write this as your intent filter in receiver tag.

<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
Manikanta Ottiprolu
  • 751
  • 1
  • 7
  • 23
8

Unfortunately, for apps targeting api 26 or higher, manifest declared broadcast receivers don't work anymore (reference here: https://developer.android.com/guide/components/broadcast-exceptions), with some exceptions. android.bluetooth.adapter.action.STATE_CHANGED is not in that list.

For bluetooth, you can only listen for changes on:

ACTION_CONNECTION_STATE_CHANGED, ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECTED

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • you listed action connection state changed twice – behelit May 02 '19 at 23:54
  • so what is the workaround? Once i register with ACTION_CONNECTION_STATE_CHANGED , what should the intent return me in onReceive . Or is there another technique to perform the task – Faisal Jul 08 '19 at 11:12
  • You still can register your broadcast receiver at activity or fragment level, remember to unregister the receiver once its job is done – Nicola Gallazzi Jul 09 '19 at 06:38
  • 1
    Thanks. I had problems detecting a change from "Bluetooth on" to "Bluetooth off" due to these changes in API 26. Works like a charm when I register the receiver in MainApplikation.kt: registerBlootoothReceiver(){ val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) bluetoothReceiver = BluetoothReceiver() registerReceiver(bluetoothReceiver, filter) } – Luigi_Papardelle Apr 25 '20 at 10:55