基于 NORDIC softDevice 蓝牙协议栈 S132
蓝牙角色: 主机,ble_central
应用主函数 main() 中必须的函数:
ble_stack_init();
gatt_init();
db_discovery_init();
scan_start();
int main(void)
{
// Initialize.
log_init();
timer_init();
leds_init();
buttons_init();
power_management_init();
ble_stack_init();
gatt_init();
db_discovery_init();
lbs_c_init();
// Start execution.
NRF_LOG_INFO("Blinky CENTRAL example started.");
scan_start();
// Turn on the LED to signal scanning.
bsp_board_led_on(CENTRAL_SCANNING_LED);
// Enter main loop.
for (;;)
{
idle_state_handle();
}
}
ble_stack_init()
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL); 语句中, ble_evt_handler 为 BLE 事件回调函数 (handler for BLE events).
/**@brief Function for initializing the BLE stack.
* @details Initializes the SoftDevice and the BLE event interrupts.
*/
static void ble_stack_init(void)
{
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}
BLE 事件回调函数,ble_evt_handler
/**@brief Function for handling BLE events.
* @param[in] p_ble_evt Bluetooth stack event.
* @param[in] p_context Unused.
*/
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
ret_code_t err_code;
// For readability.
ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
switch (p_ble_evt->header.evt_id)
{
// Upon connection, check which peripheral has connected (HR or RSC), initiate DB
// discovery, update LEDs status and resume scanning if necessary. */
case BLE_GAP_EVT_CONNECTED:
{
NRF_LOG_INFO("Connected.");
err_code = ble_lbs_c_handles_assign(&m_ble_lbs_c, p_gap_evt->conn_handle, NULL);
APP_ERROR_CHECK(err_code);
err_code = ble_db_discovery_start(&m_db_disc, p_gap_evt->conn_handle);
APP_ERROR_CHECK(err_code);
// Update LEDs status, and check if we should be looking for more
// peripherals to connect to.
bsp_board_led_on(CENTRAL_CONNECTED_LED);
bsp_board_led_off(CENTRAL_SCANNING_LED);
} break;
// Upon disconnection, reset the connection handle of the peer which disconnected, update
// the LEDs status and start scanning again.
case BLE_GAP_EVT_DISCONNECTED:
{
NRF_LOG_INFO("Disconnected.");
scan_start();
} break;
case BLE_GAP_EVT_ADV_REPORT:
{
on_adv_report(&p_gap_evt->params.adv_report);
} break;
case BLE_GAP_EVT_TIMEOUT:
{
// We have not specified a timeout for scanning, so only connection attemps can timeout.
if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
{
NRF_LOG_DEBUG("Connection request timed out.");
}
} break;
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
{
// Accept parameters requested by peer.
err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
&p_gap_evt->params.conn_param_update_request.conn_params);
APP_ERROR_CHECK(err_code);
} break;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
{
NRF_LOG_DEBUG("PHY update request.");
ble_gap_phys_t const phys =
{
.rx_phys = BLE_GAP_PHY_AUTO,
.tx_phys = BLE_GAP_PHY_AUTO,
};
err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
APP_ERROR_CHECK(err_code);
} break;
case BLE_GATTC_EVT_TIMEOUT:
{
// Disconnect on GATT Client timeout event.
NRF_LOG_DEBUG("GATT Client Timeout.");
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
} break;
case BLE_GATTS_EVT_TIMEOUT:
{
// Disconnect on GATT Server timeout event.
NRF_LOG_DEBUG("GATT Server Timeout.");
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
} break;
default:
// No implementation needed.
break;
}
}
ble_evt_handler 回调函数中,事件 BLE_GAP_EVT_ADV_REPORT 处理函数 on_adv_report(), 用于匹配的蓝牙联结;
/**@brief Function for handling the advertising report BLE event.
*
* @param[in] p_adv_report Advertising report from the SoftDevice.
*/
static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
{
ret_code_t err_code;
if (ble_advdata_name_find(p_adv_report->data.p_data,
p_adv_report->data.len,
m_target_periph_name))
{
// Name is a match, initiate connection.
err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,
&m_scan_params,
&m_connection_param,
APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
}
else
{
err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer);
APP_ERROR_CHECK(err_code);
}
}
BLE GAP 事件列表 (GAP Event IDs.); (BLE 事件回调函数 ble_evt_handler 中常处理的事件)
enum BLE_GAP_EVTS |
GAP Event IDs. IDs that uniquely identify an event coming from the stack to the application.
Enumerator | |
---|---|
BLE_GAP_EVT_CONNECTED | GAP BLE Event base. |
BLE_GAP_EVT_DISCONNECTED | GAP BLE Event base. Disconnected from peer. |
BLE_GAP_EVT_CONN_PARAM_UPDATE | GAP BLE Event base. Connection Parameters updated. |
BLE_GAP_EVT_SEC_PARAMS_REQUEST | GAP BLE Event base. Request to provide security parameters. |
BLE_GAP_EVT_SEC_INFO_REQUEST | GAP BLE Event base. Request to provide security information. |
BLE_GAP_EVT_PASSKEY_DISPLAY | GAP BLE Event base. Request to display a passkey to the user. |
BLE_GAP_EVT_KEY_PRESSED | GAP BLE Event base. Notification of a keypress on the remote device. |
BLE_GAP_EVT_AUTH_KEY_REQUEST | GAP BLE Event base. Request to provide an authentication key. |
BLE_GAP_EVT_LESC_DHKEY_REQUEST | GAP BLE Event base. Request to calculate an LE Secure Connections DHKey. |
BLE_GAP_EVT_AUTH_STATUS | GAP BLE Event base. Authentication procedure completed with status. |
BLE_GAP_EVT_CONN_SEC_UPDATE | GAP BLE Event base. Connection security updated. |
BLE_GAP_EVT_TIMEOUT | GAP BLE Event base. Timeout expired. |
BLE_GAP_EVT_RSSI_CHANGED | GAP BLE Event base. RSSI report. |
BLE_GAP_EVT_ADV_REPORT | GAP BLE Event base. Advertising report. |
BLE_GAP_EVT_SEC_REQUEST | GAP BLE Event base. Security Request. |
BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST | GAP BLE Event base. Connection Parameter Update Request. |
BLE_GAP_EVT_SCAN_REQ_REPORT | GAP BLE Event base. Scan request report. |
BLE_GAP_EVT_PHY_UPDATE_REQUEST | GAP BLE Event base. PHY Update Request. |
BLE_GAP_EVT_PHY_UPDATE | GAP BLE Event base. PHY Update Procedure is complete. |
BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST | GAP BLE Event base. Data Length Update Request. |
BLE_GAP_EVT_DATA_LENGTH_UPDATE | GAP BLE Event base. LL Data Channel PDU payload length updated. |
BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT | GAP BLE Event base. Channel survey report. |
BLE_GAP_EVT_ADV_SET_TERMINATED | GAP BLE Event base. Advertising set terminated. |
BLE GATT Server 事件列表 (GATT Server Event IDs.)(BLE 事件回调函数 ble_evt_handler 中常处理的事件)
enum BLE_GATTS_EVTS |
GATT Server Event IDs.
Enumerator | |
---|---|
BLE_GATTS_EVT_WRITE | GATTS BLE Event base. Write operation performed. |
BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST | Read/Write Authorization request. |
BLE_GATTS_EVT_SYS_ATTR_MISSING | A persistent system attribute access is pending. |
BLE_GATTS_EVT_HVC | Handle Value Confirmation. |
BLE_GATTS_EVT_SC_CONFIRM | Service Changed Confirmation. |
BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST | Exchange MTU Request. |
BLE_GATTS_EVT_TIMEOUT | Peer failed to respond to an ATT request in time. |
BLE_GATTS_EVT_HVN_TX_COMPLETE | Handle Value Notification transmission complete. |
BLE GATT Client 事件列表 (GATT Client Event IDs.)(BLE 事件回调函数 ble_evt_handler 中常处理的事件)
enum BLE_GATTC_EVTS |
GATT Client Event IDs.
Enumerator | |
---|---|
BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP | GATTC BLE Event base. Primary Service Discovery Response event. |
BLE_GATTC_EVT_REL_DISC_RSP | Relationship Discovery Response event. |
BLE_GATTC_EVT_CHAR_DISC_RSP | Characteristic Discovery Response event. |
BLE_GATTC_EVT_DESC_DISC_RSP | Descriptor Discovery Response event. |
BLE_GATTC_EVT_ATTR_INFO_DISC_RSP | Attribute Information Response event. |
BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP | Read By UUID Response event. |
BLE_GATTC_EVT_READ_RSP | Read Response event. |
BLE_GATTC_EVT_CHAR_VALS_READ_RSP | Read multiple Response event. |
BLE_GATTC_EVT_WRITE_RSP | Write Response event. |
BLE_GATTC_EVT_HVX | Handle Value Notification or Indication event. |
BLE_GATTC_EVT_EXCHANGE_MTU_RSP | Exchange MTU Response event. |
BLE_GATTC_EVT_TIMEOUT | Timeout event. |
BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE | Write without Response transmission complete. |
gatt_init()
/**@brief Function for initializing the GATT module.
*/
static void gatt_init(void)
{
ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
APP_ERROR_CHECK(err_code);
}
db_discovery_init()
/**@brief Database discovery initialization.
*/
static void db_discovery_init(void)
{
ret_code_t err_code = ble_db_discovery_init(db_disc_handler);
APP_ERROR_CHECK(err_code);
}
scan_start()
/**@brief Function to start scanning.
*/
static void scan_start(void)
{
ret_code_t err_code;
(void) sd_ble_gap_scan_stop();
err_code = sd_ble_gap_scan_start(&m_scan_params, &m_scan_buffer);
APP_ERROR_CHECK(err_code);
bsp_board_led_off(CENTRAL_CONNECTED_LED);
bsp_board_led_on(CENTRAL_SCANNING_LED);
}