跳到主要内容

经典蓝牙-蓝牙耳机管理模块

📋 模块概述

蓝牙耳机管理模块提供音频路由控制功能,负责在扬声器、听筒、蓝牙耳机之间切换音频输出。

核心类: android.znhaas.bluetooth.BluetoothAudioRoutingManager

🎯 主要功能

  • ✅ 音频路由切换
  • ✅ 蓝牙SCO连接管理
  • ✅ 自动音频路由选择
  • ✅ 音频设备状态监听

📚 核心API

1. 初始化

val audioRoutingManager = BluetoothAudioRoutingManager(context)

2. 音频路由切换

// 切换到蓝牙耳机
audioRoutingManager.routeAudioToBluetooth()

// 切换到扬声器
audioRoutingManager.routeAudioToSpeaker()

// 切换到听筒
audioRoutingManager.routeAudioToEarpiece()

3. SCO连接管理

// 开启SCO连接
audioRoutingManager.startBluetoothSco()

// 停止SCO连接
audioRoutingManager.stopBluetoothSco()

🔧 使用示例

class AudioActivity : AppCompatActivity() {

private lateinit var audioRoutingManager: BluetoothAudioRoutingManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

audioRoutingManager = BluetoothAudioRoutingManager(this)

// 蓝牙按钮
bluetoothButton.setOnClickListener {
audioRoutingManager.routeAudioToBluetooth()
}

// 扬声器按钮
speakerButton.setOnClickListener {
audioRoutingManager.routeAudioToSpeaker()
}
}
}

⚠️ 注意事项

权限要求

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

配合使用

通常与A2DP和HFP模块配合使用,实现完整的蓝牙音频功能。

🔗 相关资源

  • 源码: app/src/main/java/android/znhaas/bluetooth/BluetoothAudioRoutingManager.kt
  • A2DP模块: 配合实现音乐播放
  • HFP模块: 配合实现通话功能