Browse Source

修复发送ble命令没有回复。导致后续的命令发送异常

newdev_shunjiawei
fdp 1 year ago
parent
commit
1061812bda
  1. 54
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt

54
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 var isInitialized = false
private val replyTimeoutHandler = Handler(Looper.getMainLooper())
private val replyTimeoutRunnable = Runnable {
if (!isReply) {
Log.w(TAG, "命令响应超时,自动重置 isReply 为 true")
isReply = true
processNextCommand()
}
}
/** /**
* 初始化BLE服务 * 初始化BLE服务
* @param appContext 应用上下文 * @param appContext 应用上下文
@ -471,6 +480,8 @@ object BleService {
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "关闭GATT连接异常: ${e.message}", e) Log.e(TAG, "关闭GATT连接异常: ${e.message}", e)
} }
isReply = true;
commandQueue.clear()
bluetoothGatt = null bluetoothGatt = null
notifyChar = null notifyChar = null
writeChar = null writeChar = null
@ -520,6 +531,7 @@ object BleService {
newState == BluetoothProfile.STATE_DISCONNECTED -> { newState == BluetoothProfile.STATE_DISCONNECTED -> {
updateConnectionState(BleConst.STATE_DISCONNECTED) updateConnectionState(BleConst.STATE_DISCONNECTED)
disconnectGatt() disconnectGatt()
// otaManager?.onBtDeviceConnection(g.device, StateCode.CONNECTION_CONNECTING) // otaManager?.onBtDeviceConnection(g.device, StateCode.CONNECTION_CONNECTING)
// g.close() // g.close()
// otaManager?.release(); // otaManager?.release();
@ -752,19 +764,10 @@ object BleService {
} }
}" }"
) )
isReply = true; isReply = true
cmdReplyType = cmdType; cmdReplyType = cmdType
if (!commandQueue.isEmpty()) { replyTimeoutHandler.removeCallbacks(replyTimeoutRunnable) // 移除超时回调
val nextCmd = commandQueue.poll() processNextCommand()
writeChar!!.value = nextCmd
var isSeccess = bluetoothGatt?.writeCharacteristic(writeChar)
if (isSeccess == true) {
Log.d(
TAG,
"收到设备响应:"
)
}
}
// 根据命令类型处理不同的响应 // 根据命令类型处理不同的响应
when (cmdType) { when (cmdType) {
BleConst.CMD_GET_VERSION -> { BleConst.CMD_GET_VERSION -> {
@ -1385,13 +1388,16 @@ object BleService {
// 加入队列并触发发送 // 加入队列并触发发送
if (isReply) { if (isReply) {
var isSeccess = bluetoothGatt?.writeCharacteristic(writeChar) val isSuccess = bluetoothGatt?.writeCharacteristic(writeChar)
if (isSeccess == true) { if (isSuccess == true) {
isReply = false; isReply = false
cmdType; replyTimeoutHandler.postDelayed(replyTimeoutRunnable, 1000) // 设置1秒超时
return true return true
} else } else
{
commandQueue.poll()
return false return false
}
} else { } else {
commandQueue.offer(cmdPacket) 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校验值 * 计算CRC8/MAXIM校验值
*/ */
@ -1588,4 +1607,3 @@ class RecordingFile(private val context: Context) {
} }
} }
} }
Loading…
Cancel
Save