W801蓝牙收发数据与控制设计(二)-NOTIFY
这是目录
- 一、项目概述
- 二、程序设计
- 二、测试
W801蓝牙收发数据与控制设计(一)-INDICATE
W801蓝牙收发数据与控制设计(二)-NOTIFY方式
本文使用环境:
主控:W801 (开发板)
兼容:W800 AIR101
一、项目概述
^^^^这篇教程接着上文继续介绍W801的BT使用,上文使用INDICATE方式进行数据的收发,这篇文章主要采用NOTIFY方式进行数据收发。具体Indicate 和 Notify的区别,可以自己查一下,我也不是很了解这个东西。好像是 (indicate发送接收端有应答,notify接收端无应答) 。
^^^^程序功能: 和上文差不多,区别在于Indicate 和 Notify不一样。
二、程序设计
写在前面:本文的程序是在上文的基础上做的改进,所以必须下载前文的程序。
以下代码均在wm_ble_server_api_demo.c中添加。
1、先添加宏定义,关于notify主要有两个地方,如下:
/* ble attr write/notify handle */uint16_t g_ble_demo_attr_indicate_handle;uint16_t g_ble_demo_attr_write_handle;uint16_t g_ble_demo_conn_handle ;//add by zxx startuint16_t g_ble_demo_attr_notify_handle;//add by zxx end#define WM_GATT_SVC_UUID 0xFFF0#define WM_GATT_INDICATE_UUID 0xFFF1#define WM_GATT_WRITE_UUID 0xFFF2//add by zxx start#define WM_GATT_NOTIFY_UUID 0xFFF3//add by zxx end
2、官方的demo中只有WRITE和INDICATE,因此添加代码characteristics :
//notify_test函数没有任何的作用,不添加蓝牙notify方式初始化会失败。static int notify_test(){;}static const struct ble_gatt_svc_def gatt_demo_svr_svcs[] = { { /* Service: uart */ .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = BLE_UUID16_DECLARE(WM_GATT_SVC_UUID), .characteristics = (struct ble_gatt_chr_def[]) { { .uuid = BLE_UUID16_DECLARE(WM_GATT_WRITE_UUID), .val_handle = &g_ble_demo_attr_write_handle, .access_cb = gatt_svr_chr_demo_access_func, .flags = BLE_GATT_CHR_F_WRITE, },{ .uuid = BLE_UUID16_DECLARE(WM_GATT_INDICATE_UUID), .val_handle = &g_ble_demo_attr_indicate_handle, .access_cb = gatt_svr_chr_demo_access_func, .flags = BLE_GATT_CHR_F_INDICATE, }//add by zxx start,{ .uuid = BLE_UUID16_DECLARE(WM_GATT_NOTIFY_UUID), .val_handle = &g_ble_demo_attr_notify_handle, //这个函数不会调用,但是必须要有, .access_cb = notify_test, .flags = BLE_GATT_CHR_F_NOTIFY, }//add by zxx end,{0, /* No more characteristics in this service */ } }, }, { 0, /* No more services */ },};
3、添加tls_ble_server_demo_api_send_notify_msg
函数,只需仿照tls_ble_server_demo_api_send_msg
函数进行修改即可。
//add by zxx startint tls_ble_server_demo_api_send_notify_msg(uint8_t *data, int data_len){ int rc; struct os_mbuf *om; //TLS_BT_APPL_TRACE_DEBUG("### %s len=%d\r\n", __FUNCTION__, data_len); //这句话要注释掉,要不然这个代码的逻辑不对 //if(g_send_pending) return BLE_HS_EBUSY; if(data_len<=0 || data == NULL) { return BLE_HS_EINVAL; } om = ble_hs_mbuf_from_flat(data, data_len); if (!om) { return BLE_HS_ENOMEM; } //参数g_ble_demo_attr_notify_handle句柄要和characteristics 的句柄一样 rc = ble_gattc_notify_custom(g_ble_demo_conn_handle,g_ble_demo_attr_notify_handle, om); //这句话注释不注释都可以,我注释了 /*if(rc == 0) { g_send_pending = 1; }*/ return rc;}//add by zxx end
二、测试
1、app端配置
按照如图所示进行配置,注意TX是xxxf3,RX是xxxf2。配置完成正常收发程序即可。
其他测试步骤和上文一样,不再赘述。