其餘部分連接:html
nRF52832藍牙iBeacon廣播windows
軟件都安裝完畢後,就能夠開始進行調試了。app
開發板經過SWD接口鏈接調試仿真,經過nrfgo官方工具燒寫藍牙協議棧(s132);函數
協議棧hex文件在sdk的路徑:nRF5_SDK_11.0.0_89a8197\components\softdevice\s132\hex.工具
先擦除,寫入s132協議棧,應用部分在keil上編程燒寫及調試。oop
2.NRF52832添加串口私有服務透傳實現post
int main(void) { uint32_t err_code; bool erase_bonds; // Initialize. //APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false); uart_init(); //串口硬件初始化 APP_LOG("\r\nNRF52832例程測試!\r\n"); timers_init(); //定時器建立 APP_LOG("\r\n應用定時器初始化!\r\n"); buttons_leds_init(&erase_bonds); //使用BSP驅動功能較完善,到時修改 APP_LOG("\r\nBSP按鍵、LED初始化!\r\n"); // leds_init(); // buttons_init(); ble_stack_init(); APP_LOG("\r\nBLE協議棧初始化!\r\n"); gap_params_init(); APP_LOG("\r\nGAP參數設置初始化:\r\n"); APP_LOG(" 最小鏈接間隔:20ms\r\n"); APP_LOG(" 最大鏈接間隔:75ms\r\n"); APP_LOG(" 從機延遲:0\r\n"); APP_LOG(" 鏈接超時:4s\r\n"); services_init(); //添加私有服務,須要初始化在廣播初始化以前 APP_LOG("\r\n添加私有服務\r\n"); advertising_init(); APP_LOG("\r\n廣播初始化\r\n"); conn_params_init(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); application_timers_start();//開始定時,時間間隔 // Enter main loop. for (;;) { power_manage(); } }
#include "ble_nus.h" //藍牙串口服務測試
同時工程中添加對應文件及引用路徑以下圖:
static void services_init(void) { uint32_t err_code; ble_nus_init_t nus_init; // ble_lbs_init_t lbs_init; memset(&nus_init, 0, sizeof(nus_init)); nus_init.data_handler = nus_data_handler; //藍牙接收數據串口處理 err_code = ble_nus_init(&m_nus, &nus_init); //添加nus的GATTS服務UUID APP_ERROR_CHECK(err_code); // memset(&lbs_init, 0, sizeof(lbs_init)); // lbs_init.led_write_handler = led_write_handler; // err_code = ble_lbs_init(&m_lbs, &lbs_init); //特性:按鍵字節、LED字節 // APP_ERROR_CHECK(err_code); }
services_init
nus_data_handler
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { for (uint32_t i = 0; i < length; i++) { while(app_uart_put(p_data[i]) != NRF_SUCCESS); //數據串口發送 } while(app_uart_put('\n') != NRF_SUCCESS); }
uart_init //串口初始化,註冊串口接收事件處理函數
uart_event_handle //串口數據接收處理函數
void uart_event_handle(app_uart_evt_t * p_event) { static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; //藍牙規範限長20字節 static uint8_t index = 0; uint32_t err_code; switch (p_event->evt_type) { case APP_UART_DATA_READY: UNUSED_VARIABLE(app_uart_get(&data_array[index])); index++; if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN))) { err_code = ble_nus_string_send(&m_nus, data_array, index); //藍牙無線發送 if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } index = 0; } break; case APP_UART_COMMUNICATION_ERROR: APP_ERROR_HANDLER(p_event->data.error_communication); break; case APP_UART_FIFO_ERROR: APP_ERROR_HANDLER(p_event->data.error_code); break; default: break; } } static void uart_init(void) { uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_DISABLED, //禁止硬件流控制 false, UART_BAUDRATE_BAUDRATE_Baud115200 }; APP_UART_FIFO_INIT( &comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_event_handle, //串口接收事件處理(串口接收-藍牙發送) APP_IRQ_PRIORITY_LOW, err_code); APP_ERROR_CHECK(err_code); }
私有服務UUID服務的添加在ble_nus_init中實現:
rx_char_add(p_nus, p_nus_init); //添加串口接收特徵字節
tx_char_add(p_nus, p_nus_init); //添加串口發送特徵
串口私有服務經過掃描響應的方式告訴主設備對應的服務UUID信息,在advertising_init()中添加掃描響應數據;
static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
3.調試
經過keil編譯下載程序到測試板中,經過手機BLE調試助手和PC的串口調試助手便可實現藍牙串口簡單的數據透傳。