2.4. DEMO_BLE工程说明
概述
本工程展示了使用蓝牙BLE分别充当主从机进行数据传输测试功能。
1.从机 数传
2.从机 hogp
3.主机 client角色 / 多机通讯(支持多个主机同时运行)
4.wifi配网
具体的示例工程代码详见 apps/demo/demo_ble
。
2.4.1. 工程配置说明
apps/demo/demo_ble/include/app_config.h
A)打开宏 #define TCFG_USER_BLE_ENABLE 使能BLE功能
B)BLE DEMO模式选择:选择需要打开的测试用例并配置
配置#define THIRD_PARTY_PROTOCOLS_SEL CUSTOM_DEMO_EN //从机模式 数传配置#define THIRD_PARTY_PROTOCOLS_SEL LE_HOGP_EN //从机模式 hogp配置#define THIRD_PARTY_PROTOCOLS_SEL MULTI_BOX_ADV_EN //主机模式/多机通讯(支持多个主机同时运行)配置#define THIRD_PARTY_PROTOCOLS_SEL NET_CFG_EN //用于wifi配网
#define CUSTOM_DEMO_EN (1 << 19) // 第三方协议的demo,用于示例客户开发自定义协议
#define MULTI_BOX_ADV_EN (1 << 20)
#define NET_CFG_EN (1 << 29)
#define LE_HOGP_EN (1 << 30)
#if TCFG_THIRD_PARTY_PROTOCOLS_ENABLE
#if TCFG_PAY_ALIOS_ENABLE
#define ALIPAY_SEL ALIPAY_EN
#else
#define ALIPAY_SEL 0
#endif
#if TCFG_AI_SERVER == TCFG_DUER_ENABLE
#define THIRD_PARTY_PROTOCOLS_SEL ((TCFG_THIRD_PARTY_PROTOCOLS_SEL & ~NET_CFG_EN) | DUEROS_EN)
#else
#define THIRD_PARTY_PROTOCOLS_SEL (TCFG_THIRD_PARTY_PROTOCOLS_SEL|ALIPAY_SEL)
#endif
#endif
#ifndef THIRD_PARTY_PROTOCOLS_SEL
#define THIRD_PARTY_PROTOCOLS_SEL MULTI_BOX_ADV_EN
#endif
2.4.2. 操作说明:
- 1.从机模式
- 通过手机app(nrf connect)(扫描、发现、连接低功耗蓝牙设备的应用)连接到BLE设备JL-AC79XXble

- 对已经连接的蓝牙ble设备进行trans_data传输数据,进行写write,读read,通知notify等操作。

