论坛大神
- 积分
- 1473
- 金钱
- 1473
- 注册时间
- 2013-4-12
- 在线时间
- 77 小时
|
楼主 |
发表于 2015-9-4 23:59:03
|
显示全部楼层
<p class="MsoNormal">
<a href="http://gg.eefocus.com/www/delivery/ck.php?oaparams=2__bannerid=4595__zoneid=380__cb=70145fc168__oadest=http%3A%2F%2Furl.eefocus.com%2Fsq" target="_blank"></a>
</p>
<p class="MsoNormal">
以下内容来自网络,如有侵权请及时联系删除!
</p>
<p class="MsoNormal">
<a href="http://gg.eefocus.com/www/delivery/ck.php?oaparams=2__bannerid=4595__zoneid=380__cb=70145fc168__oadest=http%3A%2F%2Furl.eefocus.com%2Fsq" target="_blank">仅供学习,<br />
花了一个早上写的。板子是pca10001<br />
<img border="0" width="32" height="32" src="file://C:/Users/SLHuang/AppData/Local/Temp/msohtml1/01/clip_image003.gif" /><span class="apple-converted-space"> </span></a><a href="http://www.eeboard.com/bbs/forum.php?mod=attachment&aid=OTAzMDh8ZTY4ZTAzMTh8MTQzMDU3Mzk3MXw5NDAzNHwzOTUxMg%3D%3D" target="_blank">nrf51822.part02.rar</a><span class="apple-converted-space"> </span><em>(4.07
MB, </em><em>下载次数</em><em>: 372)</em><span class="apple-converted-space"> </span><img border="0" width="32" height="32" src="file://C:/Users/SLHuang/AppData/Local/Temp/msohtml1/01/clip_image003.gif" /><a href="http://www.eeboard.com/bbs/forum.php?mod=attachment&aid=OTAzMDd8OTUwMWFiMjh8MTQzMDU3Mzk3MXw5NDAzNHwzOTUxMg%3D%3D" target="_blank">nrf51822.part01.rar</a><span class="apple-converted-space"> </span><em>(7
MB, </em><em>下载次数</em><em>: 516)</em><span class="apple-converted-space"> </span><img border="0" width="32" height="32" src="file://C:/Users/SLHuang/AppData/Local/Temp/msohtml1/01/clip_image003.gif" /><a href="http://www.eeboard.com/bbs/forum.php?mod=attachment&aid=OTAzMDZ8NjcwMjc2Nzd8MTQzMDU3Mzk3MXw5NDAzNHwzOTUxMg%3D%3D" target="_blank">NRF51822 SD_HEX.rar</a><span class="apple-converted-space"> </span><em>(535.87
KB, </em><em>下载次数</em><em>: 138)</em><span class="apple-converted-space"> </span><br />
工程文件nrf51822\Board\nrf6310\s120\experimental\ble_app_uart_c\arm<br />
配套的uart的串口从机是nrf51822\Board\pca10001\s110\experimental\ble_app_uart<br />
<br />
1、首先添加16位的uuid号,重新宏定义如下<br />
<img border="0" width="600" height="22" src="file://C:/Users/SLHuang/AppData/Local/Temp/msohtml1/01/clip_image005.jpg" /><br />
<br />
<strong></strong>
</p>
<p class="MsoNormal">
<b>点击</b><b>(</b><b>此处</b><b>)</b><b>折叠或打开</b>
</p>
<p class="MsoNormal">
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
#define
BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the
Nordic UART Service. */
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
#define
BLE_UUID_NUS_TX_C_CHARACTERISTIC 0x0003 /**<<span class="apple-converted-space"> </span>主机的TX 特性 对应于从机的RX特性*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
#define
BLE_UUID_NUS_RX_C_CHARACTERISTIC 0x0002 /**<<span class="apple-converted-space"> </span>主机的RX 特性.<span class="apple-converted-space"> </span>*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
#define
BLE_NUS_MAX_DATA_LEN<span class="apple-converted-space"> </span>(GATT_MTU_SIZE_DEFAULT<span class="apple-converted-space"> </span>-<span class="apple-converted-space"> </span>3)<span class="apple-converted-space"> </span>/**<<span class="apple-converted-space"> </span>最大20字节Maximum<span class="apple-converted-space"> </span>length<span class="apple-converted-space"> </span>of<span class="apple-converted-space"> </span>data<span class="apple-converted-space"> </span>(in<span class="apple-converted-space"> </span>bytes)<span class="apple-converted-space"> </span>that can be transmitted by the Nordic
UART service module to the peer.<span class="apple-converted-space"> </span>*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
#define
BLE_NUS_MAX_RX_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**<<span class="apple-converted-space"> </span>等价于接收长度Maximum<span class="apple-converted-space"> </span>length<span class="apple-converted-space"> </span>of<span class="apple-converted-space"> </span>the
RX Characteristic(in<span class="apple-converted-space"> </span>bytes).<span class="apple-converted-space"> </span>*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
#define
BLE_NUS_MAX_TX_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**<<span class="apple-converted-space"> </span>Maximum<span class="apple-converted-space"> </span>length<span class="apple-converted-space"> </span>of<span class="apple-converted-space"> </span>the
TX Characteristic<span class="apple-converted-space"> </span>(in<span class="apple-converted-space"> </span>bytes).<span class="apple-converted-space"> </span>*/
</p>
<p class="MsoNormal">
<br />
<br />
蓝牙ble是基于属性协议的<br />
属性协议:客户端通过它可以发现并获取属**器上的属性:6种基本操作<br />
1、请求:客户端发给请求给服务器,需要服务器回复一个响应<br />
2、响应:<br />
3、命令:客户端发给请求给服务器,不需要服务器回复一个响应<br />
4、指示:服务器发给指示给客户端,需要客户端发一个确认给服务器<br />
5、确认:<br />
6、通知:客户端发给请求给服务器,不需要服务器回复一个响应<br />
例子用到了请求,通知。<br />
写入请求包含:一个句柄和一个该属性的值,什么是句柄?首先服务器运行属性数据库(跟数组差不多),里面包含了很多属性,每一个属性都有一个句柄(跟内存地址差不多,随机分配)。<br />
通知:一个句柄和一个该属性的值,客户端在连接建立的时候需要对特性的描述符进行写操作,允许服务器发送通知。<br />
主函数
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
int main(void)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
// Initialization<span class="apple-converted-space"> </span>of<span class="apple-converted-space"> </span>various modules.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
//初始化调试模块:用串口输出信息
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
app_trace_init();
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
NRF_UART0->INTENSET<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>UART_INTENSET_RXDRDY_Enabled<span class="apple-converted-space"> </span><<<span class="apple-converted-space"> </span>UART_INTENSET_RXDRDY_Pos;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
NVIC_SetPriority(UART0_IRQn,<span class="apple-converted-space"> </span>APP_IRQ_PRIORITY_LOW);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
NVIC_EnableIRQ(UART0_IRQn);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11. app_trace_log("init\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13. leds_init();//led初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14. buttons_init();//按键初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15. ble_stack_init();//蓝牙协议栈初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16. device_manager_init();//从设备管理模块初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18. db_discovery_init();/发现模块初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19. //串口服务客户端初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20. nus_c_init();
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22. // Start scanning<span class="apple-converted-space"> </span>for<span class="apple-converted-space"> </span>peripherals<span class="apple-converted-space"> </span>and<span class="apple-converted-space"> </span>initiate
connection
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23. // with devices that advertise Heart Rate
UUID.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24. scan_start();/开始扫描
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25. app_trace_log("start\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26. <span class="apple-converted-space"> </span>for<span class="apple-converted-space"> </span>(;;)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28. power_manage();//等待事件发生
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30. }
</p>
<p class="MsoNormal">
<br />
<br />
主要分析nus_c_init();<br />
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
/*
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
*<span class="apple-converted-space"> </span>@brief 初始化串口服务的客户端
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
*
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
static void
nus_c_init(void)<br />
{<br />
ble_nus_c_init_t nus_c_init_obj;<br />
//注册一个回调函数,这个函数是在main文件实现的,主要实现1、发现服务的处理,2、通知事件的处理,<br />
nus_c_init_obj.evt_handler = nus_c_evt_handler;<br />
<br />
uint32_t err_code = ble_nus_c_init(&m_ble_nus_c,
&nus_c_init_obj);<br />
APP_ERROR_CHECK(err_code);<br />
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
</p>
<p class="MsoNormal">
<br />
<br />
接下来看一下nus_c_evt_handler,里面主要处理的就是两个宏定义,<br />
<b><br />
</b>
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
/**@brief
ble_nus_c_evt_type_t Handler.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
static void
nus_c_evt_handler(ble_nus_c_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_nus_c,<span class="apple-converted-space"> </span>ble_nus_c_evt_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_nus_c_evt)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
bool success;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
switch<span class="apple-converted-space"> </span>(p_nus_c_evt->evt_type)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10.
APPL_LOG("[APP]: 发现了从机,进行绑定,及允许服务器发送通知\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11.
//发现了串口的从机,进行绑定
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12. //
Initiate bonding.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>dm_security_setup_req(&m_dm_device_handle);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14.
APP_ERROR_CHECK(err_code);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15.
//使能从机发送通知事件
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>ble_nus_c_tx_notif_enable(p_nus_c);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17.
APP_ERROR_CHECK(err_code);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21. case
BLE_NUS_C_EVT_TX_NOTIFICATION:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23. char
hr_as_string[LCD_LLEN;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24.
//接收到了通知事件,直接打印下来
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25.
APPL_LOG("[APP]: receive=%s.\r\n",<span class="apple-converted-space"> </span>p_nus_c_evt->params.tx.tx_value);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28. default:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
31. }
</p>
<p class="MsoNormal">
<br />
<br />
ble_nus_c_init函数主要是添加了一个自定义的uuid基数,并把服务的16位uuid号的回调函数注册到了发现模块,当系统运行扫描后,当发现模块发现这个uuid后,会调用这个回调函数<br />
<b><br />
<br />
</b>
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
/*
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
*<span class="apple-converted-space"> </span>@brief s120 串口初始化
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
*
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
uint32_t
ble_nus_c_init(ble_nus_c_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_ble_nus_c,<span class="apple-converted-space"> </span>ble_nus_c_init_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_ble_nus_c_init){
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>((p_ble_nus_c<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NULL)<span class="apple-converted-space"> </span>||<span class="apple-converted-space"> </span>(p_ble_nus_c_init<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NULL))
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. return
NRF_ERROR_NULL;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12. //增加一个自定义的蓝牙uuid基数,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13. ble_uuid128_t nus_base_uuid<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>{0x9E,<span class="apple-converted-space"> </span>0xCA,<span class="apple-converted-space"> </span>0xDC,<span class="apple-converted-space"> </span>0x24,<span class="apple-converted-space"> </span>0x0E,<span class="apple-converted-space"> </span>0xE5,<span class="apple-converted-space"> </span>0xA9,<span class="apple-converted-space"> </span>0xE0,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14.
0x93,<span class="apple-converted-space"> </span>0xF3,<span class="apple-converted-space"> </span>0xA3,<span class="apple-converted-space"> </span>0xB5,<span class="apple-converted-space"> </span>0x00,<span class="apple-converted-space"> </span>0x00,<span class="apple-converted-space"> </span>0x40,<span class="apple-converted-space"> </span>0x6E};
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16. mp_ble_nus_c<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_ble_nus_c;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18. err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>sd_ble_uuid_vs_add(&nus_base_uuid,<span class="apple-converted-space"> </span>&mp_ble_nus_c->nus_uuid.type);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19. <span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(err_code<span class="apple-converted-space"> </span>!=<span class="apple-converted-space"> </span>NRF_SUCCESS)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21. return err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23. //设置服务的16位uuid,配合前面加的蓝牙uuid基数,组成一组完整128位的uuid号
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24. mp_ble_nus_c->nus_uuid.uuid<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_UUID_NUS_SERVICE;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27. mp_ble_nus_c->evt_handler<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_ble_nus_c_init->evt_handler;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28. //设置服务连接句柄为:无效
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29. mp_ble_nus_c->conn_handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_CONN_HANDLE_INVALID;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30. //设置特性的句柄为无效
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
31.
mp_ble_nus_c->nus_rx_handle
=<span class="apple-converted-space"> </span>BLE_GATT_HANDLE_INVALID;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
32.
mp_ble_nus_c->nus_tx_handle
=<span class="apple-converted-space"> </span>BLE_GATT_HANDLE_INVALID;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
33. //设置接收的描述符句柄为:无效
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
34. mp_ble_nus_c->nus_rx_cccd_handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_GATT_HANDLE_INVALID;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
35. mp_ble_nus_c->nus_tx_cccd_handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_GATT_HANDLE_INVALID;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
36. //注册服务的uuid,及其回调函数,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
37. //当发现模块发送有该uuid,就会调用db_discover_evt_handler
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
38. return
ble_db_discovery_register(&mp_ble_nus_c->nus_uuid,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
39.
db_discover_evt_handler);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
40. }
</p>
<p class="MsoNormal">
<br />
<br />
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
//当这个服务 的uuid号被发现,那么这个函数就被调用
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
static void
db_discover_evt_handler(ble_db_discovery_evt_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_evt)<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
//检查uuid号和类型是不是一致
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(p_evt->evt_type<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>BLE_DB_DISCOVERY_COMPLETE
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
&&
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
p_evt->params.discovered_db.srv_uuid.uuid<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>mp_ble_nus_c->nus_uuid.uuid
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
&&
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
p_evt->params.discovered_db.srv_uuid.type<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>mp_ble_nus_c->nus_uuid.type)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10.
//获取属性句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11.
LOG("[NUS_C]: 服务被发现,获得了连接句柄\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12.
mp_ble_nus_c->conn_handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_evt->conn_handle;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14. // Find the CCCD
Handle<span class="apple-converted-space"> </span>of<span class="apple-converted-space"> </span>the Heart Rate Measurement
characteristic.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15. uint8_t i;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17. for<span class="apple-converted-space"> </span>(i<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>0;<span class="apple-converted-space"> </span>i<span class="apple-converted-space"> </span><<span class="apple-converted-space"> </span>p_evt->params.discovered_db.char_count;<span class="apple-converted-space"> </span>i++)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19. //检查特性的uuid号是不是一致
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20. if<span class="apple-converted-space"> </span>(p_evt->params.discovered_db.charateristics[i.characteristic.uuid.uuid
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21.
<span class="apple-converted-space"> </span>==
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22.
BLE_UUID_NUS_RX_C_CHARACTERISTIC)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25.
// characteristic.<span class="apple-converted-space"> </span>Store
CCCD handle<span class="apple-converted-space"> </span>and<span class="apple-converted-space"> </span>break.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27.
//rx的描述符句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28.
mp_ble_nus_c->nus_rx_cccd_handle<span class="apple-converted-space"> </span>=
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29.
p_evt->params.discovered_db.charateristics[i.cccd_handle;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30.
//rx的句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
31.
mp_ble_nus_c->nus_rx_handle<span class="apple-converted-space"> </span>=
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
32.
p_evt->params.discovered_db.charateristics[i.characteristic.handle_value;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
33.
LOG("[NUS_C]: RX 特性被发现,nus_rx_cccd_handle = %d
nus_rx_handle
=%d\r\n",mp_ble_nus_c->nus_rx_cccd_handle,mp_ble_nus_c->nus_rx_handle);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
34.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
35. }<span class="apple-converted-space"> </span>else<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(p_evt->params.discovered_db.charateristics[i.characteristic.uuid.uuid
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
36.
<span class="apple-converted-space"> </span>==
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
37.
BLE_UUID_NUS_TX_C_CHARACTERISTIC)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
38. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
39.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
40.
// Found Heart Rate characteristic.<span class="apple-converted-space"> </span>Store CCCD handle<span class="apple-converted-space"> </span>and<span class="apple-converted-space"> </span>break.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
41.
mp_ble_nus_c->nus_tx_cccd_handle<span class="apple-converted-space"> </span>=
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
42.
p_evt->params.discovered_db.charateristics[i.cccd_handle;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
43.
mp_ble_nus_c->nus_tx_handle<span class="apple-converted-space"> </span>=
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
44.
p_evt->params.discovered_db.charateristics[i.characteristic.handle_value;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
45.
LOG("[NUS_C]:
TX 特性被发现,nus_tx_cccd_handle = %d
nus_tx_handle
=%d\r\n",mp_ble_nus_c->nus_tx_cccd_handle,mp_ble_nus_c->nus_tx_handle);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
46. }<span class="apple-converted-space"> </span>else<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
47.
LOG("[NUS_C]:有其他的特性\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
48.
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
49.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
50.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
51. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
52.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
53. ble_nus_c_evt_t
evt;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
54.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
55. evt.evt_type<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_NUS_C_EVT_DISCOVERY_COMPLETE;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
56.
//运行main文件注册的回调函数,nus_c_evt_handler
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
57.
mp_ble_nus_c->evt_handler(mp_ble_nus_c,<span class="apple-converted-space"> </span>&evt);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
58. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
59. }
</p>
<p class="MsoNormal">
<br />
<br />
<br />
上面就是初始化做的所有事,接下来分析事件,每发生一个事件都会调用其回调函数,<br />
看一下蓝牙协议栈初始化,主要有ble_evt_dispatch,sys_evt_dispatch两个回调函数,主要讲解ble_evt_dispatch<br />
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
static void
ble_stack_init(void)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
// Initialize the SoftDevice handler module.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM,<span class="apple-converted-space"> </span>false);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
// Register with the SoftDevice handler module<span class="apple-converted-space"> </span>for<span class="apple-converted-space"> </span>BLE
events.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>softdevice_ble_evt_handler_set(ble_evt_dispatch);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. APP_ERROR_CHECK(err_code);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12. // Register with the SoftDevice handler
module<span class="apple-converted-space"> </span>for<span class="apple-converted-space"> </span>System events.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13. err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>softdevice_sys_evt_handler_set(sys_evt_dispatch);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14. APP_ERROR_CHECK(err_code);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15. }
</p>
<p class="MsoNormal">
<br />
<br />
ble_evt_dispatch:把事件分派给各个模块处理,自己主要需要编写的ble_nus_c_on_ble_evt,及on_ble_evt<br />
<b><br />
<br />
</b>
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
static void
ble_evt_dispatch(ble_evt_t * p_ble_evt)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
app_trace_log("evt_id = %x\r\n",p_ble_evt->header.evt_id);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
dm_ble_evt_handler(p_ble_evt);//管理模块的处理
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
ble_db_discovery_on_ble_evt(&m_ble_db_discovery,<span class="apple-converted-space"> </span>p_ble_evt);//发现模块的处理
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
ble_nus_c_on_ble_evt(&m_ble_nus_c,<span class="apple-converted-space"> </span>p_ble_evt);//nus的处理
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
on_ble_evt(p_ble_evt);//main的处理:广播包处理->进行连接
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
}
</p>
<p class="MsoNormal">
<br />
<br />
<br />
看一下main函数中的on_ble_evt,<br />
<b><br />
</b>
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
/**@brief
Function for handling the Application's BLE Stack events.这里主要处理的广播包
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
*
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
*<span class="apple-converted-space"> </span>@param[in<span class="apple-converted-space"> </span>p_ble_evt Bluetooth stack event.,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
static void
on_ble_evt(ble_evt_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_ble_evt)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
ble_uuid_t peer_uuid;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
<span class="apple-converted-space"> </span>const<span class="apple-converted-space"> </span>ble_gap_evt_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_gap_evt<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>&p_ble_evt->evt.gap_evt;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. app_trace_log("on_ble_evt\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11. switch<span class="apple-converted-space"> </span>(p_ble_evt->header.evt_id)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12. <span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13.
<span class="apple-converted-space"> </span>//发现了一个广播包
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14. case<span class="apple-converted-space"> </span>BLE_GAP_EVT_ADV_REPORT:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16.
data_t adv_data;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17.
data_t type_data;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19. //<span class="apple-converted-space"> </span>Initialize advertisement report<span class="apple-converted-space"> </span>for<span class="apple-converted-space"> </span>parsing.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20.
//首先是要从广播包解析出数据
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21.
adv_data.p_data<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>(uint8_t<span class="apple-converted-space"> </span>*)p_gap_evt->params.adv_report.data;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22.
adv_data.data_len<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_gap_evt->params.adv_report.dlen;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23.
//获得完整的128bit,uuid号
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>adv_report_parse(BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25.
<span class="apple-converted-space"> </span>&adv_data,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26.
<span class="apple-converted-space"> </span>&type_data);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28. //<span class="apple-converted-space"> </span>Verify<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>short<span class="apple-converted-space"> </span>or<span class="apple-converted-space"> </span>complete
name matches target.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29. if<span class="apple-converted-space"> </span>(err_code<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NRF_SUCCESS)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30.
{ //对uuid进行换算:128bit换算成一个16bit及其类型,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
31.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>sd_ble_uuid_decode(type_data.data_len,type_data.p_data,&peer_uuid);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
32.
<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(err_code<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NRF_SUCCESS)<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
33.
if<span class="apple-converted-space"> </span>((peer_uuid.uuid<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>m_ble_nus_c.nus_uuid.uuid<span class="apple-converted-space"> </span>)&&peer_uuid.type<span class="apple-converted-space"> </span>==m_ble_nus_c.nus_uuid.type<span class="apple-converted-space"> </span>)<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
34.
//发现了从机的,停止广播并尝试连接
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
35.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>sd_ble_gap_scan_stop();
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
36.
if<span class="apple-converted-space"> </span>(err_code<span class="apple-converted-space"> </span>!=<span class="apple-converted-space"> </span>NRF_SUCCESS)<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
37.
APPL_LOG("[APPL]: Scan stop failed, reason %d\r\n",<span class="apple-converted-space"> </span>err_code);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
38.
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
39.
nrf_gpio_pin_clear(SCAN_LED_PIN_NO);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
40.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>sd_ble_gap_connect(&p_gap_evt->params.adv_report.\
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
41.
peer_addr,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
42.
&m_scan_param,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
43.
&m_connection_param);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
44.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
45.
if<span class="apple-converted-space"> </span>(err_code<span class="apple-converted-space"> </span>!=<span class="apple-converted-space"> </span>NRF_SUCCESS)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
46.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
47.
APPL_LOG("[APPL]: Connection Request Failed, reason %d\r\n",<span class="apple-converted-space"> </span>err_code);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
48.
}<span class="apple-converted-space"> </span>else<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
49.
APPL_LOG("连接建立\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
50.
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
51.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
52.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
53.
}else<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
54.
APPL_LOG("匹配出错\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
55.
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
56.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
57.
<span class="apple-converted-space"> </span>}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
58. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
59.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
60. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
61. 以下省略,直接拷贝官方的处理方式
</p>
<p class="MsoNormal">
<br />
<br />
<br />
看一下nus模块的处理<br />
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
void
ble_nus_c_on_ble_evt(ble_nus_c_t * p_ble_nus_c, const ble_evt_t * p_ble_evt) {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
ble_uuid_t server_uuid;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>((p_ble_nus_c<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NULL)||(p_ble_evt<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NULL))<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
return<span class="apple-converted-space"> </span>;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
switch<span class="apple-converted-space"> </span>(p_ble_evt->header.evt_id)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. case
BLE_GAP_EVT_CONNECTED:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11.
//连接完成,获取连接句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12.
p_ble_nus_c->conn_handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_ble_evt->evt.gap_evt.conn_handle;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13.
LOG("[NUS_C]:BLE_GAP_EVT_CONNECTED ,conn_handle =
%d\r\n",p_ble_nus_c->conn_handle);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16. case
BLE_GATTC_EVT_HVX:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17.
//通知事件
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18.
LOG("[NUS_C]:BLE_GATTC_EVT_HVX\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19.
on_hvx(p_ble_nus_c,<span class="apple-converted-space"> </span>p_ble_evt);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22. case
BLE_GATTC_EVT_WRITE_RSP:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23.
LOG("[NUS_C]:BLE_GATTC_EVT_WRITE_RSP\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24.
//客户端发送写请求给服务器,服务器会回一个应答信号给客户端。这时客户端可以发送下一条写请求给服务器端。
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25.
//属性协议规定:写请求需要在前一条请求接收到应答后才能继续
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26.
//添加的原因:猜测:发送写请求是通过一个队列的,如果发送失败,那么就会有一个未发送的在队列里,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27.
//如果这时在发送一个写请求,那么发送将会是前一条写请求,而后收到应答后会继续发送写请求,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29.
on_write_rsp(p_ble_nus_c,<span class="apple-converted-space"> </span>p_ble_evt);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
31.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
32. default:
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
33.
break;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
34. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
35. }
</p>
<p class="MsoNormal">
<br />
<br />
通知事件处理:<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
//通知事件的处理方式
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
static void
on_hvx(ble_nus_c_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_ble_nus_c,<span class="apple-converted-space"> </span>const ble_evt_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_ble_evt)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
// Check<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>this<span class="apple-converted-space"> </span>is
a heart rate notification.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
//检查是不是tx的句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(p_ble_evt->evt.gattc_evt.params.hvx.handle<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>p_ble_nus_c->nus_tx_handle)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
ble_nus_c_evt_t ble_nus_c_evt;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
uint8_t index<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>0;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. //设置成BLE_NUS_C_EVT_TX_NOTIFICATION
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11.
ble_nus_c_evt.evt_type<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_NUS_C_EVT_TX_NOTIFICATION;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12.
strncpy(ble_nus_c_evt.params.tx.tx_value,p_ble_evt->evt.gattc_evt.params.hvx.data,p_ble_evt->evt.gattc_evt.params.hvx.len);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13.
ble_nus_c_evt.params.tx.tx_len<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_ble_evt->evt.gattc_evt.params.hvx.len;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14.
//调用main函数注册的回调函数nus_c_evt_handler
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15.
p_ble_nus_c->evt_handler(p_ble_nus_c,<span class="apple-converted-space"> </span>&ble_nus_c_evt);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17. }
</p>
<p class="MsoNormal">
<br />
<br />
对于写应答,主要是继续发送下一条请求(如果有的话),<br />
<br />
<br />
接下来讲解一下客户端怎么发送数据到服务器,<br />
串口中断程序<br />
<br />
<br />
<strong></strong>
</p>
<p class="MsoNormal">
<b>点击</b><b>(</b><b>此处</b><b>)</b><b>折叠或打开</b>
</p>
<p class="MsoNormal">
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
void
UART0_IRQHandler(void)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
static uint8_t index<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>0;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
/**@snippet<span class="apple-converted-space"> </span>[Handling the data
received over UART<span class="apple-converted-space"> </span>*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
data_array[index<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>simple_uart_get();
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. index++;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12. <span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>((data_array[index<span class="apple-converted-space"> </span>-<span class="apple-converted-space"> </span>1<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>'\n')<span class="apple-converted-space"> </span>||<span class="apple-converted-space"> </span>(index<span class="apple-converted-space"> </span>>=<span class="apple-converted-space"> </span>(BLE_NUS_MAX_DATA_LEN<span class="apple-converted-space"> </span>-<span class="apple-converted-space"> </span>1)))
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14. //发送数据到服务器
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15.
tx_send(&m_ble_nus_c,data_array,index);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16. index<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>0;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19. /**@snippet<span class="apple-converted-space"> </span>[Handling the data received over UART<span class="apple-converted-space"> </span>*/
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20. }
</p>
<p class="MsoNormal">
<br />
<br />
<br />
<br />
<br />
<strong></strong>
</p>
<p class="MsoNormal">
<b>点击</b><b>(</b><b>此处</b><b>)</b><b>折叠或打开</b>
</p>
<p class="MsoNormal">
<br />
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
1.
void
tx_send(ble_nus_c_t * p_ble_nus_c,char *str ,unsigned char len){
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
2.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
3.
tx_message_t<span class="apple-converted-space"> </span>*<span class="apple-converted-space"> </span>p_msg;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
4.
<span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(len<span class="apple-converted-space"> </span>><span class="apple-converted-space"> </span>WRITE_MESSAGE_LENGTH)<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
5.
return<span class="apple-converted-space"> </span>;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
6.
}
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
7.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
8.
//发送队列
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
9.
p_msg<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>&m_tx_buffer[m_tx_insert_index++;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
10. m_tx_insert_index &=<span class="apple-converted-space"> </span>TX_BUFFER_MASK;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
11. strncpy(<span class="apple-converted-space"> </span>p_msg->req.write_req.gattc_value,str,len);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
12. //属性句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
13. p_msg->req.write_req.gattc_params.handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_ble_nus_c->nus_rx_handle;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
14. //数据长度
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
15. p_msg->req.write_req.gattc_params.len<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>len;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
16. //数据
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
17.
p_msg->req.write_req.gattc_params.p_value<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_msg->req.write_req.gattc_value;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
18. p_msg->req.write_req.gattc_params.offset<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>0;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
19. //写请求
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
20.
p_msg->req.write_req.gattc_params.write_op<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>BLE_GATT_OP_WRITE_REQ;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
21. //连接句柄
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
22. p_msg->conn_handle<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>p_ble_nus_c->conn_handle;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
23. //不是协议的东西
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
24. p_msg->type<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>WRITE_REQ;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
25. //发送过程
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
26. tx_buffer_process();
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
27. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
28.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
29.
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
30. /*
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
31. *@<span class="apple-converted-space"> </span>brief 发送数据
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
32. */
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
33. static void tx_buffer_process(void)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
34. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
35. <span class="apple-converted-space"> </span>if<span class="apple-converted-space"> </span>(m_tx_index<span class="apple-converted-space"> </span>!=<span class="apple-converted-space"> </span>m_tx_insert_index)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
36. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
37. uint32_t err_code;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
38. //本例子中只有写请求
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
39. if<span class="apple-converted-space"> </span>(m_tx_buffer[m_tx_index.type<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>WRITE_REQ)<span class="apple-converted-space"> </span>{
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
40.
LOG("enter the write \r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
41.
err_code<span class="apple-converted-space"> </span>=<span class="apple-converted-space"> </span>sd_ble_gattc_write(m_tx_buffer[m_tx_index.conn_handle,
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
42.
&m_tx_buffer[m_tx_index.req.write_req.gattc_params);
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
43. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
44. if<span class="apple-converted-space"> </span>(err_code<span class="apple-converted-space"> </span>==<span class="apple-converted-space"> </span>NRF_SUCCESS)
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
45. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
46.
LOG("[NUS_C]: SD Write API returns Success..\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
47.
m_tx_index++;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
48.
m_tx_index &=<span class="apple-converted-space"> </span>TX_BUFFER_MASK;
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
49. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
50. else
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
51. {
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
52.
//发送失败
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
53.
LOG("[NUS_C]: SD Write API returns error. This message sending will
be "
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
54.
<span class="apple-converted-space"> </span>"attempted again..\r\n");
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
55. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
56. }
</p>
<p class="MsoNormal" style="margin-left:32.4pt;text-indent:-18.0pt;">
57. }
</p>
<p class="MsoNormal">
<span> </span>
</p> |
|