W801/W800 Bluetooth data transmission and reception and control design (Part 2) - NOTIFY

Publisher:annye_chengLatest update time:2022-07-25 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This article uses the following environment:

Main control: W801 (development board)

Compatible with: W800 AIR101

Software: CDK


W801 Bluetooth data transmission and reception and control design (I) - INDICATE

W801 Bluetooth data transmission and reception and control design (Part 2) - NOTIFY method

W800-KIT-Bluetooth-Temperature and humidity sensor (cht8305c)-RGB light-demo


1. Project Overview

^^^^This tutorial continues the previous one to introduce the use of W801's BT. The previous one uses the INDICATE method to send and receive data, and this article mainly uses the NOTIFY method to send and receive data. You can check the specific difference between Indicate and Notify yourself. I don't know much about this thing. It seems that (indicate sends and receives a response from the receiving end, and notify does not receive a response from the receiving end).


^^^^Program function: Similar to the above, the difference is that Indicate and Notify are different.


2. Programming

Preface: The program in this article is an improvement on the previous one, so you must download the program in the previous article.


The following codes are all added in wm_ble_server_api_demo.c.


1. Add macro definitions first. There are two main places for notify, as follows:


/* 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. The official demo only has WRITE and INDICATE, so add code characteristics:


//The notify_test function has no effect. If the Bluetooth notify method is not added, initialization will fail.

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,

                //This function will not be called, but it must be there,

                .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. Add the tls_ble_server_demo_api_send_notify_msg function. Just modify it according to the tls_ble_server_demo_api_send_msg function.


//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=%drn", __FUNCTION__, data_len);

    //This sentence should be commented out, otherwise the logic of this code is wrong

    //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;

    }

    //The parameter g_ble_demo_attr_notify_handle handle should be the same as the handle of characteristics

    rc = ble_gattc_notify_custom(g_ble_demo_conn_handle,g_ble_demo_attr_notify_handle, om); 

    //You can comment or not comment this sentence, I commented it

    /*

if(rc == 0)

    {

        g_send_pending = 1;

    }

*/

    return rc;

}

//add by zxx end


2. Testing

1. App configuration

Configure as shown in the figure, note that TX is xxxf3 and RX is xxxf2. After the configuration is completed, the normal sending and receiving program can be performed.

insert image description here

The other test steps are the same as above and will not be repeated here.

Reference address:W801/W800 Bluetooth data transmission and reception and control design (Part 2) - NOTIFY

Previous article:W801/W800 Bluetooth data transmission and control design (Part 3) - Actual project: Bluetooth transmission of temperature and humidity data
Next article:W801/W800 Bluetooth data transmission and reception and control design (I) - INDICATE

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号