- 2.主机模式:主动通过搜索去连接其它BLE从机设备
- 用户需要填写以下过滤规则,填写不匹配会出现一直连接不上或者通讯失败的情况,需要指定搜索对应profile的uuid。
//ble.c
#if BT_NET_CENTRAL_EN || TRANS_MULTI_BLE_MASTER_NUMS
//用户需要填写以下过滤规则,填写错误会出现一直连接不上的情况
//指定搜索uuid
static const target_uuid_t search_uuid_table[] = {
// for uuid16
// PRIMARY_SERVICE, ae80
// CHARACTERISTIC, ae81, WRITE_WITHOUT_RESPONSE | DYNAMIC,
// CHARACTERISTIC, ae82, NOTIFY,
{
.services_uuid16 = 0xae80,
.characteristic_uuid16 = 0xae81,
.opt_type = ATT_PROPERTY_WRITE_WITHOUT_RESPONSE,
},
{
.services_uuid16 = 0xae80,
.characteristic_uuid16 = 0xae82,
.opt_type = ATT_PROPERTY_NOTIFY,
},
{
.services_uuid16 = 0xae00, // 6901A
.characteristic_uuid16 = 0xae01,
.opt_type = ATT_PROPERTY_WRITE_WITHOUT_RESPONSE,
},
{
.services_uuid16 = 0xae00, // 6901A
.characteristic_uuid16 = 0xae01,
.opt_type = ATT_PROPERTY_NOTIFY,
},
//for uuid128,sample
// PRIMARY_SERVICE, 0000F530-1212-EFDE-1523-785FEABCD123
// CHARACTERISTIC, 0000F531-1212-EFDE-1523-785FEABCD123, NOTIFY,
// CHARACTERISTIC, 0000F532-1212-EFDE-1523-785FEABCD123, WRITE_WITHOUT_RESPONSE | DYNAMIC,
/*
{
.services_uuid16 = 0,
.services_uuid128 = {0x00,0x00,0xF5,0x30 ,0x12,0x12 ,0xEF, 0xDE ,0x15,0x23 ,0x78,0x5F,0xEA ,0xBC,0xD1,0x23} ,
.characteristic_uuid16 = 0,
.characteristic_uuid128 = {0x00,0x00,0xF5,0x31 ,0x12,0x12 ,0xEF, 0xDE ,0x15,0x23 ,0x78,0x5F,0xEA ,0xBC,0xD1,0x23},
.opt_type = ATT_PROPERTY_NOTIFY,
},
{
.services_uuid16 = 0,
.services_uuid128 = {0x00,0x00,0xF5,0x30 ,0x12,0x12 ,0xEF, 0xDE ,0x15,0x23 ,0x78,0x5F,0xEA ,0xBC,0xD1,0x23} ,
.characteristic_uuid16 = 0,
.characteristic_uuid128 = {0x00,0x00,0xF5,0x32 ,0x12,0x12 ,0xEF, 0xDE ,0x15,0x23 ,0x78,0x5F,0xEA ,0xBC,0xD1,0x23},
.opt_type = ATT_PROPERTY_WRITE_WITHOUT_RESPONSE,
},
*/
};
3.多机通讯:支持多个主机角色同时运行。
//multi_box_scan.c
#if (THIRD_PARTY_PROTOCOLS_SEL & MULTI_BOX_ADV_EN)
#define SUPPORT_MAX_CLIENT 1
void multi_box_scan_all_init(void)
{
client_profile_init();
__bt_multi_client_init();
le_multi_client_hdl_init(cur_dev_cid);
multi_box_scan_module_enable(1);
}
#endif
2.4.3. 代码流程
1.app_main(),调用bt_ble_module_init()进行蓝牙BLE协议栈初始化
//app_main.c
/*
* 应用程序主函数
*/
void app_main()
{
puts("------------- demo_ble app main-------------\n");
extern void bt_ble_module_init(void);
bt_ble_module_init();
}
2.bt_ble_module_init()
A) 调用bt_get_mac_addr()函数获取蓝牙mac地址。B) 调用bt_make_ble_address()函数根据提供的edr地址生成唯一对应ble地址。C) 调用le_controller_set_mac()函数设置ble的public地址。D) 调用btstack_init()函数进行蓝牙协议栈初始化。
//ble.c
void bt_ble_module_init(void) //蓝牙BLE协议栈初始化
{
#if TCFG_USER_BLE_ENABLE
u8 tmp_ble_addr[6];
extern const u8 *bt_get_mac_addr(void);
bt_make_ble_address(tmp_ble_addr, (u8 *)bt_get_mac_addr()); //生成BLE随机地址 / bt_get_mac_addr()函数读取系统蓝牙mac地址
le_controller_set_mac((void *)tmp_ble_addr); //设置ble控制器mac地址
log_info("-----edr + ble 's address-----");
put_buf((void *)bt_get_mac_addr(), 6); //打印蓝牙mac地址
put_buf((void *)tmp_ble_addr, 6);//打印ble控制器mac地址
#endif
btstack_init();
}
3.BLE各个测试用例的初始化入口
//multi_protocol_main.c
#if (THIRD_PARTY_PROTOCOLS_SEL & CUSTOM_DEMO_EN) //从机模式 数传
custom_demo_all_init();
#endif
#if (THIRD_PARTY_PROTOCOLS_SEL & NET_CFG_EN) && !TCFG_POWER_ON_ENABLE_BLE //wifi配网
le_net_cfg_all_init();
#endif
#if (THIRD_PARTY_PROTOCOLS_SEL & LE_HOGP_EN) //从机模式 hogp
le_hogp_all_init();
#endif
#if (THIRD_PARTY_PROTOCOLS_SEL & MULTI_BOX_ADV_EN) //主机模式/多机通讯
#ifndef MULTI_BOX_ADV_FILTER_ENABLE
multi_box_scan_all_init();
#if (THIRD_PARTY_PROTOCOLS_SEL & ~MULTI_BOX_ADV_EN)
extern const int config_le_gatt_client_num;
extern const int config_le_hci_connection_num;
ASSERT(config_le_gatt_client_num >= 2 && config_le_hci_connection_num >= 2);
#endif
#endif
#endif
4.ble_report_data_deal(),主机模式下处理接收到的数据
//multi_box_scan.c
static void ble_report_data_deal(att_data_report_t *report_data, const target_uuid_t *search_uuid)
{
log_info("conn_handle:%04x,report_data:0x%02x,0x%02x,%d,len(%d)", report_data->conn_handle, report_data->packet_type,
report_data->value_handle, report_data->value_offset, report_data->blob_length);
log_info_hexdump(report_data->blob, report_data->blob_length);
/* if (search_uuid == NULL) { */
/* log_info("not_match handle"); */
/* return; */
/* } */
switch (report_data->packet_type) {
case GATT_EVENT_QUERY_COMPLETE:
break;
case GATT_EVENT_NOTIFICATION: //notify
log_info("GATT_EVENT_NOTIFICATION");
break;
case GATT_EVENT_INDICATION://indicate
case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT://read
log_info("GATT_EVENT_INDICATION");
break;
case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT://read long
log_info("GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT");
break;
default:
log_info("ble_report_data_deal is default");
break;
}
}
5.主机模式下发送测试数据
//multi_box_scan.c
static void client_event_callback(le_client_event_e event, u8 *packet, int size)
{
switch (event) {
case CLI_EVENT_MATCH_DEV: {
client_match_cfg_t *match_dev = (client_match_cfg_t *)packet;
log_info("match_name:%s", match_dev->compare_data);
}
break;
case CLI_EVENT_MATCH_UUID: {
opt_handle_t *opt_hdl = (opt_handle_t *)packet;
if (opt_hdl->search_uuid == &jl_search_uuid_table[0]) {
ble_client_write_handle = opt_hdl->value_handle;
log_info("match_uuid");
}
}
break;
case CLI_EVENT_SEARCH_PROFILE_COMPLETE:
log_info("CLI_EVENT_SEARCH_PROFILE_COMPLETE_CLIENT_EVENT_CALLBACK");
/* is_mode_active = 0; */
/* if ((!ble_client_timer) && ble_client_write_handle) { */
/* log_info("test timer_add\n"); */
/* ble_client_timer = sys_timer_add(0, client_test_write, 500); */
/* } */
/* target_handle[cur_dev_cid].write_handle = 0x0003; */
/* client_write_send(dg_conn_handle, mouse_notify_handle[2] + 1, &mouse_ccc_value, 2, ATT_OP_WRITE); */
break;
case CLI_EVENT_CONNECTED:
break;
case CLI_EVENT_DISCONNECT:
log_info("CLI_EVENT_DISCONNECT_CLIENT_EVENT_CALLBACK");
/* if (ble_client_timer && TRANS_MULTI_BLE_MASTER_NUMS == 1) { */
/* sys_timeout_del(ble_client_timer); */
/* ble_client_write_handle = 0; */
/* ble_client_timer = 0; */
/* } */
break;
default:
break;
}
}