1 /**@brief Function for initializing the Advertising functionality. 2 * 3 * @details Encodes the required advertising data and passes it to the stack. 4 * Also builds a structure to be passed to the stack when starting advertising. 5 */ 6 static void advertising_init(void) 7 { 8 uint32_t err_code; 9 ble_advdata_t advdata; 10 uint8_t flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; 11 12 ble_uuid_t adv_uuids[] = 13 { 14 {BLE_UUID_HEART_RATE_SERVICE, BLE_UUID_TYPE_BLE}, 15 {BLE_UUID_BATTERY_SERVICE, BLE_UUID_TYPE_BLE}, 16 {BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE} 17 };//广播工程蓝牙3个服务的uuid 18 19 // Build and set advertising data 20 memset(&advdata, 0, sizeof(advdata)); 21 22 advdata.name_type = BLE_ADVDATA_FULL_NAME; 23 advdata.include_appearance = true; 24 advdata.flags.size = sizeof(flags); 25 advdata.flags.p_data = &flags; 26 advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]); 27 advdata.uuids_complete.p_uuids = adv_uuids; 28 29 err_code = ble_advdata_set(&advdata, NULL); 30 APP_ERROR_CHECK(err_code); 31 32 // Initialize advertising parameters (used when starting advertising) 33 memset(&m_adv_params, 0, sizeof(m_adv_params)); 34 35 m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND; 36 m_adv_params.p_peer_addr = NULL; // Undirected advertisement 37 m_adv_params.fp = BLE_GAP_ADV_FP_ANY; 38 m_adv_params.interval = APP_ADV_INTERVAL; 39 m_adv_params.timeout = APP_ADV_TIMEOUT_IN_SECONDS; 40 }
1 /***************************************************************************** 2 * Static Event Handling Functions 3 *****************************************************************************/ 4 5 /**@brief Function for handling the Application's BLE Stack events. 6 * 7 * @param[in] p_ble_evt Bluetooth stack event. 8 */ 9 static void on_ble_evt(ble_evt_t *p_ble_evt) 10 { 11 uint32_t err_code; 12 static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; 13 14 switch (p_ble_evt->header.evt_id) 15 { 16 case BLE_GAP_EVT_CONNECTED: 17 led_stop(); 18 19 m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; 20 21 // Initialize the current heart rate to the average of max and min values. So that 22 // everytime a new connection is made, the heart rate starts from the same value. 23 // m_cur_heart_rate = 0;//(MAX_HEART_RATE + MIN_HEART_RATE) / 2; 24 // Start timers used to generate battery and HR measurements. 25 application_timers_start(); 26 27 // Start handling button presses 28 err_code = app_button_enable(); 29 APP_ERROR_CHECK(err_code); 30 break; 31 32 case BLE_GAP_EVT_DISCONNECTED: 33 // Since we are not in a connection and have not started advertising, store bonds 34 err_code = ble_bondmngr_bonded_centrals_store(); 35 APP_ERROR_CHECK(err_code); 36 37 // @note Flash access may not be complete on return of this API. System attributes are now 38 // stored to flash when they are updated to ensure flash access on disconnect does not 39 // result in system powering off before data was successfully written. 40 41 // Go to system-off mode, should not return from this function, wakeup will trigger 42 // a reset. 43 //system_off_mode_enter(); 44 app_button_disable(); 45 DispColor(BLACK); 46 //re-enter advertising model if disconnected 47 48 advertising_init(); 49 // Start advertising 50 advertising_start(); 51 break; 52 53 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: 54 err_code = sd_ble_gap_sec_params_reply(m_conn_handle, 55 BLE_GAP_SEC_STATUS_SUCCESS, 56 &m_sec_params); 57 APP_ERROR_CHECK(err_code); 58 break; 59 60 case BLE_GAP_EVT_TIMEOUT: 61 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) 62 { 63 led_stop(); 64 65 nrf_gpio_cfg_sense_input(SENSOR_BUTTON_PIN_RIGHT, 66 BUTTON_PULL, 67 NRF_GPIO_PIN_SENSE_LOW); 68 69 nrf_gpio_cfg_sense_input(SENSOR_BUTTON_PIN_LEFT, 70 BUTTON_PULL, 71 NRF_GPIO_PIN_SENSE_LOW); 72 #if defined(ENABLE_UP) 73 nrf_gpio_cfg_sense_input(SENSOR_BUTTON_PIN_UP, 74 BUTTON_PULL, 75 NRF_GPIO_PIN_SENSE_LOW); 76 77 78 nrf_gpio_cfg_sense_input(SENSOR_BUTTON_PIN_DOWN, 79 BUTTON_PULL, 80 NRF_GPIO_PIN_SENSE_LOW); 81 #endif 82 83 system_off_mode_enter(); 84 } 85 break; 86 87 default: 88 // No implementation needed. 89 break; 90 } 91 }
More:[蓝牙] 1、蓝牙核心技术了解(蓝牙协议、架构、硬件和软件笔记)
@beautifulzzzz 2015-12-11 continue~