Android Airplane Mode, Bluetooth BroadcastReceiver example Kotlin

06-Dec-2024

Learn how to detect changes in Airplane mode and Bluetooth settings using BroadcastReceiver in Android Studio with Kotlin


1/ Step : Create A New Project  add the code in  AndroidManiFest 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<receiver android:name=".receivers.MyBroadcastReceiver"
android:exported="true"
tools:ignore="MissingClass,WrongManifestParent">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
</intent-filter>
</receiver>

</activity>


</application>

</manifest>



2/Step :  Create A New Class BroadcastReceiver 



package com.example.app.receivers

import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_AIRPLANE_MODE_CHANGED -> {
val isAirplaneModeOn = intent.getBooleanExtra("state", false)
Log.d("MyBroadcastReceiver", "Airplane Mode is ${if (isAirplaneModeOn) "ON" else "OFF"}")
}

BluetoothAdapter.ACTION_STATE_CHANGED -> {
val bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)
when (bluetoothState) {
BluetoothAdapter.STATE_OFF -> Log.d("MyBroadcastReceiver", "Bluetooth is OFF")
BluetoothAdapter.STATE_ON -> Log.d("MyBroadcastReceiver", "Bluetooth is ON")
}
}
}
}
}




3/Step :  Add the code in MainActivity


import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
private lateinit var myReceiver: BroadcastReceiver

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Create the BroadcastReceiver
myReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_AIRPLANE_MODE_CHANGED -> {
val isAirplaneModeOn = intent.getBooleanExtra("state", false)
Log.d("MainActivity", "Airplane Mode is ${if (isAirplaneModeOn) "ON" else "OFF"}")
}

BluetoothAdapter.ACTION_STATE_CHANGED -> {
val bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)
when (bluetoothState) {
BluetoothAdapter.STATE_OFF -> Log.d("MainActivity", "Bluetooth is OFF")
BluetoothAdapter.STATE_ON -> Log.d("MainActivity", "Bluetooth is ON")
}
}
}
}
}

// Register the receiver
val filter = IntentFilter().apply {
addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED)
addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
}
registerReceiver(myReceiver, filter)
}

override fun onDestroy() {
super.onDestroy()
// Unregister the receiver
unregisterReceiver(myReceiver)
}
}




Comments