Open In App

How to Check if AUX is Plugged or Not in Android Programmatically?

Last Updated : 27 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

AUX (Auxiliary) port is a common port observed in most of the daily use electronic devices. It is a standard communication port for transferring audio signals from one device to another. We mostly observe them on mobile phones, computers, television sets, speakers, headphones, and headsets. While playing music, if we establish a wired cable between our phone and external speaker, like headphones, the music application must quickly disconnect from its previous speakers and start playing in the headphones. Here it becomes necessary for the device to know if an AUX is plugged in.

So in this article, we will show you how you could programmatically check if an AUX cable is plugged in or not in Android.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring a broadcast receiver to 
        // check if headset plug is plugged in
        val mBroadcastReceiver = object : BroadcastReceiver(){
            override fun onReceive(context: Context?, intent: Intent) {
                val mAction = intent.action
                if (Intent.ACTION_HEADSET_PLUG == mAction) {
                    if (intent.getIntExtra("state", -1) == 0) {
                        Toast.makeText(applicationContext, "AUX not plugged in", Toast.LENGTH_LONG).show()
                    }
                    if (intent.getIntExtra("state", -1) == 1) {
                        Toast.makeText(applicationContext, "AUX plugged in", Toast.LENGTH_LONG).show()
                    }
                }
            }
        }
          
        // Declaring a receiver filter for registering
        val mReceiverFilter = IntentFilter(Intent.ACTION_HEADSET_PLUG)
  
        // Registering both broadcast receiver with receiver filter
        registerReceiver(mBroadcastReceiver, mReceiverFilter)
    }
}


Output:

Initially, no AUX cable was plugged in. So we got no aux plugged in the message. Then we manually plugged in the AUX cable and we got an affirmative toast message.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads