问题点5:Android BLE具体连接flow 并问询DB的API flow 之第二阶段问询;
表示第二阶段的log “Start service discovery: srvc_idx =”在Android9没有,但在Android 8.0中有,所以后续截图基于Android8.0。

-->执行API bta_gattc_start_disc_include_srvc;

-->执行API bta_gattc_discover_procedure,入参是GATT_DISC_INC_SRVC;
最终执行的API 和第一阶段一致,也是GATTC_Discover;

-->执行GATTC_Discover,内部执行API gatt_act_discovery;
此时使用的Opcode是“Read By Type Request”,
原生BT中的宏是:GATT_REQ_READ_BY_TYPE;
此时使用的Attribute ID 是GATT_UUID_INCLUDE_SERVICE(0x2802)

-->问询结果在API gatt_client_handle_server_rsp中返回,如果此服务中没有包含Include,则返回的 opcode是0x01(原生BT中使用GATT_RSP_ERROR表示0x01);执行到“case GATT_RSP_ERROR”

-->执行API gatt_process_error_rsp;

-->执行gatt_proc_disc_error_rsp,最终执行到gatt_end_operation,进而触发
p_disc_cmpl_cb,使得实作API bta_gattc_disc_cmpl_cback被执行;

-->执行到bta_gattc_disc_cmpl_cback,此时的disc_type是GATT_DISC_INC_SRVC;

-->此时执行触发第二阶段的第二步,使用“Read By Type Request”+ Attribute Type :Characteristic Declaration”问询是否支援Characteristic
执行API bta_gattc_incl_srvc_disc_cmpl;

-->第二阶段的第二步启动API bta_gattc_start_disc_char;
此API 也是使用GATTC_Discover,设置的op_subtype是GATT_DISC_CHAR

对应的opcode是:GATT_REQ_READ_BY_TYPE
对应的Attribute ID是:GATT_UUID_CHAR_DECLARE


-->问询结果在API gatt_client_handle_server_rsp中返回:
若当前Service不包含特征时,返回的是response是0x01 (GATT_RSP_ERROR);

若当前Service 包含特征(Characteristic)时, 返回的reponse是
0x09(GATT_RSP_READ_BY_GRP_TYPE),

-->当前假定有特征值,此次执行gatt_process_read_by_type_rsp;
(问询Read By Group Type Request 和Read By Type Request)返回内容的处理API是同一个:gatt_process_read_by_type_rsp
因此第二阶段的第二步也是在API gatt_process_read_by_type_rsp内部,使用
gatt_act_discovery自动后续问询当前Service中的其他特征;
-->当第二阶段的第二步全部问询完成时,callback API bta_gattc_disc_cmpl_cback被执行,此时的disc_type是GATT_DISC_CHAR,因此执行API bta_gattc_char_disc_cmpl;

-->执行 API bta_gattc_char_disc_cmpl;
(需注意的是:第二阶段可能还有第三步操作:通过“Find Information Request”进行问询Information;)

-->通过执行API bta_gattc_start_disc_char_dscp进行第二阶段的第三步问询;

-->执行API bta_gattc_discover_procedure,入参是GATT_DISC_CHAR_DSCPT;
最终执行的API GATTC_Discover;
此时对应的opcode:GATT_REQ_FIND_INFO(0x04)
此时对应的Attribute:没有;


-->第二阶段第三步的问询结果通过gatt_client_handle_server_rsp返回,此时对应的op_code是GATT_RSP_FIND_INFO(0x05),


-->执行API gatt_process_read_info_rsp;

需注意的是:虽然API gatt_process_read_info_rsp最后也执行了gatt_act_discovery用于触发后续同类型的自动问询,但如果handle值已经满,那么也将通过执行gatt_end_operation,触发callback API bta_gattc_disc_cmpl_cback;



-->执行bta_gattc_disc_cmpl_cback, 此时disc_type 是GATT_DISC_CHAR_DSCPT;

-->执行API bta_gattc_char_dscpt_disc_cmpl;

当同一服务内的特征全部完成完成时,此时执行else部分,执行API bta_gattc_explore_srvc;

-->当前假定全部问询结束,触发log “no more services found”;


![[计算机毕业设计]基于SM9的密钥交换方案的实现与应用](https://img-blog.csdnimg.cn/632f16aceeee4be6a8443d20fb0be8d8.png)

















