nRF 蓝牙一从对应多主

一从对多主例程路径:examples\ble_peripheral\experimental\ble_app_multiperipheral\pca10040\s132\arm5_no_packs

 

 

SDK15.3:

与普通流程不同:

一从对多主在连接时可同时存在n个连接句柄(conn_handle),具体多少个,需要看NRF_SDH_BLE_PERIPHERAL_LINK_COUNT 与 NRF_SDH_BLE_TOTAL_LINK_COUNT的数值,在通讯过程中使用到QWR 队列写模块实现与多个主之间的通讯

 

在连接时会自带一个连接句柄,连接句柄具体值不固定在0到n-1之间变化,断开后则变成0xFF

 

nus服务通讯过程中:

ble_nus_evt_t --> conn_handle也带有连接句柄参数,可与建立连接时的连接句柄对应上,也可用这个连接句柄做到,回复对应哪个主的操作

 

 

需要注意的几点:

ble_stack_init --> ble_evt_handler --> BLE_GAP_EVT_CONNECTED and BLE_GAP_EVT_DISCONNECTED

 

gatt_init --> 一对一:

err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
APP_ERROR_CHECK(err_code);

err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
APP_ERROR_CHECK(err_code);

 

一从对多主:

ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
APP_ERROR_CHECK(err_code);

 

services_init --> 一对一:

// Initialize Queued Write Module.
qwr_init.error_handler = nrf_qwr_error_handler;

err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
APP_ERROR_CHECK(err_code);

// Initialize NUS.
memset(&nus_init, 0, sizeof(nus_init));

nus_init.data_handler = nus_data_handler;

err_code = ble_nus_init(&m_nus, &nus_init);
APP_ERROR_CHECK(err_code);

 

一从对多主:

ret_code_t err_code;

nrf_ble_qwr_init_t qwr_init = {0};

// Initialize Queued Write Module instances.
qwr_init.error_handler = nrf_qwr_error_handler;

for (uint32_t i = 0; i < LINK_TOTAL; i++)
{
    err_code = nrf_ble_qwr_init(&m_qwr[i], &qwr_init);
    APP_ERROR_CHECK(err_code);
}


uint32_t err_code;

ble_nus_init_t nus_init;

// Initialize NUS.
memset(&nus_init, 0, sizeof(nus_init));

nus_init.data_handler = nus_data_handler;

err_code = ble_nus_init(&m_nus, &nus_init);
APP_ERROR_CHECK(err_code);

ble_conn_state_init();

 

conn_params_init --> 一对一:

uint32_t err_code;

ble_conn_params_init_t cp_init;

memset(&cp_init, 0, sizeof(cp_init));

cp_init.p_conn_params = NULL;
cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
cp_init.disconnect_on_fail = false;
cp_init.evt_handler = on_conn_params_evt;
cp_init.error_handler = conn_params_error_handler;

err_code = ble_conn_params_init(&cp_init);
APP_ERROR_CHECK(err_code);

一从对多主:

ret_code_t err_code;

ble_conn_params_init_t cp_init;

memset(&cp_init, 0, sizeof(cp_init));

cp_init.p_conn_params = NULL;
cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
cp_init.disconnect_on_fail = true;
cp_init.error_handler = conn_params_error_handler;

err_code = ble_conn_params_init(&cp_init);
APP_ERROR_CHECK(err_code);

具体操作流程,参考 16-享钥-wiegand-可多主连接 项目

 

你可能感兴趣的:(nRF 蓝牙一从对应多主)