안드로이드 헤드셋 연결 상태 감지하기
이 예제는 BroadcastReceiver와 시스템 브로드캐스트인 ACTION_HEADSET_PLUG를 활용해 안드로이드 기기에 유선 헤드셋(이어폰·마이크)이 연결되어 있는지 확인하는 방법을 보여줍니다.
헤드셋이 연결되거나 분리될 때마다 시스템은 ACTION_HEADSET_PLUG 브로드캐스트를 발생시키며, 함께 전달되는 state 값(0 = 분리됨, 1 = 연결됨)으로 현재 상태를 판별할 수 있습니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File ⇒ New Project로 이동한 후, 필요한 정보를 모두 입력하여 새 프로젝트를 만듭니다.
2단계 — 레이아웃 작성 (res/layout/activity_main.xml)
activity_main.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detecting headphones plug In" />
</LinearLayout>
3단계 — 메인 액티비티 구현 (src/MainActivity.java)
MainActivity.java에 브로드캐스트 리시버를 등록하여 헤드셋 연결·분리 이벤트를 수신합니다. 리시버가 등록되는 순간 현재 연결 상태도 함께 전달받을 수 있습니다.
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
boolean Microphone_Plugged_in = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
int iii;
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
iii = intent.getIntExtra("state", -1);
if (iii == 0) {
Microphone_Plugged_in = false;
Toast.makeText(getApplicationContext(), "microphone not plugged in", Toast.LENGTH_LONG).show();
}
if (iii == 1) {
Microphone_Plugged_in = true;
Toast.makeText(getApplicationContext(), "microphone plugged in",
Toast.LENGTH_LONG).show();
}
}
}
};
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(broadcastReceiver, receiverFilter);
}
}
4단계 — 매니페스트 설정 (androidManifest.xml)
AndroidManifest.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 스마트폰이 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 열고 툴바의 Run
아이콘을 클릭하세요. 목록에서 본인의 모바일 기기를 선택해 앱을 실행하면, 기기 화면에 아래와 같은 기본 화면이 표시됩니다.

참고 사항
- 매니페스트 등록 불가:
ACTION_HEADSET_PLUG는 매니페스트에 선언된 컴포넌트로는 수신할 수 없으며, 반드시registerReceiver()로 동적 등록해야 합니다. - Android 13(API 33) 이상: 타깃 SDK 버전이 33 이상이라면 리시버 등록 시 내보내기 플래그 지정이 필요합니다. 예:
ContextCompat.registerReceiver(this, broadcastReceiver, receiverFilter, ContextCompat.RECEIVER_NOT_EXPORTED); - 블루투스 헤드셋: 이 방법은 유선 헤드셋만 감지합니다. 블루투스 오디오 기기의 연결 상태는
AudioManager또는BluetoothProfile을 활용해 확인해야 합니다.