搜索插件、代码仓与 APP 分类

插件详情v4.1.0

headset_connection_event

用于耳机事件的 Flutter 插件。检测耳机是否已插入和拔出。获取当前耳机状态。

AndroidiOS
近 30 天下载
2190
喜欢数
28
Pub 分数
145

安装插件

flutter pub add headset_connection_event

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 插件,可在 Android 和 iOS 设备上检测耳机是否已插入或拔出。它提供实时更新当前耳机连接状态,使应用程序能够动态响应音频配件的变化。非常适合媒体播放器、语音应用或任何需要了解音频输出配置的应用程序。该插件通过平台通道与原生设备 API 无缝集成。

适合场景

  • 媒体播放器音频路由
  • 语音助手输入切换
  • 实时音频设备监控
  • 基于头戴设备存在的用户体验适应
  • 可访问性功能触发

主要特点

  • 实时耳机插拔检测
  • 跨平台支持(Android 和 iOS)
  • 简单的事件流 API
  • 当前状态获取
  • 轻量且依赖极少

适合对象

  • 以音频为重点的应用程序
  • 需要硬件交互的移动应用
  • 需要响应式UI以应对音频设备更改的应用程序
  • 开发无障碍工具的开发者
  • Flutter 项目同时针对 Android 和 iOS

使用建议

  • Android 上需要适当的权限
  • 仅限头戴式设备连接事件
  • 不支持蓝牙耳机的高级功能,仅支持基本检测
  • 某些平台可能需要后台执行处理
  • 依赖于原生平台功能

原始 README

以下为关联源码仓库的 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;
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。