#include "ble_comm.h" #include "app_flash.h" #include "system.h" #if USENAMEFR char Target_scan[TARFET_LEN_MAX] = "SH_E9F4"; #else char Target_scan[TARFET_LEN_MAX] = {0x01, 0xf9, 0x84, 0x6a, 0x83, 0xeb}; #endif static Ble_receive_handler_t Rec_h = NULL; #define APP_BLE_OBSERVER_PRIO 3 /**< BLE observer priority of the application. There is no need to modify this value. */ #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */ #define ECHOBACK_BLE_UART_DATA 1 /**< Echo the UART data that is received over the Nordic UART Service (NUS) back to the sender. */ BLE_NUS_C_DEF(m_ble_nus_c); /**< BLE Nordic UART Service (NUS) client instance. */ NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */ BLE_DB_DISCOVERY_DEF(m_db_disc); /**< Database discovery module instance. */ NRF_BLE_SCAN_DEF(m_scan); /**< Scanning Module instance. */ NRF_BLE_GQ_DEF(m_ble_gatt_queue, /**< BLE GATT Queue instance. */ NRF_SDH_BLE_CENTRAL_LINK_COUNT, NRF_BLE_GQ_QUEUE_SIZE); static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */ unsigned char connect_to_server = 0; ble_gap_conn_params_t host_conn_params = {0}; static void db_disc_handler(ble_db_discovery_evt_t *p_evt) { ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt); } void timer_init(void) { ret_code_t err_code = app_timer_init(); APP_ERROR_CHECK(err_code); } static void db_discovery_init(void) { ble_db_discovery_init_t db_init; memset(&db_init, 0, sizeof(ble_db_discovery_init_t)); db_init.evt_handler = db_disc_handler; db_init.p_gatt_queue = &m_ble_gatt_queue; ret_code_t err_code = ble_db_discovery_init(&db_init); APP_ERROR_CHECK(err_code); } void power_management_init(void) { ret_code_t err_code; err_code = nrf_pwr_mgmt_init(); APP_ERROR_CHECK(err_code); } static void gatt_evt_handler(nrf_ble_gatt_t *p_gatt, nrf_ble_gatt_evt_t const *p_evt) { if (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED) { BLE_PRINT("GATT MTU exchange completed.\r\r\n"); m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH; BLE_PRINT("Ble NUS max data length set to 0x%X(%d)\r\r\n", m_ble_nus_max_data_len, m_ble_nus_max_data_len); } } void gatt_init(void) { ret_code_t err_code; err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler); APP_ERROR_CHECK(err_code); err_code = nrf_ble_gatt_att_mtu_central_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE); 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); } void scan_start(void) { ret_code_t ret; if(connect_to_server)return; ret = nrf_ble_scan_start(&m_scan); APP_ERROR_CHECK(ret); BLE_PRINT("scan_start -> scan_name <%s> \r\n", Target_scan); } void ST_scan_stop(void) { nrf_ble_scan_stop(); BLE_PRINT("ST_scan_stop \r\n"); } unsigned int ST_scan_start(void) { ret_code_t ret; ret = nrf_ble_scan_start(&m_scan); APP_ERROR_CHECK(ret); BLE_PRINT("ST_scan_start -> scan_name <%s> \r\n", Target_scan); if(ret != APP_SUCCESS) { return APP_ERROR_RESOURCES; } return APP_SUCCESS; } int wireless_pair_scan_start(int active) { ret_code_t ret; nrf_ble_scan_stop(); if(active == 0x01) { //在时间受限的应用程序中,当用户需要尽快接收数据时,确保扫描窗口大于广告间隔+10ms,以确保发现。 //(假设没有干扰,则10ms额外占每个广告事件之间0 ms到10ms的伪随机延迟。)遵循此规则将增加在第一次扫描时接收广告包的机会。 m_scan.scan_params.interval = 160; m_scan.scan_params.window = 112; m_scan.scan_params.active = 1; } else if(active == 0x02) { m_scan.scan_params.interval = 80; m_scan.scan_params.window = 48; m_scan.scan_params.active = 0; } else { m_scan.scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL; m_scan.scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW; m_scan.scan_params.active = 0; } ret = nrf_ble_scan_start(&m_scan); if(ret != NRF_SUCCESS)return -1; return 0; } int wireless_pair_scan_get_active(void) { return m_scan.scan_params.active; } static void ble_nus_c_evt_handler(ble_nus_c_t *p_ble_nus_c, ble_nus_c_evt_t const *p_ble_nus_evt) { ret_code_t err_code; switch (p_ble_nus_evt->evt_type) { case BLE_NUS_C_EVT_DISCOVERY_COMPLETE: BLE_PRINT("Discovery complete.\r\n"); err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles); APP_ERROR_CHECK(err_code); err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c); APP_ERROR_CHECK(err_code); BLE_PRINT("Connected to device with Nordic UART Service.\r\n"); break; case BLE_NUS_C_EVT_NUS_TX_EVT: //作为主机接收从机的数据 Rec_h(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len); break; case BLE_NUS_C_EVT_DISCONNECTED: BLE_PRINT("Disconnected.\r\n"); #if DEBUGBLE scan_start(); #endif break; } } static void nus_error_handler(uint32_t nrf_error) { if (nrf_error == NRF_ERROR_RESOURCES) return; if (nrf_error == NRF_ERROR_INVALID_STATE) return; APP_ERROR_HANDLER(nrf_error); } static void nus_c_init(void) { ret_code_t err_code; ble_nus_c_init_t init; init.evt_handler = ble_nus_c_evt_handler; init.error_handler = nus_error_handler; init.p_gatt_queue = &m_ble_gatt_queue; err_code = ble_nus_c_init(&m_ble_nus_c, &init); APP_ERROR_CHECK(err_code); } #define host_connected_evt_num_max 3 static uint8_t host_connected_evt_num = 0; static Ble_evt_cb ble_host_connected_evt_cb[host_connected_evt_num_max] = {0}; int Ble_Host_Connectd_Evt_Regist(Ble_evt_cb cb) { for (int i = 0; i < host_connected_evt_num_max; i++) { if (ble_host_connected_evt_cb[i] == cb) return -1; if (ble_host_connected_evt_cb[i] == 0) { host_connected_evt_num++; ble_host_connected_evt_cb[i] = cb; //回调函数 return 0; } } DEBUG_LOG( "ble_evt_Regist -> too many!\n"); return -2; } void ble_host_connected_evt_pcs(void) { for (int i = 0; i < host_connected_evt_num; i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]); if (ble_host_connected_evt_cb[i]) { ble_host_connected_evt_cb[i](); //回调函数 } } } #define host_disconn_evt_num_max 16 static uint8_t host_disconn_evt_num = 0; static Ble_evt_cb ble_Host_disconn_evt_cb[host_disconn_evt_num_max] = {0}; int Ble_Host_Disconn_Evt_Regist(Ble_evt_cb cb) { for (int i = 0; i < host_disconn_evt_num_max; i++) { if (ble_Host_disconn_evt_cb[i] == cb) return -1; if (ble_Host_disconn_evt_cb[i] == 0) { host_disconn_evt_num++; ble_Host_disconn_evt_cb[i] = cb; //回调函数 return 0; } } DEBUG_LOG( "Ble_Slave_Disconn_Evt_Regist -> too many!\r\n"); return -2; } void ble_host_dicconn_evt_pcs(void) { for (int i = 0; i < host_disconn_evt_num; i++) { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]); if (ble_Host_disconn_evt_cb[i]) { ble_Host_disconn_evt_cb[i](); //回调函数 } } } bool advdata_name_find(uint8_t const * p_encoded_data, uint16_t data_len, char const * p_target_name) { return ble_advdata_name_find(p_encoded_data,data_len,p_target_name); } bool advdata_short_name_find(uint8_t const * p_encoded_data, uint16_t data_len, char const * p_target_name, uint8_t const short_name_min_len) { return ble_advdata_short_name_find(p_encoded_data,data_len,p_target_name,short_name_min_len); } #if BleNameHoldOn_ENANBLE uint8_t SaveFlashFlag_holdOn =0; #endif advdata_rep_handler_t _advdata_rep_callback = NULL; void advdata_report_Evt_Regist(advdata_rep_handler_t handler) { if(handler==NULL) { BLE_PRINT("advdata_report_Evt_Regist ERROR\r\n"); } else { BLE_PRINT("advdata_report_Evt_Regist SUCCESS\r\n"); _advdata_rep_callback=handler; } } static void on_ble_central_evt(ble_evt_t const *p_ble_evt, void *p_context) //作为主设备时的处理 { ret_code_t err_code; ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: err_code = ble_nus_c_handles_assign(&m_ble_nus_c, p_ble_evt->evt.gap_evt.conn_handle, NULL); APP_ERROR_CHECK(err_code); BLE_PRINT("start discovery services\r\n"); //添加开始发现服务提示 connect_to_server = 1; // start discovery of services. The NUS Client waits for a discovery result err_code = ble_db_discovery_start(&m_db_disc, p_ble_evt->evt.gap_evt.conn_handle); APP_ERROR_CHECK(err_code); ble_host_connected_evt_pcs(); #if BleNameHoldOn_ENANBLE if(mFlash.isHost >0){ uint8_t i =0; for(i=0;ievt.gap_evt.params.connected.peer_addr.addr[5-i]; } SaveFlashFlag_holdOn = 1; } } #endif sd_ble_gap_rssi_start(m_ble_nus_c.conn_handle, BLE_GAP_RSSI_THRESHOLD_INVALID, 0); break; case BLE_GAP_EVT_DISCONNECTED: connect_to_server = 0; BLE_PRINT("Disconnected. conn_handle: 0x%x, reason: 0x%x", p_gap_evt->conn_handle, p_gap_evt->params.disconnected.reason); BLE_PRINT("Disconnected to Server.\r\n"); ble_host_dicconn_evt_pcs(); sd_ble_gap_rssi_stop(m_ble_nus_c.conn_handle); break; case BLE_GAP_EVT_TIMEOUT: if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN) { BLE_PRINT("Connection Request timed out.\r\n"); } break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: // Pairing not supported. err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL); APP_ERROR_CHECK(err_code); BLE_PRINT("on_ble_central_evt -> BLE_GAP_EVT_SEC_PARAMS_REQUEST\r\n"); break; case BLE_GAP_EVT_PHY_UPDATE_REQUEST: { BLE_PRINT("Connection 0x%x PHY update request.", p_ble_evt->evt.gap_evt.conn_handle); 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_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: // Accepting 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); BLE_PRINT("on_ble_central_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST\r\n"); break; case BLE_GAP_EVT_CONN_PARAM_UPDATE: BLE_PRINT("on_ble_central_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE\r\n"); memcpy(&host_conn_params, &p_gap_evt->params.conn_param_update_request.conn_params, sizeof(ble_gap_conn_params_t)); BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval); BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval); BLE_PRINT("slave_latency : %d\r\n", p_gap_evt->params.conn_param_update_request.conn_params.slave_latency); BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout); break; case BLE_GATTC_EVT_TIMEOUT: // Disconnect on GATT Client timeout event. BLE_PRINT("GATT Client Timeout.\r\n"); 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. BLE_PRINT("GATT Server Timeout.\r\n"); 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; case BLE_GAP_EVT_ADV_REPORT: { if(p_gap_evt->params.adv_report.type.scan_response == 0) { if(_advdata_rep_callback != NULL) { _advdata_rep_callback(p_gap_evt->params.adv_report.data.p_data , p_gap_evt->params.adv_report.data.len ,p_gap_evt->params.adv_report.rssi); } // unsigned short parsed_name_len; // uint16_t offset = 0; // parsed_name_len = ble_advdata_search(p_gap_evt->params.adv_report.data.p_data,p_gap_evt->params.adv_report.data.len,&offset,BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME); // if(parsed_name_len>0) // { // printf( " RSSI:%d \tNAME :",p_gap_evt->params.adv_report.rssi); // for(int k=0;kparams.adv_report.data.p_data[k+offset] ); // } // printf( "\r\n"); // } } } break; default: break; } } static uint8_t connect_macaddr[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; uint8_t* wireless_pair_get_connect_macaddr(void){return connect_macaddr;} extern int app_wireless_pair_Scan_Req(int rssi, uint8_t *addr); extern int app_wireless_pair_Scan_Rsp(int rssi, uint8_t *addr); extern int app_wireless_pair_start_led(void); extern int app_wireless_pair_stop_led(void); extern bool ble_evt_is_advertising_timeout(ble_evt_t const *p_ble_evt); extern void on_ble_peripheral_evt(ble_evt_t const *p_ble_evt); //作为从设备的处理 static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) { uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle; uint16_t role = ble_conn_state_role(conn_handle); ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt; ble_gap_evt_connected_t const * p_connected_evt = &p_gap_evt->params.connected; switch(p_ble_evt->header.evt_id) { case BLE_GAP_EVT_SCAN_REQ_REPORT: app_wireless_pair_Scan_Req(p_gap_evt->params.scan_req_report.rssi,(uint8_t*)&p_gap_evt->params.scan_req_report.peer_addr.addr[0]); break; case BLE_GAP_EVT_ADV_REPORT: if(p_gap_evt->params.adv_report.type.scan_response != 0) { if(p_gap_evt->params.adv_report.data.len > 31)break; //解析扫描回应包 for(int i=1; iparams.adv_report.data.len;) { if(p_gap_evt->params.adv_report.data.p_data[i] != BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA) { i = i - 1 + p_gap_evt->params.adv_report.data.p_data[i-1] + 2; } else { uint8_t addr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; memcpy(addr,(uint8_t*)&p_gap_evt->params.adv_report.data.p_data[i+3],6);//拷贝原因:避免被协议栈覆盖。 app_wireless_pair_Scan_Rsp(p_gap_evt->params.adv_report.rssi,addr); break; } } } break; case BLE_GAP_EVT_CONNECTED: for(int i=0;i<6;i++)connect_macaddr[i] = p_connected_evt->peer_addr.addr[5-i]; app_wireless_pair_start_led(); break; case BLE_GAP_EVT_DISCONNECTED: app_wireless_pair_stop_led(); break; } // Based on the role this device plays in the connection, dispatch to the right handler.不同角色下所做的处理 if (role == BLE_GAP_ROLE_PERIPH || ble_evt_is_advertising_timeout(p_ble_evt)) { // ble_nus_on_ble_evt(p_ble_evt, &m_nus); on_ble_peripheral_evt(p_ble_evt); } else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT)) { // ble_nus_c_on_ble_evt(p_ble_evt, &m_ble_nus_c); on_ble_central_evt(p_ble_evt, NULL); } else { BLE_PRINT("ble_evt_handler -> other\r\n"); } } 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); } #define PRINT_MAC 0 /**@brief Function for handling Scanning Module events. */ static void scan_evt_handler(scan_evt_t const *p_scan_evt) { switch (p_scan_evt->scan_evt_id) { case NRF_BLE_SCAN_EVT_CONNECTING_ERROR: { // ret_code_t err_code = p_scan_evt->params.connecting_err.err_code; // APP_ERROR_CHECK(err_code); BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_CONNECTING_ERROR \r\n"); } break; case NRF_BLE_SCAN_EVT_CONNECTED: { BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_CONNECTED\r\n"); #if PRINT_MAC ble_gap_evt_connected_t const *p_connected = p_scan_evt->params.connected.p_connected; // Scan is automatically stopped by the connection. BLE_PRINT("Connecting to Host %02x%02x %02x%02x %02x%02x\r\r\n", p_connected->peer_addr.addr[0], p_connected->peer_addr.addr[1], p_connected->peer_addr.addr[2], p_connected->peer_addr.addr[3], p_connected->peer_addr.addr[4], p_connected->peer_addr.addr[5]); #endif } break; case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT: { BLE_PRINT("scan_evt_handler -> NRF_BLE_SCAN_EVT_SCAN_TIMEOUT -> Scan timed out.\r\n"); scan_start(); } break; } } unsigned int send_bytes_server(uint8_t *bytes, uint16_t len) { if (connect_to_server == 0) { BLE_PRINT("send_bytes_server -> APP_ERR_DISCONN\r\n"); return APP_ERR_DISCONN; } if (len > m_ble_nus_max_data_len) { BLE_PRINT("send_bytes_server -> fail ->overlength\r\n"); return APP_ERR_OVERLENGTH; } if (NRF_SUCCESS != ble_nus_c_string_send(&m_ble_nus_c, bytes, len)) { BLE_PRINT("send_bytes_server -> fail\r\n"); } return APP_SUCCESS; } static void scan_init(void) { ret_code_t err_code; nrf_ble_scan_init_t init_scan; memset(&init_scan, 0, sizeof(init_scan)); init_scan.connect_if_match = true; init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG; err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler); APP_ERROR_CHECK(err_code); #if USENAMEFR err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, &Target_scan); APP_ERROR_CHECK(err_code); err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false); APP_ERROR_CHECK(err_code); BLE_PRINT("scan_init -> scan_name <%s> \r\n", Target_scan); #else err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, &Target_scan); APP_ERROR_CHECK(err_code); err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false); APP_ERROR_CHECK(err_code); BLE_PRINT("scan_start -> scan_MAC [ %02X %02X %02X %02X %02X %02X ]\r\n", Target_scan[0], Target_scan[1], Target_scan[2], Target_scan[3], Target_scan[4], Target_scan[5]); #endif } unsigned char host_isconnect(void) { return connect_to_server; } char ble_stack_init_sta = 1; char host_init_sta = 0; void host_init(Ble_receive_handler_t receive_handler) { if (receive_handler == NULL) { BLE_PRINT("host_init -> param err \r\n"); return; } Rec_h = receive_handler; if (ble_stack_init_sta) { timer_init(); // power_management_init(); // ble_stack_init(); // gatt_init(); // ble_stack_init_sta = 0; } db_discovery_init(); nus_c_init(); scan_init(); host_init_sta=1; } void err(int err_num) { BLE_PRINT("APP ERROR -> %d \r\n", err_num); } unsigned int host_set_scan_name(char *name, int len) { unsigned int err_code; if (len > TARFET_LEN_MAX) return APP_ERR_OVERLENGTH; if(connect_to_server) return APP_ERR_CONNECTED; memset(Target_scan, 0, TARFET_LEN_MAX); if(host_init_sta) { memcpy(Target_scan, name, len); err_code =nrf_ble_scan_all_filter_remove(&m_scan); APP_ERROR_CHECK(err_code); #if USENAMEFR err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, &Target_scan); APP_ERROR_CHECK(err_code); err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false); APP_ERROR_CHECK(err_code); BLE_PRINT("host_set_scan_name -> scan_name <%s> \r\n", Target_scan); #else err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, &Target_scan); APP_ERROR_CHECK(err_code); err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false); APP_ERROR_CHECK(err_code); BLE_PRINT("host_set_scan_name -> scan_MAC [ %02X %02X %02X %02X %02X %02X ]\r\n", Target_scan[0], Target_scan[1], Target_scan[2], Target_scan[3], Target_scan[4], Target_scan[5]); #endif } else { memcpy(Target_scan, name, len); } return APP_SUCCESS; } unsigned int Ble_update_conn_interval(float min_conn_interval, float max_conn_interval) { ret_code_t err_code; ble_gap_conn_params_t bgcp; //主机接受从机连接参数更新连接参数 if (connect_to_server) { if ((max_conn_interval > 1.25 * 1599) || (max_conn_interval < min_conn_interval)) return APP_ERR_PARAMERR; if (min_conn_interval < 7.5f) return APP_ERR_PARAMERR; bgcp.max_conn_interval = MSEC_TO_UNITS(max_conn_interval, UNIT_1_25_MS); bgcp.min_conn_interval = MSEC_TO_UNITS(min_conn_interval, UNIT_1_25_MS); bgcp.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS); bgcp.slave_latency = 0; err_code = sd_ble_gap_conn_param_update(m_ble_nus_c.conn_handle, &bgcp); if(err_code != NRF_ERROR_INVALID_STATE && err_code != NRF_ERROR_BUSY && err_code != NRF_SUCCESS){ APP_ERROR_CHECK(err_code); } return err_code; } else { return APP_ERR_DISCONN; } } void host_disconnect(void) { DEBUG_LOG("=======>sd_ble_gap_disconnect host\n"); if (connect_to_server) sd_ble_gap_disconnect(m_ble_nus_c.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); } void host_get_conn_params(ble_gap_conn_params_t *p) { memcpy(p, &host_conn_params, sizeof(ble_gap_conn_params_t)); } static signed char rssi = 0; signed char host_get_rssi(void) { unsigned char channel; if (connect_to_server == 0) return 1; sd_ble_gap_rssi_get(m_ble_nus_c.conn_handle, &rssi, &channel); ///BLE_PRINT("rssi= %d channel=%d\r\n", rssi, channel); return rssi; }