diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 941f7665f..6a87534b7 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -67,29 +67,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
= Build.VERSION_CODES.S) {
+ ContextCompat.checkSelfPermission(
+ context,
+ Manifest.permission.BLUETOOTH_CONNECT
+ ) == PackageManager.PERMISSION_GRANTED
+ } else {
+ true
+ }
+ }
+
+ /**
+ * 检查蓝牙是否启用
+ * @return 蓝牙是否启用
+ */
+ fun isBluetoothEnabled(): Boolean {
+ try {
+ val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
+ if (bluetoothAdapter == null) {
+ return false
+ }
+
+ return bluetoothAdapter.isEnabled
+ } catch (e: Exception) {
+ Log.e(TAG, "检查蓝牙状态失败", e)
+ return false
+ }
+ }
+
+ /**
+ * 获取指定配置文件的已连接蓝牙设备
+ * @param profile 蓝牙配置文件类型,如 BluetoothProfile.A2DP 或 BluetoothProfile.HEADSET
+ * @param callback 回调接口,返回设备列表或错误信息
+ */
+ fun getConnectedBluetoothDevices(profile: Int, callback: BluetoothDevicesCallback) {
+ val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
+ if (bluetoothAdapter == null) {
+ callback.onError("NO_ADAPTER", "没有找到蓝牙适配器")
+ return
+ }
+
+ // 检查蓝牙权限
+ if (!hasBluetoothConnectPermission()) {
+ callback.onError("PERMISSION_DENIED", "缺少蓝牙连接权限")
+ return
+ }
+
+ try {
+ // 检查是否已经有缓存的代理对象
+ val cachedProxy = when (profile) {
+ BluetoothProfile.A2DP -> a2dpProxy
+ BluetoothProfile.HEADSET -> headsetProxy
+ else -> null
+ }
+
+ if (cachedProxy != null) {
+ // 使用已缓存的代理对象
+ processDeviceList(cachedProxy, profile, callback)
+ return
+ }
+
+ // 使用原子布尔值确保回调只被调用一次
+ val callbackCalled = AtomicBoolean(false)
+
+ // 使用Handler处理超时
+ val handler = Handler(Looper.getMainLooper())
+ val timeoutRunnable = Runnable {
+ if (callbackCalled.compareAndSet(false, true)) {
+ callback.onError("TIMEOUT", "获取蓝牙设备超时")
+ }
+ }
+
+ // 设置3秒超时
+ handler.postDelayed(timeoutRunnable, 3000)
+
+ // 通过 getProfileProxy 获取指定 Profile (A2DP/HEADSET) 的连接状态
+ bluetoothAdapter.getProfileProxy(context,
+ object : BluetoothProfile.ServiceListener {
+ override fun onServiceConnected(profileId: Int, proxy: BluetoothProfile) {
+ // 先移除超时处理
+ handler.removeCallbacks(timeoutRunnable)
+
+ if (profileId == profile) {
+ // 缓存代理对象
+ when (profileId) {
+ BluetoothProfile.A2DP -> a2dpProxy = proxy
+ BluetoothProfile.HEADSET -> headsetProxy = proxy
+ }
+
+ if (callbackCalled.compareAndSet(false, true)) {
+ try {
+ processDeviceList(proxy, profileId, callback)
+ } catch (e: Exception) {
+ callback.onError("DEVICE_ERROR", "处理设备信息时出错")
+ }
+ }
+ }
+ }
+
+ override fun onServiceDisconnected(profileId: Int) {
+ // 清除对应的缓存代理
+ when (profileId) {
+ BluetoothProfile.A2DP -> a2dpProxy = null
+ BluetoothProfile.HEADSET -> headsetProxy = null
+ }
+ }
+ }, profile
+ )
+ } catch (e: Exception) {
+ callback.onError("BLUETOOTH_ERROR", "获取蓝牙设备失败")
+ }
+ }
+
+ /**
+ * 处理设备列表
+ */
+ private fun processDeviceList(proxy: BluetoothProfile, profileId: Int, callback: BluetoothDevicesCallback) {
+ // 获取已连接设备
+ val connectedDevices = proxy.connectedDevices
+
+ // 转换为设备信息列表
+ val devicesList = connectedDevices.map { device ->
+ mapOf(
+ "name" to (device.name ?: "未知设备"),
+ "address" to device.address
+ )
+ }
+
+ // 返回设备列表(不关闭代理)
+ callback.onSuccess(devicesList)
+ }
+
+ /**
+ * 检查是否有蓝牙权限
+ */
+ fun hasBluetoothPermission(): Boolean {
+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+ ContextCompat.checkSelfPermission(
+ context,
+ Manifest.permission.BLUETOOTH_CONNECT
+ ) == PackageManager.PERMISSION_GRANTED
+ } else {
+ ContextCompat.checkSelfPermission(
+ context,
+ Manifest.permission.BLUETOOTH
+ ) == PackageManager.PERMISSION_GRANTED
+ }
+ }
+
+
+ /**
+ * 开始监听蓝牙状态变化
+ * @param callback 蓝牙状态回调接口
+ */
+ fun startBluetoothStateMonitoring(callback: BluetoothStateCallback) {
+ this.stateCallback = callback
+
+ // 注册蓝牙广播接收器(合并状态变化和设备连接/断开监听)
+ if (bluetoothReceiver == null) {
+ bluetoothReceiver = object : BroadcastReceiver() {
+ override fun onReceive(context: Context?, intent: Intent?) {
+ when (intent?.action) {
+ // 监听蓝牙状态变化
+ BluetoothAdapter.ACTION_STATE_CHANGED -> {
+ val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
+ when (state) {
+ BluetoothAdapter.STATE_ON -> {
+ stateCallback?.onBluetoothStateChanged(BluetoothState.ON)
+ }
+ BluetoothAdapter.STATE_OFF -> {
+ stateCallback?.onBluetoothStateChanged(BluetoothState.OFF)
+ }
+ BluetoothAdapter.STATE_TURNING_ON -> {
+ stateCallback?.onBluetoothStateChanged(BluetoothState.TURNING_ON)
+ }
+ BluetoothAdapter.STATE_TURNING_OFF -> {
+ stateCallback?.onBluetoothStateChanged(BluetoothState.TURNING_OFF)
+ }
+ }
+ }
+ // 监听设备连接
+ BluetoothDevice.ACTION_ACL_CONNECTED -> {
+ val device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
+ } else {
+ @Suppress("DEPRECATION")
+ intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
+ }
+
+ if (device != null) {
+ stateCallback?.onDeviceConnected(
+ mapOf(
+ "name" to (device.name ?: "未知设备"),
+ "address" to device.address
+ )
+ )
+ }
+ }
+ // 监听设备断开
+ BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
+ val device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
+ } else {
+ @Suppress("DEPRECATION")
+ intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
+ }
+
+ if (device != null) {
+ stateCallback?.onDeviceDisconnected(
+ mapOf(
+ "name" to (device.name ?: "未知设备"),
+ "address" to device.address
+ )
+ )
+ }
+ }
+ }
+ }
+ }
+
+ // 创建IntentFilter并添加所有需要监听的动作
+ val filter = IntentFilter().apply {
+ addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
+ addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
+ addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
+ }
+
+ context.registerReceiver(bluetoothReceiver, filter)
+ // 立即返回当前状态
+ reportCurrentBluetoothState()
+ }
+ }
+
+ /**
+ * 报告当前蓝牙状态
+ */
+ private fun reportCurrentBluetoothState() {
+ val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
+ if (bluetoothAdapter != null) {
+ val state = when (bluetoothAdapter.state) {
+ BluetoothAdapter.STATE_ON -> BluetoothState.ON
+ BluetoothAdapter.STATE_OFF -> BluetoothState.OFF
+ BluetoothAdapter.STATE_TURNING_ON -> BluetoothState.TURNING_ON
+ BluetoothAdapter.STATE_TURNING_OFF -> BluetoothState.TURNING_OFF
+ else -> BluetoothState.UNKNOWN
+ }
+ stateCallback?.onBluetoothStateChanged(state)
+ }
+ }
+
+ /**
+ * 停止监听蓝牙状态变化
+ */
+ fun stopBluetoothStateMonitoring() {
+ // 注销蓝牙广播接收器
+ if (bluetoothReceiver != null) {
+ try {
+ context.unregisterReceiver(bluetoothReceiver)
+ } catch (e: Exception) {
+ Log.e(TAG, "注销蓝牙接收器失败", e)
+ }
+ bluetoothReceiver = null
+ }
+
+ // 清除回调
+ stateCallback = null
+ }
+
+ /**
+ * 释放资源
+ */
+ fun dispose() {
+ val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() ?: return
+
+ // 停止蓝牙状态监听
+ stopBluetoothStateMonitoring()
+
+ // 释放A2DP代理
+ a2dpProxy?.let {
+ try {
+ bluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, it)
+ } catch (e: Exception) {
+ Log.e(TAG, "关闭A2DP代理失败", e)
+ }
+ a2dpProxy = null
+ }
+
+ // 释放HEADSET代理
+ headsetProxy?.let {
+ try {
+ bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, it)
+ } catch (e: Exception) {
+ Log.e(TAG, "关闭HEADSET代理失败", e)
+ }
+ headsetProxy = null
+ }
+ }
+
+ /**
+ * 蓝牙设备回调接口
+ */
+ interface BluetoothDevicesCallback {
+ fun onSuccess(devices: List