From 81caf12f8a80f2ecbe547187354d2391bf67217c Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Sun, 20 Jul 2025 00:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=91=E9=80=81ble?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=B2=A1=E6=9C=89=E5=9B=9E=E5=A4=8D=E3=80=82?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=90=8E=E7=BB=AD=E7=9A=84=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yunqiinnovation/ble_service/BleService.kt | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt index af78e6d49..999dd7efc 100644 --- a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt +++ b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt @@ -106,6 +106,15 @@ object BleService { // 初始化状态 private var isInitialized = false + private val replyTimeoutHandler = Handler(Looper.getMainLooper()) + private val replyTimeoutRunnable = Runnable { + if (!isReply) { + Log.w(TAG, "命令响应超时,自动重置 isReply 为 true") + isReply = true + processNextCommand() + } + } + /** * 初始化BLE服务 * @param appContext 应用上下文 @@ -471,6 +480,8 @@ object BleService { } catch (e: Exception) { Log.e(TAG, "关闭GATT连接异常: ${e.message}", e) } + isReply = true; + commandQueue.clear() bluetoothGatt = null notifyChar = null writeChar = null @@ -520,6 +531,7 @@ object BleService { newState == BluetoothProfile.STATE_DISCONNECTED -> { updateConnectionState(BleConst.STATE_DISCONNECTED) disconnectGatt() + // otaManager?.onBtDeviceConnection(g.device, StateCode.CONNECTION_CONNECTING) // g.close() // otaManager?.release(); @@ -752,19 +764,10 @@ object BleService { } }" ) - isReply = true; - cmdReplyType = cmdType; - if (!commandQueue.isEmpty()) { - val nextCmd = commandQueue.poll() - writeChar!!.value = nextCmd - var isSeccess = bluetoothGatt?.writeCharacteristic(writeChar) - if (isSeccess == true) { - Log.d( - TAG, - "收到设备响应:" - ) - } - } + isReply = true + cmdReplyType = cmdType + replyTimeoutHandler.removeCallbacks(replyTimeoutRunnable) // 移除超时回调 + processNextCommand() // 根据命令类型处理不同的响应 when (cmdType) { BleConst.CMD_GET_VERSION -> { @@ -1385,13 +1388,16 @@ object BleService { // 加入队列并触发发送 if (isReply) { - var isSeccess = bluetoothGatt?.writeCharacteristic(writeChar) - if (isSeccess == true) { - isReply = false; - cmdType; + val isSuccess = bluetoothGatt?.writeCharacteristic(writeChar) + if (isSuccess == true) { + isReply = false + replyTimeoutHandler.postDelayed(replyTimeoutRunnable, 1000) // 设置1秒超时 return true } else + { + commandQueue.poll() return false + } } else { commandQueue.offer(cmdPacket) } @@ -1403,6 +1409,19 @@ object BleService { } } + private fun processNextCommand() { + if (!commandQueue.isEmpty()) { + val nextCmd = commandQueue.poll() + writeChar!!.value = nextCmd + val isSuccess = bluetoothGatt?.writeCharacteristic(writeChar) + if (isSuccess == true) { + isReply = false + replyTimeoutHandler.postDelayed(replyTimeoutRunnable, 1000) // 设置1秒超时 + Log.d(TAG, "发送队列中的下一个命令") + } + } + } + /** * 计算CRC8/MAXIM校验值 */ @@ -1588,4 +1607,3 @@ class RecordingFile(private val context: Context) { } } } - \ No newline at end of file