// Import packageimport'package:headset_connection_event/headset_event.dart';
// Instantiate itHeadsetEvent headsetPlugin = newHeadsetEvent();
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;
});
});
Android setup
Update compileSdkVersion to 33 in your build.properties under app directory
Make the following changes to your project's main AndroidManifest.xml file:
<activity...
<!--ADDTHISBELOW "ACTIVITY" -->
android:exported="true"
<!-- ADD THIS "RECEIVER" Element Before </application> --><receiverandroid:name="flutter.moum.headset_event.HeadsetBroadcastReceiver"android:exported="true"><intent-filter><actionandroid:name="android.intent.action.HEADSET_PLUG" /><actionandroid: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: