低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析
代码实例:点击打开链接
实现的功能是从uart口发送数据至另一个蓝牙串口,或是从蓝牙读取数据通过uart打印出数据。
int main(void) { // Initialize leds_init(); timers_init(); buttons_init(); uart_init(); ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); sec_params_init(); simple_uart_putstring(START_STRING); advertising_start(); // Enter main loop for (;;) { power_manage(); } }
uart_init初始化uart的硬件。
当uart有数据时会进入uart中断处理:
/**@brief Function for handling UART interrupts. * * @details This function will receive a single character from the UART and append it to a string. * The string will be be sent over BLE when the last character received was a 'new line' * i.e '\n' (hex 0x0D) or if the string has reached a length of @ref NUS_MAX_DATA_LENGTH. */ void UART0_IRQHandler(void) { static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; ; uint32_t err_code; /**@snippet [Handling the data received over UART] */ data_array[index] = simple_uart_get(); index++; ] == ))) { err_code = ble_nus_send_string(&m_nus, data_array, index + ); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } index = ; } /**@snippet [Handling the data received over UART] */ }
data_array是数据缓冲区,当一个接收到换行符时认为是输入结束,或是达到缓冲区的最大字节数。
ble_nus_send_string函数即是把uart输入的数据通过蓝牙发送。
此实例中建立了一个服务:
/**@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, , sizeof(nus_init)); nus_init.data_handler = nus_data_handler; err_code = ble_nus_init(&m_nus, &nus_init); APP_ERROR_CHECK(err_code); }
uint32_t ble_nus_init(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init) { uint32_t err_code; ble_uuid_t ble_uuid; ble_uuid128_t nus_base_uuid = {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E}; if ((p_nus == NULL) || (p_nus_init == NULL)) { return NRF_ERROR_NULL; } // Initialize service structure. p_nus->conn_handle = BLE_CONN_HANDLE_INVALID; p_nus->data_handler = p_nus_init->data_handler; p_nus->is_notification_enabled = false; /**@snippet [Adding proprietary Service to S110 SoftDevice] */ // Add custom base UUID. err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type); if (err_code != NRF_SUCCESS) { return err_code; } ble_uuid.type = p_nus->uuid_type; ble_uuid.uuid = BLE_UUID_NUS_SERVICE; // Add service. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_nus->service_handle); /**@snippet [Adding proprietary Service to S110 SoftDevice] */ if (err_code != NRF_SUCCESS) { return err_code; } // Add RX Characteristic. err_code = rx_char_add(p_nus, p_nus_init); if (err_code != NRF_SUCCESS) { return err_code; } // Add TX Characteristic. err_code = tx_char_add(p_nus, p_nus_init); if (err_code != NRF_SUCCESS) { return err_code; } return NRF_SUCCESS; }
rx_char_add()添加了接收特性,tx_char_add()添加了发送特性。
当从蓝牙接收到数据时,通过串口将数据打印出来:
/**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the S110 SoftDevice. * * @param[in] p_dfu Nordic UART Service structure. * @param[in] p_ble_evt Pointer to the event received from BLE stack. */ static void on_write(ble_nus_t * p_nus, ble_evt_t * p_ble_evt) { ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; if ( (p_evt_write->handle == p_nus->rx_handles.cccd_handle) && (p_evt_write->len == ) ) { if (ble_srv_is_notification_enabled(p_evt_write->data)) { p_nus->is_notification_enabled = true; } else { p_nus->is_notification_enabled = false; } } else if ( (p_evt_write->handle == p_nus->tx_handles.value_handle) && (p_nus->data_handler != NULL) ) { p_nus->data_handler(p_nus, p_evt_write->data, p_evt_write->len); } else { // Do Nothing. This event is not relevant to this service. } }
低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析的更多相关文章
- 低功耗蓝牙4.0BLE编程-nrf51822开发(3)
蓝牙协议栈 nrf51822开发中,蓝牙协议栈和应用开发是分开的. (1)兼容蓝牙4.0低功耗协议栈基带层,L2CAP\AAT\SM\GAP\GATT协议,设备和广播,GATT客户端和服务器,SMP支 ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(1)
为了省钱,也为了提高手动能力,只买了块核心板,仿真器用的是旧的jinkv7,自己搭扩展板,DIY就这样开始了. 买这块之前做了些调查,最终选定了nrf51822,功耗低,性能强,开发难度小,虽然比TI ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(9)
Android 4.3以后的系统自动支持蓝牙4.0规范的低功耗蓝牙(BLE).在android4.3之前,蓝牙4.0支持是由手机厂家加入支持的,接口各异,导致开发一个支持蓝牙4.0程序支持市面上的手机 ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(4)
蓝牙是一种短距离的通讯方式,它设计的意图是取代电子便携设备之间的有线电缆连接.蓝牙的主要特性是健壮性.低功耗.成本低,它工作于免费的2.4无线传输频段. 蓝牙有两种技术系统:基本速率Basic Rat ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(2)
相关下载:http://download.csdn.net/detail/xgbing/9565708 首先看的示例是心率计一个示例程序:<KEIL path> \ARM\Device\N ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(7)-SDP服务发现协议
SDP的全称是Service Discovery Protocol,中文是服务发现协议.SDP(服务发现协议)是蓝牙协议体系中的核心协议,是蓝牙系统重要组成部分,是所有用户模式的基础.在蓝牙系统中.客 ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(6)-Battery Service
Battery Service是有关电池特性方面的服务,如果需要它,在初始化时将它加入到蓝牙协议栈. 如果通过ble_bas_battery_level_update(),电池电量将会通知,Batte ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(10)-描述符
特性中的属性有两种:属性值或描述符. 支持通知或指示的特性中默认有一个描述符:客户端特性配置描述符(Client Characteristic Configuration Descriptor,CCC ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(8)-GATT
The Generic Attribute Profile (GATT)使用属性定义一个服务框架,定义了服务和特性的过程和数据格式,包含发现.读取.写入.通知指示特性和配置特性广播. GATT配置文件 ...
随机推荐
- cmd.ExecuteNonQuery();和cmd.ExecuteScalar();
C#...cmd.ExecuteNonQuery();是返回执行命令后影响的参数 返回符合你条件的所有语句,如果你要数据库里某张表的数据,说执行这个命令后他返回的是就是这张表的全部数据cmd.Exec ...
- servlet中cookie的使用
---恢复内容开始--- Cookie是存储在客户端计算机上的文本文件,并保留了它们的各种信息跟踪的目的. Java Servlet透明支持HTTP Cookie. 涉及标识返回用户有三个步骤: 服务 ...
- Android 编程下 App Install Location
从 API 8 开始(参考官方文档:App Install Location | Android Developers),你可以将你的应用安装在外部储存中(例如,安装到设备的 SD 卡上).这是一个可 ...
- 2016.6.20 计算机网络复习要点第三章之CSMA/CD协议
1.最早的以太网是将许多计算机都连接到一根总线上: (1)总线的特点是:当一台计算机发送数据时,总线上的所有计算机都检测到这个数据,这种就是广播通信方式: (2)为了实现在总线上的一对一通信,可以使每 ...
- 掌上PDA无线下单 移动开单设备 手持POS终端打印扫描开单进销软件
您公司是否存在以下问题吗? ◆ 接单员对价钱是否能记住?接单员是否知道库存? ◆ 接单员是否向客户推介优势产品或新产品? ◆ 接单员是否知道客户为什么有的产品再也没有给我们拿货? ◆ 退货时为什么会退 ...
- LightOJ1051 Good or Bad(DP)
这题感觉做法应该挺多吧,数据规模那么小. 我用DP乱搞了.. dp0[i][j]表示字符串前i位能否组成末尾有连续j个元音字母 dp1[i][j]表示字符串前i位能否组成末尾有连续j个辅音字母 我的转 ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Time.deltaTime 增量时间
static var deltaTime : float Description描述 The time in seconds it took to complete the last frame (R ...
- BZOJ3796 : Mushroom追妹纸
将S1与S2用#号拼接在一起形成S串 将S3与S串跑KMP求出S3在S串中每次出现的位置l[i] 对于S串每个后缀i,求出f[i]表示该串不包含S3串的最长前缀 然后求出S串的后缀数组 先从小到大扫描 ...
- 【转】eclipse插件开发,调试运行,导出与安装
[转自]http://www.kankanews.com/ICkengine/archives/61043.shtml 文章来自KENGINE | Kankanews.com 摘要: 本文主要讲ecl ...