|
|
|
@ -43,10 +43,12 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
// 用于验证的临时MAC地址 |
|
|
|
private var pendingMacAddress: String? = null |
|
|
|
|
|
|
|
// 获取CompanionDeviceManager服务 |
|
|
|
private val companionDeviceManager: CompanionDeviceManager? by lazy { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager |
|
|
|
/** |
|
|
|
* 获取CompanionDeviceManager服务(使用Activity上下文) |
|
|
|
*/ |
|
|
|
private fun getCompanionDeviceManager(activity: Activity): CompanionDeviceManager? { |
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
activity.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager |
|
|
|
} else { |
|
|
|
null |
|
|
|
} |
|
|
|
@ -56,7 +58,7 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
* 检查是否支持CompanionDeviceManager |
|
|
|
*/ |
|
|
|
fun isCompanionDeviceManagerSupported(): Boolean { |
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && companionDeviceManager != null |
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -96,6 +98,24 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
*/ |
|
|
|
@RequiresApi(Build.VERSION_CODES.O) |
|
|
|
fun getAssociationIds(): List<String> { |
|
|
|
// 使用默认上下文获取CompanionDeviceManager |
|
|
|
val companionDeviceManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager |
|
|
|
} else { |
|
|
|
null |
|
|
|
} |
|
|
|
return getAssociationIds(companionDeviceManager) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取所有关联ID(使用指定的CompanionDeviceManager) |
|
|
|
*/ |
|
|
|
@RequiresApi(Build.VERSION_CODES.O) |
|
|
|
private fun getAssociationIds(companionDeviceManager: CompanionDeviceManager?): List<String> { |
|
|
|
if (companionDeviceManager == null) { |
|
|
|
return emptyList() |
|
|
|
} |
|
|
|
|
|
|
|
// 注意: allAssociations 仅在API 33 (Android 13)及以上版本可用 |
|
|
|
// 在早期版本中,使用associations (返回的是String数组) |
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
|
|
|
@ -115,7 +135,7 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
} |
|
|
|
} else { |
|
|
|
// API 26-32 使用 associations 方法 |
|
|
|
companionDeviceManager?.associations?.toList() ?: emptyList() |
|
|
|
companionDeviceManager.associations?.toList() ?: emptyList() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -134,7 +154,9 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
serviceUuid: String? = null, |
|
|
|
macAddress: String? = null |
|
|
|
): Boolean { |
|
|
|
if (!isCompanionDeviceManagerSupported()) { |
|
|
|
// 使用Activity上下文获取CompanionDeviceManager |
|
|
|
val companionDeviceManager = getCompanionDeviceManager(activity) |
|
|
|
if (companionDeviceManager == null) { |
|
|
|
Log.e(TAG, "设备不支持CompanionDeviceManager") |
|
|
|
return false |
|
|
|
} |
|
|
|
@ -196,7 +218,7 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
.build() |
|
|
|
|
|
|
|
// 发起关联请求 |
|
|
|
companionDeviceManager?.associate( |
|
|
|
companionDeviceManager.associate( |
|
|
|
associationRequest, |
|
|
|
object : CompanionDeviceManager.Callback() { |
|
|
|
override fun onDeviceFound(intentSender: IntentSender) { |
|
|
|
@ -328,12 +350,12 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
pendingMacAddress = null |
|
|
|
|
|
|
|
// 🎯 立即发起BLE连接 |
|
|
|
try { |
|
|
|
Log.i(TAG, "配对成功,立即发起BLE连接: $deviceMac") |
|
|
|
BleService.connect(deviceMac) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "配对成功后发起BLE连接失败: ${e.message}", e) |
|
|
|
} |
|
|
|
// try { |
|
|
|
// Log.i(TAG, "配对成功,立即发起BLE连接: $deviceMac") |
|
|
|
// BleService.connect(deviceMac) |
|
|
|
// } catch (e: Exception) { |
|
|
|
// Log.e(TAG, "配对成功后发起BLE连接失败: ${e.message}", e) |
|
|
|
// } |
|
|
|
|
|
|
|
return true |
|
|
|
} else { |
|
|
|
@ -447,19 +469,26 @@ class BleCompanionManager(private val context: Context) { |
|
|
|
@RequiresApi(Build.VERSION_CODES.O) |
|
|
|
fun clearAllAssociations() { |
|
|
|
try { |
|
|
|
// 获取CompanionDeviceManager |
|
|
|
val companionDeviceManager = context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager |
|
|
|
if (companionDeviceManager == null) { |
|
|
|
Log.e(TAG, "无法获取CompanionDeviceManager") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 清除本地保存的MAC地址 |
|
|
|
clearPairedMacAddress() |
|
|
|
|
|
|
|
// 获取所有关联ID |
|
|
|
val associationIds = getAssociationIds() |
|
|
|
val associationIds = getAssociationIds(companionDeviceManager) |
|
|
|
|
|
|
|
// 遍历解除关联 |
|
|
|
for (id in associationIds) { |
|
|
|
try { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
|
|
|
companionDeviceManager?.disassociate(id) |
|
|
|
companionDeviceManager.disassociate(id) |
|
|
|
} else { |
|
|
|
companionDeviceManager?.disassociate(id) |
|
|
|
companionDeviceManager.disassociate(id) |
|
|
|
} |
|
|
|
Log.i(TAG, "已解除设备关联: $id") |
|
|
|
} catch (e: Exception) { |
|
|
|
|