1.目的
学习nrf51822一个主从一体,并且连接8个从机,手机连接S130设备,控制其他8个设备。
2.分析
学习nrf51822主机和从机通信,
3.平台:
协议栈版本:SDK10.0.0
编译软件:keil 5.12
硬件平台:nrf51822最小系统
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4 从机例子
SDK10.0\examples\ble_central_and_peripheral\experimental\ble_app_hrs_rscs_relay\pca10028\s130\arm4 做主机
4步骤.
首先把主从一体的设备里面的,hrs和rscs服务屏蔽掉。把串口服务移植进去。把ble_nus.c假如工程
并假如对应的路径 和 头文件
在main文件添加如下代码:
static ble_nus_t m_nus; /**< Structure to identify the Nordic UART Service. */ static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
/**@snippet [Handling the data received over BLE] */ static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { // if(p_data[0] == 0x55) // { // Syn_gap_params_init(); // } uint8_t data_array; if(p_data[1]) { if(m_conn_handle_nus_c[1] != BLE_CONN_HANDLE_INVALID) { data_array = 1 ; ble_nus_c_string_send(&m_ble_nus_c[1], &data_array, 1); }else { data_array = 0 ; ble_nus_c_string_send(&m_ble_nus_c[1], &data_array, 1); } } }
/**@brief Function for initializing services that will be used by the application. */ static void services_init(void) { uint32_t err_code; ble_nus_init_t nus_init; 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); }
static void ble_evt_dispatch(ble_evt_t * p_ble_evt) { uint16_t conn_handle; uint16_t role; /** The Connection state module has to be fed BLE events in order to function correctly * Remember to call ble_conn_state_on_ble_evt before calling any ble_conns_state_* functions. */ ble_conn_state_on_ble_evt(p_ble_evt); pm_ble_evt_handler(p_ble_evt); // The connection handle should really be retrievable for any event type. conn_handle = p_ble_evt->evt.gap_evt.conn_handle; role = ble_conn_state_role(conn_handle); // Based on the role this device plays in the connection, dispatch to the right applications. if (role == BLE_GAP_ROLE_PERIPH) { // Manages peripheral LEDs. on_ble_peripheral_evt(p_ble_evt); ble_advertising_on_ble_evt(p_ble_evt); ble_conn_params_on_ble_evt(p_ble_evt); // Dispatch to peripheral applications. ble_nus_on_ble_evt (&m_nus, p_ble_evt); // ble_hrs_on_ble_evt (&m_hrs, p_ble_evt); // ble_rscs_on_ble_evt(&m_rscs, p_ble_evt); } else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT)) { /** on_ble_central_evt will update the connection handles, so we want to execute it * after dispatching to the central applications upon disconnection. */ if (p_ble_evt->header.evt_id != BLE_GAP_EVT_DISCONNECTED) { on_ble_central_evt(p_ble_evt); } // if (conn_handle == m_conn_handle_hrs_c) // { // ble_hrs_c_on_ble_evt(&m_ble_hrs_c, p_ble_evt); // ble_db_discovery_on_ble_evt(&m_ble_db_discovery_hrs, p_ble_evt); // } // else if (conn_handle == m_conn_handle_rscs_c) // { // ble_rscs_c_on_ble_evt(&m_ble_rsc_c, p_ble_evt); // ble_db_discovery_on_ble_evt(&m_ble_db_discovery_rsc, p_ble_evt); // } for(uint8_t i = 0;i<8;i++) { if (conn_handle == m_conn_handle_nus_c[i]) { ble_nus_c_on_ble_evt(&m_ble_nus_c[i], p_ble_evt); ble_db_discovery_on_ble_evt(&m_ble_db_discovery_nus[i], p_ble_evt); } } // If the peer disconnected, we update the connection handles last. if (p_ble_evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED) { on_ble_central_evt(p_ble_evt); } } }
用lightblue 连接,现在显示的是串口的服务了。。。。