2、认证时使用的command(先看认证,认证失败才会重新配对)
(1)Authentication Requested Command(常用)
这个命令用来请求对远端设备进行认证,Connection_Handle表示本地设备和远端设备之间的ACL连接
注意:如果认证失败,不会自动断开连接,需要host发起断开动作
这个命令的后续动作:
第一步:host发送Authentication_Requested以后,会立即收到一个Command Status event表示Authentication_Requested是否成功。
第二步:
A)如果Simple Pairing Mode是enabled,host会收到一个Link Key Request event,此时
a)Host如果回复Link Key Request Negative Reply Command,认证失败,那么就会开始Simple Pairing
b)Host如果回复Link Key Request Reply Command,则继续认证过程
B)如果Simple Pairing Mode不是enabled,host会收到一个Link Key Request event,此时
a)Host如果回复Link Key Request Negative Reply Command,认证失败,那么就会开始PIN code认证,host会收到PIN Code Request event
b)Host如果回复Link Key Request Reply Command,则继续认证过程
第三步:认证成功,host会接收到Authentication Complete event
参数说明:
Connection_Handle(2 Octet):表示一个ACL连接(注意:这个只是用来确定两个BR/EDR Controller,认证的是两个BR/EDR Controller之间所有的连接,不是只有Connection_Handle表示的连接)
举例说明:
Command如下:
0x01:表示HCI Command Packet
0x0411:表示是HCI_Authentication_Requested command
指令的OGF是0x01即0000 01
OCF是0x0011即00 0001 0001
所以整个opcode是0000 0100 0001 0001即0x0411
0x2:表示Parameter_Total_Length=0x2,即参数总长度是2个byte
0x000d:表示Connection_Handle
对应的Event:
Command Status event:
0x04:表示HCI Event Packet
0x0f:表示这个event是command status event
0x04:表示Parameter_Total_Length即4个byte
0x00:表示status为0
0x01:表示Num_HCI_Command_Packets
0x0411:表示是HCI_Authentication_Requested command
之后
host收到Link Key Request event->
host没有link key,所以回复Link Key Request Negative Reply Command->
认证失败->
host收到PIN Code Request event->
PIN_Code_Request_Reply->
PIN code配对完成收到Link Key Notification event->
认证完成,收到Authentication Complete event
如下图所示:
(2)Link Key Request Event(常用)
收到这个event表示与BD_ADDR的连接需要一个Link Key,如果host有对应的 Link Key,host需要使用Link_Key_Request_Reply将这个Link Key发送给Controller,如果host没有对应的Link Key,host需要使用Link_Key_Request_Negative_Reply进行回复
注意:需要在LMP response timeout之前回复;
如果host用Link_Key_Request_Negative_Reply回复Link Key Request Event,并且即将用到的service需要一个认证的link key,那么在之后的IO_capability中应该设置Authentication_Requirements为MITM Protection Required的选项(一般都用MITM Protection Required的选项)
参数说明:
BD_ADDR(6 Octet):远端设备地址
举例说明:
0x04:表示HCI Event Packet
0x17:表示这个event是Link Key Request event
0x06:表示Parameter_Total_Length即6个byte
0xe1 0x99 0xce 0x0a 0xcc 0x66:表示远端设备地址是0xcc 0x66 0x0a 0xce 0x99 0xe1
(3)Link Key Request Reply Command(常用)
当收到Link Key Request event时,如果host保存有跟BD_ADDR相关的Link key则可以使用这个命令进行回复
注意:需要在LMP response timeout之前回复
当BR/EDR Controller支持Secure Connections (Controller Support) feature时,每次断开连接都要删除Link Key
参数说明:
BD_ADDR(6 Octet):远端设备地址
Link_Key(16 Octet):与BD_ADDR相关的link key
返回参数说明:
Status(1 Octet):0x00表示成功,其他值表示失败
BD_ADDR(6 Octet):远端设备地址
举例说明:
Command如下:
0x01:表示HCI Command Packet
0x040B:表示是HCI_Link_Key_Request_Reply command
指令的OGF是0x01即0000 01
OCF是0x000B即00 0000 1011
所以整个opcode是0000 0100 0000 1011即0x040B
0x16:表示Parameter_Total_Length=0x16,即参数总长度是22个byte
0xe1 0x99 0xce 0x0a 0xcc 0x66:表示远端设备地址是0xcc 0x66 0x0a 0xce 0x99 0xe1
Link Key: 0xf1 0x4f 0x26 0x9b 0x00 0x3f 0x3b 0x88 0x85 0x95 0x79 0x20 0x66 0x71 0x3d 0x39
对应的Event:
Command Complete event:
0x04:表示HCI Event Packet
0x0e:表示这个event是command complete event
0x0a:表示Parameter_Total_Length即10个byte
0x01:表示Num_HCI_Command_Packets
0x040b:表示是HCI_Link_Key_Request_Reply command
0x00:表示status为0
0xe1 0x99 0xce 0x0a 0xcc 0x66:表示远端设备地址是0xcc 0x66 0x0a 0xce 0x99 0xe1
(4)Link Key Request Negative Reply Command(常用)
如果host没有对应BD_ADDR的Link Key,host需要使用Link_Key_Request_Negative_Reply进行回复
注意:需要在LMP response timeout之前回复
参数说明:
BD_ADDR(6 Octet):远端设备地址
返回参数说明:
Status(1 Octet):0x00表示成功,其他值表示失败
BD_ADDR(6 Octet):远端设备地址
举例说明:
Command如下:
0x01:表示HCI Command Packet
0x040C:表示是HCI_Link_Key_Request_Negative_Reply command
指令的OGF是0x01即0000 01
OCF是0x000C即00 0000 1100
所以整个opcode是0000 0100 0000 1100即0x040C
0x6:表示Parameter_Total_Length=0x6,即参数总长度是6个byte
0x35 0x8b 0xf8 0x11 0x1a 0xfc:表示远端设备地址是0xfc 0x1a 0x11 0xf8 0x8b 0x35
对应的Event:
Command Complete event:
0x04:表示HCI Event Packet
0x0e:表示这个event是command complete event
0x0a:表示Parameter_Total_Length即10个byte
0x01:表示Num_HCI_Command_Packets
0x040c:表示是HCI_Link_Key_Request_Negative_Reply command
0x00:表示status为0
0x35 0x8b 0xf8 0x11 0x1a 0xfc:表示远端设备地址是0xfc 0x1a 0x11 0xf8 0x8b 0x35
(5)Authentication Complete Event(常用)
当认证完成时,host会收到这个event
参数说明:
Status(1 Octet):0x00表示成功,其他值表示失败
Connection_Handle(2 Octet):表示一个ACL连接
举例说明:
0x04:表示HCI Event Packet
0x06:表示这个event是Authentication Complete event
0x03:表示Parameter_Total_Length即3个byte
0x00:表示status为0
0x000d:表示Connection_Handle