|
|
|
@ -197,6 +197,11 @@ object BleService { |
|
|
|
private var maxMtu = 517 // 最大MTU值 (512 + 5字节头部) |
|
|
|
private var isWaitingForWriteCallback = false // 是否正在等待写入回调 |
|
|
|
|
|
|
|
// 连接参数(由 GATT 隐藏回调 onConnectionUpdated 更新,-1 表示尚未拿到) |
|
|
|
@Volatile private var connInterval = -1 // 单位 1.25ms |
|
|
|
@Volatile private var connLatency = -1 // 从机延迟(可跳过的连接事件数) |
|
|
|
@Volatile private var connTimeout = -1 // 监督超时,单位 10ms |
|
|
|
|
|
|
|
// 通知注册串行化相关变量 |
|
|
|
private val notificationSetupQueue: ArrayDeque<BluetoothGattCharacteristic> = ArrayDeque() |
|
|
|
private var isDescriptorWriteInProgress = false |
|
|
|
@ -899,6 +904,28 @@ object BleService { |
|
|
|
*/ |
|
|
|
private val gattCallback = object : BluetoothGattCallback() { |
|
|
|
|
|
|
|
/** |
|
|
|
* 连接参数更新回调,用于获取真实的连接间隔。 |
|
|
|
* |
|
|
|
* 注意:BluetoothGattCallback#onConnectionUpdated 在 SDK 中标记为 @hide, |
|
|
|
* 编译期不可见,所以这里【不能加 override】;只要方法签名与父类完全一致, |
|
|
|
* 运行时 JVM 虚方法分派仍会调用到这里(与 ota 插件 BleManager 的做法一致)。 |
|
|
|
* |
|
|
|
* interval 单位 1.25ms,timeout 单位 10ms,status=0 表示更新成功。 |
|
|
|
*/ |
|
|
|
fun onConnectionUpdated(g: BluetoothGatt, interval: Int, latency: Int, timeout: Int, status: Int) { |
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
|
connInterval = interval |
|
|
|
connLatency = latency |
|
|
|
connTimeout = timeout |
|
|
|
} |
|
|
|
Log.i( |
|
|
|
TAG, |
|
|
|
"连接参数更新: interval=$interval (${String.format("%.2f", interval * 1.25)}ms), " + |
|
|
|
"latency=$latency, timeout=$timeout (${timeout * 10}ms), status=$status" |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
override fun onConnectionStateChange(g: BluetoothGatt, status: Int, newState: Int) { |
|
|
|
when { |
|
|
|
status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED -> { |
|
|
|
@ -912,6 +939,9 @@ object BleService { |
|
|
|
newState == BluetoothProfile.STATE_DISCONNECTED -> { |
|
|
|
updateConnectionState(BleConst.STATE_DISCONNECTED) |
|
|
|
Log.d(TAG, "监听到已断开连接") |
|
|
|
connInterval = -1 |
|
|
|
connLatency = -1 |
|
|
|
connTimeout = -1 |
|
|
|
|
|
|
|
disconnectGatt() |
|
|
|
val pairedUuid = companionManager?.getPairedUuid() |
|
|
|
@ -1041,6 +1071,9 @@ object BleService { |
|
|
|
// 在服务发现完成后,请求最大MTU |
|
|
|
//requestMaxMtu(g) |
|
|
|
|
|
|
|
// 服务发现完成后打印一次当前连接参数(连接间隔等) |
|
|
|
Handler(Looper.getMainLooper()).postDelayed({ logConnectionParams() }, 1500) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
override fun onCharacteristicChanged(g: BluetoothGatt, c: BluetoothGattCharacteristic) { |
|
|
|
@ -2073,6 +2106,53 @@ object BleService { |
|
|
|
} ?: false |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取当前连接参数(连接间隔/从机延迟/监督超时) |
|
|
|
* 值来自 GATT 隐藏回调 onConnectionUpdated,未拿到时各项为 -1。 |
|
|
|
*/ |
|
|
|
fun getConnectionParams(): Map<String, Any> { |
|
|
|
return mapOf( |
|
|
|
"interval" to connInterval, // 原始值,单位 1.25ms |
|
|
|
"intervalMs" to if (connInterval < 0) -1.0 else connInterval * 1.25, |
|
|
|
"latency" to connLatency, |
|
|
|
"timeout" to connTimeout, // 原始值,单位 10ms |
|
|
|
"timeoutMs" to if (connTimeout < 0) -1 else connTimeout * 10, |
|
|
|
"connected" to isConnected() |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 打印当前连接参数到日志 |
|
|
|
*/ |
|
|
|
fun logConnectionParams() { |
|
|
|
if (connInterval < 0) { |
|
|
|
Log.i(TAG, "连接参数: 尚未收到 onConnectionUpdated 回调(未连接,或系统未上报连接参数更新)") |
|
|
|
} else { |
|
|
|
Log.i( |
|
|
|
TAG, |
|
|
|
"当前连接参数: interval=$connInterval (${String.format("%.2f", connInterval * 1.25)}ms), " + |
|
|
|
"latency=$connLatency, timeout=$connTimeout (${connTimeout * 10}ms)" |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 主动请求连接优先级,会触发一次连接参数协商,随后由 onConnectionUpdated 打印新的连接间隔。 |
|
|
|
* @param priority BluetoothGatt.CONNECTION_PRIORITY_HIGH(1) / BALANCED(0) / LOW_POWER(2) |
|
|
|
*/ |
|
|
|
fun requestConnectionPriority(priority: Int): Boolean { |
|
|
|
return bluetoothGatt?.let { gatt -> |
|
|
|
try { |
|
|
|
val result = gatt.requestConnectionPriority(priority) |
|
|
|
Log.i(TAG, "请求连接优先级 priority=$priority: $result") |
|
|
|
result |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "请求连接优先级异常: ${e.message}", e) |
|
|
|
false |
|
|
|
} |
|
|
|
} ?: false |
|
|
|
} |
|
|
|
|
|
|
|
fun startWakeupVoiceUpgrade(bytes: ByteArray): Boolean { |
|
|
|
return wakeupVoicesUpgradeSender.startWakeupVoiceUpgrade(bytes) |
|
|
|
} |
|
|
|
|