低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析

代码实例:Board/pca100001/06 s110/experimental/ble_app_uart。

    实现的功能是从uart口发送数据至另一个蓝牙串口,或是从蓝牙读取数据通过uart打印出数据。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int main(void)  
  2. {  
  3.     // Initialize  
  4.     leds_init();  
  5.     timers_init();  
  6.     buttons_init();  
  7.     uart_init();  
  8.     ble_stack_init();  
  9.     gap_params_init();  
  10.     services_init();  
  11.     advertising_init();  
  12.     conn_params_init();  
  13.     sec_params_init();  
  14.       
  15.     simple_uart_putstring(START_STRING);  
  16.       
  17.     advertising_start();  
  18.       
  19.     // Enter main loop  
  20.     for (;;)  
  21.     {  
  22.         power_manage();  
  23.     }  
  24. }  

uart_init初始化uart的硬件。

当uart有数据时会进入uart中断处理:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief   Function for handling UART interrupts. 
  2.  * 
  3.  * @details This function will receive a single character from the UART and append it to a string. 
  4.  *          The string will be be sent over BLE when the last character received was a 'new line' 
  5.  *          i.e '\n' (hex 0x0D) or if the string has reached a length of @ref NUS_MAX_DATA_LENGTH. 
  6.  */  
  7. void UART0_IRQHandler(void)  
  8. {  
  9.     static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];  
  10.     static uint8_t index = 0;  
  11.     uint32_t err_code;  
  12.   
  13.     /**@snippet [Handling the data received over UART] */  
  14.   
  15.     data_array[index] = simple_uart_get();  
  16.     index++;  
  17.   
  18.     if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN - 1)))  
  19.     {  
  20.         err_code = ble_nus_send_string(&m_nus, data_array, index + 1);  
  21.         if (err_code != NRF_ERROR_INVALID_STATE)  
  22.         {  
  23.             APP_ERROR_CHECK(err_code);  
  24.         }  
  25.           
  26.         index = 0;  
  27.     }  
  28.   
  29.     /**@snippet [Handling the data received over UART] */  
  30. }  

data_array是数据缓冲区,当一个接收到换行符时认为是输入结束,或是达到缓冲区的最大字节数。

ble_nus_send_string函数即是把uart输入的数据通过蓝牙发送。

此实例中建立了一个服务:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for initializing services that will be used by the application. 
  2.  */  
  3. static void services_init(void)  
  4. {  
  5.     uint32_t         err_code;  
  6.     ble_nus_init_t   nus_init;  
  7.       
  8.     memset(&nus_init, 0, sizeof(nus_init));  
  9.   
  10.     nus_init.data_handler = nus_data_handler;  
  11.       
  12.     err_code = ble_nus_init(&m_nus, &nus_init);  
  13.     APP_ERROR_CHECK(err_code);  
  14. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. uint32_t ble_nus_init(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)  
  2. {  
  3.     uint32_t        err_code;  
  4.     ble_uuid_t      ble_uuid;  
  5.     ble_uuid128_t   nus_base_uuid = {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,  
  6.                                      0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E};  
  7.   
  8.     if ((p_nus == NULL) || (p_nus_init == NULL))  
  9.     {  
  10.         return NRF_ERROR_NULL;  
  11.     }  
  12.       
  13.     // Initialize service structure.  
  14.     p_nus->conn_handle              = BLE_CONN_HANDLE_INVALID;  
  15.     p_nus->data_handler             = p_nus_init->data_handler;  
  16.     p_nus->is_notification_enabled  = false;  
  17.       
  18.   
  19.     /**@snippet [Adding proprietary Service to S110 SoftDevice] */  
  20.   
  21.     // Add custom base UUID.  
  22.     err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);  
  23.     if (err_code != NRF_SUCCESS)  
  24.     {  
  25.         return err_code;  
  26.     }  
  27.   
  28.     ble_uuid.type = p_nus->uuid_type;  
  29.     ble_uuid.uuid = BLE_UUID_NUS_SERVICE;  
  30.   
  31.     // Add service.  
  32.     err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,  
  33.                                         &ble_uuid,  
  34.                                         &p_nus->service_handle);  
  35.     /**@snippet [Adding proprietary Service to S110 SoftDevice] */  
  36.     if (err_code != NRF_SUCCESS)  
  37.     {  
  38.         return err_code;  
  39.     }  
  40.       
  41.     // Add RX Characteristic.  
  42.     err_code = rx_char_add(p_nus, p_nus_init);  
  43.     if (err_code != NRF_SUCCESS)  
  44.     {  
  45.         return err_code;  
  46.     }  
  47.   
  48.     // Add TX Characteristic.  
  49.     err_code = tx_char_add(p_nus, p_nus_init);  
  50.     if (err_code != NRF_SUCCESS)  
  51.     {  
  52.         return err_code;  
  53.     }  
  54.       
  55.     return NRF_SUCCESS;  
  56. }  

rx_char_add()添加了接收特性,tx_char_add()添加了发送特性。

当从蓝牙接收到数据时,通过串口将数据打印出来:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief     Function for handling the @ref BLE_GATTS_EVT_WRITE event from the S110 SoftDevice. 
  2.  * 
  3.  * @param[in] p_dfu     Nordic UART Service structure. 
  4.  * @param[in] p_ble_evt Pointer to the event received from BLE stack. 
  5.  */  
  6. static void on_write(ble_nus_t * p_nus, ble_evt_t * p_ble_evt)  
  7. {  
  8.     ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;  
  9.       
  10.     if (  
  11.         (p_evt_write->handle == p_nus->rx_handles.cccd_handle)  
  12.         &&  
  13.         (p_evt_write->len == 2)  
  14.        )  
  15.     {  
  16.         if (ble_srv_is_notification_enabled(p_evt_write->data))  
  17.         {  
  18.             p_nus->is_notification_enabled = true;  
  19.         }  
  20.         else  
  21.         {  
  22.             p_nus->is_notification_enabled = false;  
  23.         }  
  24.     }  
  25.     else if (  
  26.              (p_evt_write->handle == p_nus->tx_handles.value_handle)  
  27.              &&  
  28.              (p_nus->data_handler != NULL)  
  29.             )  
  30.     {  
  31.         p_nus->data_handler(p_nus, p_evt_write->data, p_evt_write->len);  
  32.     }  
  33.     else  
  34.     {  
  35.         // Do Nothing. This event is not relevant to this service.  
  36.     }  
  37. }  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值