headset_connection_event
헤드셋 이벤트용 플러터 플러그인. 헤드셋이 꽂혔는지, 빠졌는지 감지합니다. 현재 헤드셋 상태를 가져옵니다.
헤드셋의 연결 및 분리 이벤트를 캡처하는 데 사용할 수 있는 Flutter 라이브러리
{"sdk":"flutter"}^5.0.0{"sdk":"flutter"}^5.0.0아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
A Flutter plugin to get a headset event.
This is a clone of headset_event, but with fix on swift errors and bluetooth connection events for Android and iOS
Migrated to AndroidX
| Platform | Physical Headset | Bluetooth |
|---|---|---|
| iOS | ✅ | ✅ |
| Android | ✅ | ✅ |
To use this plugin, add headset_connection_event as a dependency in your pubspec.yaml file.
// Import package
import 'package:headset_connection_event/headset_event.dart';
// Instantiate it
HeadsetEvent headsetPlugin = new HeadsetEvent();
HeadsetState headsetEvent;
/// if headset is plugged
headsetPlugin.getCurrentState.then((_val){
setState(() {
headsetEvent = _val;
});
});
/// Detect the moment headset is plugged or unplugged
headsetPlugin.setListener((_val) {
setState(() {
headsetEvent = _val;
});
});
Update compileSdkVersion to 33 in your build.properties under app directory
Make the following changes to your project's main AndroidManifest.xml file:
<activity...
<!-- ADD THIS BELOW "ACTIVITY" -->
android:exported="true"
<!-- ADD THIS "RECEIVER" Element Before </application> -->
<receiver android:name="flutter.moum.headset_event.HeadsetBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
Please see example project AndroidManifest.xml for more information.
Android 12 requires bluetoothConnect permission. You may request it using the permission_handler:
Add permission in your manifest
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
import 'package:permission_handler/permission_handler.dart';
Future<bool> requestPermission() async {
if (!Platform.isAndroid) return true;
return Permission.bluetoothConnect.request().isGranted;
}