3. 消息类型

通信消息有三种类型: Request,Response 和 Indication。他们分别是通过BL的 Write 或 Indication Characteristic 来传输。

具体关系如下表:

消息类型

方向

Write Characteristic

Indication Characteristic

Request

手机 -> 设备

Y

Response

手机 <- 设备

Y

Indication

手机 <- 设备

Y

3.1. 同步和异步命令(Request)

根据命令执行时长不同,命令分为两类: 同步命令和异步命令。

同步命令会很快返回执行结果,而异步命令需要较久才能返回执行结果。

3.2. 向设备发送同步命令

同步的执行结果会在Response中返回。

!pragma teoz true
participant 手机 as P #cyan
box 设备 #LightBlue
box BLE
participant Write as W #khaki
participant Indication as I  #lightgreen
end box
participant MCU as M #deepskyblue
end box

autonumber
...
note over P,I: BLE已连接
P ->(10) W : Request( timeout, ...)
W -> M :
M -> I :
P (10)<- I : Response( error, result )

3.3. 向设备发送异步命令

对于异步命令,Response中返回的是处理结果。执行结果会在Indication中返回。 如果Response中返回的是错误,那么后续不会有Indication。

!pragma teoz true
participant 手机 as P #cyan
box 设备 #LightBlue
box BLE
participant Write as W #khaki
participant Indication as I  #lightgreen
end box
participant MCU as M #deepskyblue
end box

autonumber
...
note over P,I: BLE已连接
P ->(10) W : Request( timeout, ...)
W -> M :
M -> I :
P (10)<- I : Response( error )
...
M -> I :
P (10)<- I : Indication( error,result )

注意

即使手机断开连接,已经发送的命令也不会被取消。命令会继续执行,直到有结果或者超时。

3.4. 未收到Response的情况

如果手机在 1秒 内没有收到response,那么可以认为命令发送失败。

!pragma teoz true
participant 手机 as P #cyan
box 设备 #LightBlue
box BLE
participant Write as W #khaki
participant Indication as I  #lightgreen
end box
participant MCU as M #deepskyblue
end box

autonumber
...
note over P,I: BLE已连接
== 发送命令 ==

P ->(10) W : Request( timeout, ...)
W -> M :
M -> I :
P (10)<-- I !! : Response( error )

如果是同步命令,手机可以再次发送此命令。

如果是异步命令,设备如果确实收到了命令,那么设备很可能还在处理此命令。再次发送任何一个异步命令都会收到一个 “COMMAND_IN_PROCESS” 的错误。如果此命令支持取消,那么可以发送一次取消。如果此命令不支持取消,那么只能等超时或者将设备断电再重新上电。