@TOC
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 start
uint16_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 start
int 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==. 配置完成正常收发程序即可.
其他测试步骤和上文一样, 不再赘述.