Browse Source

修复android低版本的连接校验问题

weicu
liwei1dao 12 months ago
parent
commit
549ba8a10e
  1. 40
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleMediaService.kt

40
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleMediaService.kt

@ -247,11 +247,18 @@ private var companionManager: BleCompanionManager? = null
* 检查已连接的蓝牙设备 * 检查已连接的蓝牙设备
* 在服务启动或蓝牙开启时调用,检查是否有目标设备已经连接 * 在服务启动或蓝牙开启时调用,检查是否有目标设备已经连接
*/ */
// 方法:checkAlreadyConnectedDevices
private fun checkAlreadyConnectedDevices() { private fun checkAlreadyConnectedDevices() {
try { try {
bluetoothAdapter?.let { adapter -> bluetoothAdapter?.let { adapter ->
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) // 新增:仅在 API>=31 时检查 BLUETOOTH_CONNECT;Android 11 及以下跳过
== PackageManager.PERMISSION_GRANTED) { val needBtConnectPermission = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S
if (needBtConnectPermission &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT)
!= PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "缺少蓝牙连接权限(API>=31),无法检查已连接设备")
return
}
val bondedDevices = adapter.bondedDevices val bondedDevices = adapter.bondedDevices
Log.i(TAG, "检查已绑定的设备数量: ${bondedDevices?.size ?: 0}") Log.i(TAG, "检查已绑定的设备数量: ${bondedDevices?.size ?: 0}")
@ -269,9 +276,8 @@ private var companionManager: BleCompanionManager? = null
// 实际连接状态需要通过广播接收器监听 // 实际连接状态需要通过广播接收器监听
} }
} }
} else { } ?: run {
Log.w(TAG, "缺少蓝牙连接权限,无法检查已连接设备") Log.e(TAG, "蓝牙适配器为空")
}
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "检查已连接设备时发生异常", e) Log.e(TAG, "检查已连接设备时发生异常", e)
@ -706,14 +712,11 @@ private var companionManager: BleCompanionManager? = null
if (hasConnectedHeadset && headsetInfoList.isNotEmpty()) { if (hasConnectedHeadset && headsetInfoList.isNotEmpty()) {
Log.i(TAG, "检测到 ${headsetInfoList.size} 个蓝牙耳机已连接") Log.i(TAG, "检测到 ${headsetInfoList.size} 个蓝牙耳机已连接")
// 查找匹配的配对设备
val pairedMacAddress = BleService.getPairedMacAddress() val pairedMacAddress = BleService.getPairedMacAddress()
val targetHeadset = headsetInfoList.find { it.address == pairedMacAddress } val targetHeadset = headsetInfoList.find { it.address == pairedMacAddress }
if (targetHeadset != null) { if (targetHeadset != null) {
Log.i(TAG, "找到匹配的蓝牙耳机: ${targetHeadset.name} (${targetHeadset.address})") Log.i(TAG, "找到匹配的蓝牙耳机: ${targetHeadset.name} (${targetHeadset.address})")
// 使用协程异步处理连接
launch { launch {
if (!BleService.isConnected()) { if (!BleService.isConnected()) {
BleService.connect() BleService.connect()
@ -743,14 +746,18 @@ private var companionManager: BleCompanionManager? = null
private fun checkBluetoothHeadsetConnection(callback: (Boolean, List<BluetoothHeadsetInfo>) -> Unit) { private fun checkBluetoothHeadsetConnection(callback: (Boolean, List<BluetoothHeadsetInfo>) -> Unit) {
try { try {
bluetoothAdapter?.let { adapter -> bluetoothAdapter?.let { adapter ->
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) // 仅在 API>=31 时检查 BLUETOOTH_CONNECT;Android 11 及以下跳过
val needBtConnectPermission = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S
if (needBtConnectPermission &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT)
!= PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "缺少蓝牙连接权限,无法检查蓝牙耳机连接状态") Log.w(TAG, "缺少蓝牙连接权限(API>=31),无法检查蓝牙耳机连接状态")
callback(false, emptyList()) callback(false, emptyList())
return return
} }
// 检查HEADSET设备连接状态 // 检查HEADSET设备连接状态(兼容捕获安全异常)
try {
adapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener { adapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener {
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
try { try {
@ -760,7 +767,6 @@ private var companionManager: BleCompanionManager? = null
Log.i(TAG, "蓝牙耳机连接设备数量: ${connectedDevices.size}") Log.i(TAG, "蓝牙耳机连接设备数量: ${connectedDevices.size}")
if (hasConnectedHeadset) { if (hasConnectedHeadset) {
// 返回所有连接的蓝牙耳机设备信息
val headsetInfoList = connectedDevices.map { device -> val headsetInfoList = connectedDevices.map { device ->
BluetoothHeadsetInfo( BluetoothHeadsetInfo(
name = device.name ?: "Unknown Headset", name = device.name ?: "Unknown Headset",
@ -779,7 +785,10 @@ private var companionManager: BleCompanionManager? = null
adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy)
callback(false, emptyList()) callback(false, emptyList())
} }
} catch (se: SecurityException) {
Log.e(TAG, "读取连接设备安全异常(厂商兼容)", se)
adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy)
callback(false, emptyList())
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "检查蓝牙耳机连接状态时发生异常", e) Log.e(TAG, "检查蓝牙耳机连接状态时发生异常", e)
adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy)
@ -791,7 +800,10 @@ private var companionManager: BleCompanionManager? = null
Log.d(TAG, "蓝牙耳机服务断开连接") Log.d(TAG, "蓝牙耳机服务断开连接")
} }
}, BluetoothProfile.HEADSET) }, BluetoothProfile.HEADSET)
} catch (se: SecurityException) {
Log.e(TAG, "获取ProfileProxy安全异常(厂商兼容)", se)
callback(false, emptyList())
}
} ?: run { } ?: run {
Log.e(TAG, "蓝牙适配器为空") Log.e(TAG, "蓝牙适配器为空")
callback(false, emptyList()) callback(false, emptyList())

Loading…
Cancel
Save