Detecting State Changes Made to the Bluetoothadapter

Detecting state changes made to the BluetoothAdapter?

You will want to register a BroadcastReceiver to listen for any changes in the state of the BluetoothAdapter:

As a private instance variable in your Activity (or in a separate class file... whichever one you prefer):

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:
setButtonText("Bluetooth off");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
setButtonText("Turning Bluetooth off...");
break;
case BluetoothAdapter.STATE_ON:
setButtonText("Bluetooth on");
break;
case BluetoothAdapter.STATE_TURNING_ON:
setButtonText("Turning Bluetooth on...");
break;
}
}
}
};

Note that this assumes that your Activity implements a method setButtonText(String text) that will change the Button's text accordingly.

And then in your Activity, register and unregister the BroadcastReceiver as follows,

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* ... */

// Register for broadcasts on BluetoothAdapter state change
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {
super.onDestroy();

/* ... */

// Unregister broadcast listeners
unregisterReceiver(mReceiver);
}

How to track BluetoothAdapter state changes dynamically in Jetpack Compose?

I was able to solve this just by using mutableStateOf. It doesn't have to be initialized inside of a @Composable function to be reactive, which is what I misunderstood in the first place.

  1. Define a mutableStateOf value and a BroadcastReceiver that will track the state of BluetoothAdapter. When Bluetooth state changes, update the value.
    private var isBluetoothEnabled = mutableStateOf(false)

private val mReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
when (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
BluetoothAdapter.STATE_OFF -> {
isBluetoothEnabled.value = false
Log.i("Bluetooth", "State OFF")
}
BluetoothAdapter.STATE_ON -> {
isBluetoothEnabled.value = true
Log.i("Bluetooth", "State ON")
}
}

}
}
}

  1. Inside of a @Composable function, use this value as usual:
@Composable
private fun MainScreen(
isBluetoothEnabled: Boolean
) {
if (isBluetoothEnabled) {
// Display some UI
} else {
// Display different UI
}
}

And that's it!

How to detect Bluetooth state change using a broadcast receiver?

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>

Detect Enable/Disable Bluetooth

To detect the state change of bluetooth you need to add following permission to your AndroidManifest.xml.

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

Use a Local broadcast preferably. You do not need to register it in Manifest . register it at runtime where you need it.(If need throughout the app then register it in Manifest)

private final BroadcastReceiver bStateReceiver = 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
}
}
}

};

Runtime register:

LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

Runtime unregister : Don't forget to unregister the broadcast.

LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver);

Static Register:

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

Ionic - How To Check Bluetooth State Change

cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){
if (enabled) {
// bluetooth already on
} else {
// bluetooth off
}
}, function(error){
console.error("The following error occurred: "+error);
});

cordova.plugins.diagnostic.setBluetoothState(function(){
console.log("Bluetooth was enabled");
}, function(error){
console.error("The following error occurred: "+error);
}, true);

html makeup

<button class="button" ng-disabled="!bluetoothIsEnabled" on-tap="yourFunction($event)"></button>

bluetooth state listen

cordova.plugins.diagnostic.registerBluetoothStateChangeHandler(function(state){
// "unknown", "resetting", "unsupported", "unauthorized", "powered_off", "powered_on"
if (state == "powered_on") {
$scope.bluetoothIsEnabled = true;
} else {
$scope.bluetoothIsEnabled = false;
}
});


Related Topics



Leave a reply



Submit