低功耗蓝牙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配置文件 ...
随机推荐
- java 线程的几种状态
java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...
- js:数据结构笔记2---列表
列表: 定义:一组有序的数据: function List() { this.listSize = 0; this.pos = 0; this.dataStore = []; this.find = ...
- Android RelativeLayout 实现左右中布局
效果图如下: 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- MBR 基础
1.简介 MBR,全称为Master Boot Record,即硬盘的主引导记录,它位于整个硬盘的0磁道0柱面1扇区,其主要对硬盘进行了组织,是在驱动器最前端的一段引导扇区. MBR是不属于任何一个操 ...
- BZOJ1185 : [HNOI2007]最小矩形覆盖
求出凸包后,矩形的一条边一定与凸包的某条边重合. 枚举每条边,求出离它最远的点和离它最左最右的点,因为那三个点是单调变化的,所以复杂度为$O(n)$. 注意精度. #include<cstdio ...
- 精通CSS :nth-child伪类
:nth-child 基本用法 :nth-child:nth-child(8) 选中第8个子元素 li:nth-child(8) span { background-color: #298EB2; b ...
- CentOS6.4 配置DNS服务器
1.安装bind yum install -y bind bind-chroot bind-utis 2.配置named.conf [root@dns /]# vi /etc/named.conf 注 ...
- Codeforces Round #208 (Div. 2) A.Dima and Continuous Line
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
- Linux_屏蔽360、scanv、QQ管家等IP扫描
vi banip.sh #!/bin/bash echo "banip" iptables -A INPUT -s 221.204.203.0/24 -j DROP iptable ...
- FireFox火狐浏览器与IE兼容问题 - 透明滤镜 DIV滚动条
问题一:最简单的鼠标移过手变型的css要改了 cursor:pointer;/*FireFox(火狐)不支持cursor:hand*/ dw8下面自动出来的也没有hand这个属性了,标准的是point ...