FLUTTER ECOSYSTEM

rafdls/headset_connection_event

Flutter 库,用于捕获耳机的连接和断开事件

耳机连接事件 项目封面
Stars
8
Forks
29
最近推送(UTC)
2026年2月10日
项目状态
未归档
Rafael Delos Santos GitHub avatar
GITHUB User

Rafael Delos Santos ↗

Building apps @rafaelatdoshii, @raf-tyro @tmc-apps

Commonwealth Bank of AustraliaSydney, Australia官方网站 ↗
语言JavaDartSwiftRubyPythonObjective-C

此仓库发布的插件

使用的依赖

依赖清单 4 项
  • flutter{"sdk":"flutter"}
  • flutter_lints开发依赖^5.0.0
  • flutter_test开发依赖{"sdk":"flutter"}
  • mockito开发依赖^5.0.0

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

展开 / 收起项目 README

Headset Connection Event Flutter Plugin

A Flutter plugin to get a headset event.

Flutter

This is a clone of headset_event, but with fix on swift errors and bluetooth connection events for Android and iOS

Migrated to AndroidX

Current Status

Platform Physical Headset Bluetooth
iOS
Android

Usage

To use this plugin, add headset_connection_event as a dependency in your pubspec.yaml file.

Example
    // 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;
      });
    });
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...
 <!-- 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;
}