nRF52832添加微信硬件接入服務AirSync

開發環境

SDK版本:nRF5_SDK_15.0.0html

芯片:nRF52832-QFAA數組

OS: FreeRTOS 10.0.0服務器

測試APP:AirSyncDebugger  https://iot.weixin.qq.com/wiki/doc/blue/AirSyncDebugger2.3.0.apk微信

AirSync概述

AirSync是微信硬件平臺提供的一種微信客戶端與藍牙設備間通信的技術協議,它容許藍牙設備與微信客戶端之間收發數據,並支持經過微信客戶端透傳到遠程服務器。該技術在支持微信互聯的藍牙手環、血壓計、智能秤、血糖儀等設備上有比較多的應用。數據結構

詳細開發文檔請參考官網 微信硬件平臺的 app

AirSync開發文檔框架

AirSync 服務添加

本文僅僅就怎麼在sdk15的框架下添加 AirSync 服務進行描述,具體協議實現請參考官方給出的示例程序便可ide

ble_wechat_service.c函數

藍牙設備需暴露兩個特徵值(Characteristics):Write特徵值,Indication特徵值。藍牙設備從Write特徵值接受數據,從Indication特徵值發送數據。測試

/**
  **********************************************************************************
  * @file    ble_wechat_service.c
  * @author  wanghuan  any question please send mail to 371463817@qq.com
  * @version V1.0
  * @date    2019-01-28
  * @brief   藍牙BLE微信硬件接入服務
  **********************************************************************************
  * @attention
  *
  * 版權說明:Copyright (c) 2019-2020   江蘇亨通光網科技有限公司
  * 硬件平臺:nRF52832_QFAA
  * SDK版本:nRF5_SDK_15.0.0
  * 修改日誌:
  *
  **********************************************************************************
  */

#include "sdk_common.h"
#if NRF_MODULE_ENABLED(BLE_WECHAT)
#include "ble.h"
#include "ble_wechat_service.h"
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_soc.h"
#include "app_error.h"

// 日誌打印配置
#define NRF_LOG_MODULE_NAME ble_wechat
#if BLE_WECHAT_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL       BLE_WECHAT_CONFIG_LOG_LEVEL
#define NRF_LOG_INFO_COLOR  BLE_WECHAT_CONFIG_INFO_COLOR
#define NRF_LOG_DEBUG_COLOR BLE_WECHAT_CONFIG_DEBUG_COLOR
#else // BLE_WECHAT_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL       0
#endif // BLE_WECHAT_CONFIG_LOG_ENABLED
#include "nrf_log.h"
NRF_LOG_MODULE_REGISTER();

uint8_t m_addl_adv_manuf_data [BLE_GAP_ADDR_LEN];

/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the SoftDevice.
 * 
 * @param[in] p_wechats     BLE_WECHAT Service structure.
 * @param[in] p_ble_evt Pointer to the event received from BLE stack.
 */
static void on_connect(ble_wechat_t * p_wechat, ble_evt_t const * p_ble_evt)
{
    ret_code_t                  err_code;
    ble_wechat_evt_t            evt;
    ble_gatts_value_t           gatts_val;
    uint8_t                     cccd_value[2];
    ble_wechat_client_context_t * p_client = NULL;

    p_wechat->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    
    err_code = blcm_link_ctx_get(p_wechat->p_link_ctx_storage,
                                 p_ble_evt->evt.gap_evt.conn_handle,
                                 (void *) &p_client);
    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
                      p_ble_evt->evt.gap_evt.conn_handle);
    }

    /* Check the hosts CCCD value to inform of readiness to send data using the RX characteristic */
    memset(&gatts_val, 0, sizeof(ble_gatts_value_t));
    gatts_val.p_value = cccd_value;
    gatts_val.len     = sizeof(cccd_value);
    gatts_val.offset  = 0;

    err_code = sd_ble_gatts_value_get(p_ble_evt->evt.gap_evt.conn_handle,
                                      p_wechat->indicate_handles.cccd_handle,
                                      &gatts_val);

    if ((err_code == NRF_SUCCESS)     &&
        (p_wechat->data_handler != NULL) &&
        ble_srv_is_indication_enabled(gatts_val.p_value))
    {
        if (p_client != NULL)
        {
            p_client->is_indicate_enabled = true;
        }

        memset(&evt, 0, sizeof(ble_wechat_evt_t));
        evt.type        = BLE_WECHAT_EVT_COMM_STARTED;
        evt.p_wechat    = p_wechat;
        evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
        evt.p_link_ctx  = p_client;

        p_wechat->data_handler(&evt);
    }
}


static void on_disconnect(ble_wechat_t * p_wechat, ble_evt_t const * p_ble_evt)
{
    ble_wechat_evt_t            evt;
    
    p_wechat->conn_handle = BLE_CONN_HANDLE_INVALID;
    
    evt.type        = BLE_WECHAT_EVT_COMM_STOPPED;
    evt.p_wechat    = p_wechat;
    evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    
    p_wechat->data_handler(&evt);
}


// BLE 寫操做
static void on_write(ble_wechat_t * p_wechat, ble_evt_t const * p_ble_evt)
{
    ret_code_t                      err_code;
    ble_wechat_evt_t                evt;
    ble_wechat_client_context_t     * p_client;
    ble_gatts_evt_write_t const     * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;

    err_code = blcm_link_ctx_get(p_wechat->p_link_ctx_storage,
                                 p_ble_evt->evt.gatts_evt.conn_handle,
                                 (void *) &p_client);
    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
                      p_ble_evt->evt.gatts_evt.conn_handle);
    }

    memset(&evt, 0, sizeof(ble_wechat_evt_t));
    evt.p_wechat    = p_wechat;
    evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
    evt.p_link_ctx  = p_client;

    if ((p_evt_write->handle == p_wechat->write_handles.value_handle) && (p_evt_write->len <= BLE_WECHAT_MAX_DATA_LEN) && (p_wechat->data_handler != NULL)) 
    {
        evt.type                  = BLE_WECHAT_EVT_RX_DATA;
        evt.params.rx_data.p_data = p_evt_write->data;
        evt.params.rx_data.length = p_evt_write->len;

        p_wechat->data_handler(&evt);
    }
    else
    {
        // Do Nothing. This event is not relevant for this service.
    }
}


static void on_indicate_comfirm(ble_wechat_t *p_wechat, ble_evt_t const * p_ble_evt)
{
    ret_code_t                  err_code;
    ble_wechat_evt_t              evt;
    ble_wechat_client_context_t * p_client;

    err_code = blcm_link_ctx_get(p_wechat->p_link_ctx_storage,
                                 p_ble_evt->evt.gatts_evt.conn_handle,
                                 (void *) &p_client);
    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
                      p_ble_evt->evt.gatts_evt.conn_handle);
        return;
    }

    if (p_client->is_indicate_enabled)
    {
        memset(&evt, 0, sizeof(ble_wechat_evt_t));
        evt.type        = BLE_WECHAT_EVT_TX_RDY;
        evt.p_wechat    = p_wechat;
        evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
        evt.p_link_ctx  = p_client;

        p_wechat->data_handler(&evt);
    }
}


// BLE_WECHAT服務藍牙事件處理函數
void ble_wechat_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    if ((p_context == NULL) || (p_ble_evt == NULL))
    {
        return;
    }

    ble_wechat_t * p_wechat = (ble_wechat_t *)p_context;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_wechat, p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_wechat, p_ble_evt);
            break;

        case BLE_GATTS_EVT_HVC:
            on_indicate_comfirm(p_wechat, p_ble_evt);
            break;
        
        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
//            on_hvx_tx_complete(p_wechat, p_ble_evt);
            break;
        
        case BLE_GAP_EVT_DISCONNECTED:
            on_disconnect(p_wechat, p_ble_evt);
            break;
        default:
            // No implementation needed.
            break;
    }
}


static void get_mac_addr(uint8_t *p_mac_addr)
{
        uint32_t error_code;
        ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
        error_code = sd_ble_gap_addr_get(p_mac_addr_t);
        APP_ERROR_CHECK(error_code);
        uint8_t *d = p_mac_addr_t->addr;
        for ( uint8_t i = 6; i >0;)
        {    
            i--;
            p_mac_addr[5-i]= d[i];
        }
        free(p_mac_addr_t);
        p_mac_addr_t = NULL;
}


// 微信服務藍牙通訊indicate特徵字節添加
static uint32_t ble_wechat_add_indicate_char(ble_wechat_t * p_wechat)
{
    ble_gatts_char_md_t char_md;            //GATT特徵屬性所有都用char_md表示
    ble_gatts_attr_t    attr_char_value;    //GATT屬性
    ble_uuid_t          char_uuid;          //特徵字節用uuid表示
    ble_gatts_attr_md_t attr_md;            //特徵值屬性
    ble_gatts_attr_md_t cccd_md;            //CCCD信息(客戶端特性配置描述符,用於配置通知和指示)
    
    memset(&cccd_md, 0, sizeof(cccd_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);     //開放read權限(權限等級)
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);    //開放write權限
    cccd_md.vloc = BLE_GATTS_VLOC_STACK;                    //屬性值存放在堆棧空間,Softdevice能夠控制的memory
    
    memset(&char_md, 0, sizeof(char_md));
    // 特徵值參數組(用於申明)
    char char_desc[] = "indicate char";
    char_md.p_char_user_desc = (uint8_t *)char_desc;
    char_md.char_user_desc_size = strlen(char_desc);
    char_md.char_user_desc_max_size =strlen(char_desc);
    
    char_md.char_props.indicate      = 1;   //容許指示修改
    char_md.p_char_pf                = NULL;
    char_md.p_user_desc_md           = NULL;
    char_md.p_cccd_md                = &cccd_md;
    char_md.p_sccd_md                = NULL;
    
    
    // 使用公用服務藍牙協議小組基礎UUID
    BLE_UUID_BLE_ASSIGN(char_uuid, BLE_UUID_WECHAT_INDICATE_CHARACTERISTICS);

    memset(&attr_md, 0, sizeof(attr_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

    attr_md.vloc    = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth = 0;
    attr_md.wr_auth = 0;
    attr_md.vlen    = 1;

    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;
    attr_char_value.init_len  = strlen(char_desc);
    attr_char_value.p_value   = (uint8_t *)char_desc;
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = BLE_WECHAT_MAX_DATA_LEN;

    return sd_ble_gatts_characteristic_add(p_wechat->service_handle,
                                           &char_md,
                                           &attr_char_value,
                                           &p_wechat->indicate_handles);
}


// 微信服務藍牙通訊write特徵字節添加
static uint32_t ble_wechat_add_write_char(ble_wechat_t * p_wechat)
{
    ble_gatts_char_md_t char_md;            //GATT特徵屬性所有都用char_md表示
    ble_gatts_attr_t    attr_char_value;    //GATT屬性
    ble_uuid_t          char_uuid;          //特徵字節用uuid表示
    ble_gatts_attr_md_t attr_md;            //特徵值屬性
    
    
    memset(&char_md, 0, sizeof(char_md));
    // 特徵值參數組(用於申明)
    char char_desc[] = "write char";
    char_md.p_char_user_desc = (uint8_t *)char_desc;
    char_md.char_user_desc_size = strlen(char_desc);
    char_md.char_user_desc_max_size =strlen(char_desc);
    
    char_md.char_props.write         = 1;   //容許請求寫修改操做
    char_md.p_char_pf                = NULL;
    char_md.p_user_desc_md           = NULL;
    char_md.p_cccd_md                = NULL;
    char_md.p_sccd_md                = NULL;
    
    
    // 使用公用服務藍牙協議小組基礎UUID
    BLE_UUID_BLE_ASSIGN(char_uuid, BLE_UUID_WECHAT_WRITE_CHARACTERISTICS);

    memset(&attr_md, 0, sizeof(attr_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

    attr_md.vloc    = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth = 0;
    attr_md.wr_auth = 0;
    attr_md.vlen    = 1;

    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;
    attr_char_value.init_len  = strlen(char_desc);
    attr_char_value.p_value   = (uint8_t *)char_desc;
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = BLE_WECHAT_MAX_DATA_LEN;

    return sd_ble_gatts_characteristic_add(p_wechat->service_handle,
                                           &char_md,
                                           &attr_char_value,
                                           &p_wechat->write_handles);
}


// 微信服務藍牙通訊read特徵字節添加
static uint32_t ble_wechat_add_read_char(ble_wechat_t * p_wechat)
{
    ble_gatts_char_md_t char_md;            //GATT特徵屬性所有都用char_md表示
    ble_gatts_attr_t    attr_char_value;    //GATT屬性
    ble_uuid_t          char_uuid;          //特徵字節用uuid表示
    ble_gatts_attr_md_t attr_md;            //特徵值屬性
    
    get_mac_addr(m_addl_adv_manuf_data);
    
    memset(&char_md, 0, sizeof(char_md));
    // 特徵值參數組(用於申明)
//    char char_desc[] = "read char";
//    char_md.p_char_user_desc = (uint8_t *)char_desc;
//    char_md.char_user_desc_size = strlen(char_desc);
//    char_md.char_user_desc_max_size =strlen(char_desc);
    
    char_md.char_props.read          = 1;   //容許讀操做
    char_md.p_char_user_desc  = NULL;
    char_md.p_char_pf                = NULL;
    char_md.p_user_desc_md           = NULL;
    char_md.p_cccd_md                = NULL;
    char_md.p_sccd_md                = NULL;
    
    
    // 使用公用服務藍牙協議小組基礎UUID
    BLE_UUID_BLE_ASSIGN(char_uuid, BLE_UUID_WECHAT_READ_CHARACTERISTICS);

    memset(&attr_md, 0, sizeof(attr_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

    attr_md.vloc    = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth = 0;
    attr_md.wr_auth = 0;
    attr_md.vlen    = 1;

    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;
    attr_char_value.init_len  = sizeof(m_addl_adv_manuf_data);
    attr_char_value.p_value   = (uint8_t *)m_addl_adv_manuf_data;
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = BLE_WECHAT_MAX_DATA_LEN;

    return sd_ble_gatts_characteristic_add(p_wechat->service_handle,
                                           &char_md,
                                           &attr_char_value,
                                           &p_wechat->read_handles);
}


// BLE_WECHAT 服務初始化
uint32_t ble_wechat_init(ble_wechat_t * p_wechat, ble_wechat_init_t const * p_wechat_init)
{
    ret_code_t    err_code;
    ble_uuid_t    ble_uuid;

    VERIFY_PARAM_NOT_NULL(p_wechat);
    VERIFY_PARAM_NOT_NULL(p_wechat_init);

    // 初始化服務結構體
    // BLE_WECHAT 事件句柄
    p_wechat->data_handler = p_wechat_init->data_handler;
    
    // 添加基於通用SIG-UUID的BLE_WECHAT服務
    BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_WECHAT_SERVICE);

    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_wechat->service_handle);
    VERIFY_SUCCESS(err_code);

    // 添加BLE_WECHAT服務特徵字節
    err_code = ble_wechat_add_indicate_char(p_wechat);
    VERIFY_SUCCESS(err_code);
    err_code = ble_wechat_add_write_char(p_wechat);
    VERIFY_SUCCESS(err_code);
    err_code = ble_wechat_add_read_char(p_wechat);
    VERIFY_SUCCESS(err_code);

    return NRF_SUCCESS;
}



// 藍牙數據發送
uint32_t ble_wechat_indicate_data(ble_wechat_t * p_wechat, uint8_t * p_data, uint16_t * p_length)
{
    ret_code_t                 err_code;
    ble_gatts_hvx_params_t     hvx_params;
    ble_wechat_client_context_t * p_client;

    VERIFY_PARAM_NOT_NULL(p_wechat);

    err_code = blcm_link_ctx_get(p_wechat->p_link_ctx_storage, p_wechat->conn_handle, (void *) &p_client);  //獲取indicate是否使能
    VERIFY_SUCCESS(err_code);

    if ((p_wechat->conn_handle == BLE_CONN_HANDLE_INVALID) || (p_client == NULL))
    {
        return NRF_ERROR_NOT_FOUND;
    }

    if (!p_client->is_indicate_enabled)//通知使能了,沒有使能通知返回錯誤,不藍牙發送
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (*p_length > BLE_WECHAT_MAX_DATA_LEN)
    {
        return NRF_ERROR_INVALID_PARAM;
    }

    memset(&hvx_params, 0, sizeof(hvx_params));

    hvx_params.handle = p_wechat->indicate_handles.value_handle;//數據處理
    hvx_params.p_data = p_data;     //數據
    hvx_params.p_len  = p_length;   //長度
    hvx_params.type   = BLE_GATT_HVX_INDICATION;

    return sd_ble_gatts_hvx(p_wechat->conn_handle, &hvx_params);
}

#endif   // NRF_MODULE_ENABLED(BLE_WECHAT)

 

ble_airsyncs.h

#ifndef BLE_WECHAT_SERVICE__
#define BLE_WECHAT_SERVICE__

#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "sdk_config.h"
#include "ble.h"
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
#include "ble_link_ctx_manager.h"

#ifdef __cplusplus
extern "C" {
#endif

// 藍牙微信服務實例宏定義
// _name    ->實例名稱
// _nus_max_clients  ->最大客戶端連接數
#define BLE_WECHAT_DEF(_name, _wechat_max_clients)                      \
    BLE_LINK_CTX_MANAGER_DEF(CONCAT_2(_name, _link_ctx_storage),        \
                             (_wechat_max_clients),                     \
                             sizeof(ble_wechat_client_context_t));      \
    static ble_wechat_t _name =                                         \
    {                                                                   \
        .p_link_ctx_storage = &CONCAT_2(_name, _link_ctx_storage)       \
    };                                                                  \
    NRF_SDH_BLE_OBSERVER(_name ## _obs,                                 \
                         2,                  \
                         ble_wechat_on_ble_evt,                         \
                         &_name)


// 藍牙微信服務UUID定義
#define BLE_UUID_WECHAT_SERVICE                        0xFEE7
#define BLE_UUID_WECHAT_WRITE_CHARACTERISTICS         0xFEC7
#define BLE_UUID_WECHAT_INDICATE_CHARACTERISTICS     0xFEC8
#define BLE_UUID_WECHAT_READ_CHARACTERISTICS         0xFEC9

#define BLE_WECHAT_MAX_DATA_LEN                 20     


/**@brief   BLE_WECHAT Service event types. */
// 藍牙微信服務事件類型
typedef enum
{
    BLE_WECHAT_EVT_RX_DATA,         /**< Data received. */
    BLE_WECHAT_EVT_TX_RDY,          /**< Service is ready to accept new data to be transmitted. */
    BLE_WECHAT_EVT_COMM_STARTED,    /**< Notification has been enabled. */
    BLE_WECHAT_EVT_COMM_STOPPED,    /**< Notification has been disabled. */
} ble_wechat_evt_type_t;

/* Forward declaration of the ble_wechat_t type. 前置申明*/
typedef struct ble_wechat_s ble_wechat_t;


/**@brief   BLE_WECHAT Service @ref BLE_WECHATS_EVT_RX_DATA event data.
 *          藍牙接收數據事件數據結構體
 * @details This structure is passed to an event when @ref BLE_WECHATS_EVT_RX_DATA occurs.
 */
typedef struct
{
    uint8_t const * p_data;     /**< A pointer to the buffer with received data. */
    uint16_t        length;     /**< Length of received data. */
} ble_wechat_evt_rx_data_t;


/**@brief BLE_WECHAT Service client context structure.
 *
 * @details This structure contains state context related to hosts.
 *          藍牙客戶端參數
 */
typedef struct
{
    bool is_indicate_enabled; /**< Variable to indicate if the peer has enabled notification of the WECHAT_INDICATE characteristic.*/
} ble_wechat_client_context_t;


/**@brief   BLE_WECHAT Service event structure.
 *          藍牙微信服務事件結構體
 * @details This structure is passed to an event coming from service.
 */
typedef struct
{
    ble_wechat_evt_type_t           type;           /**< Event type. */
    ble_wechat_t                    * p_wechat;     /**< A pointer to the instance. */
    uint16_t                        conn_handle;    /**< Connection handle. */
    ble_wechat_client_context_t     * p_link_ctx;   /**< A pointer to the link context. */
    union
    {
        ble_wechat_evt_rx_data_t    rx_data;        /**< @ref BLE_WECHATS_EVT_RX_DATA event data. */
    } params;
} ble_wechat_evt_t;


/**@brief BLE_WECHAT Service event handler type. */
//        藍牙微信服務事件回調函數類型
typedef void (* ble_wechat_data_handler_t) (ble_wechat_evt_t * p_evt);


/**@brief BLE_WECHAT Service initialization structure. */
// 藍牙微信服務初始化結構體,主要用於註冊應用數據接收處理回調
typedef struct
{
    ble_wechat_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
} ble_wechat_init_t;


/**@brief   BLE_WECHAT Service structure.
 *          藍牙微信服務結構體
 * @details This structure contains status information related to the service.
 */
struct ble_wechat_s
{
    uint8_t                         uuid_type;          /**< UUID type for Nordic UART Service Base UUID. */
    uint16_t                        service_handle;     /**< Handle of Nordic UART Service (as provided by the SoftDevice). */
    uint16_t                        conn_handle;        
    ble_gatts_char_handles_t        indicate_handles;   /**< Handles related to the GNTS characteristic (as provided by the SoftDevice). */
    ble_gatts_char_handles_t        write_handles;
    ble_gatts_char_handles_t        read_handles;
    blcm_link_ctx_storage_t * const p_link_ctx_storage; /**< Pointer to link context storage with handles of all current connections and its context. */
    ble_wechat_data_handler_t       data_handler;       /**< Event handler to be called for handling received data. */
};



uint32_t ble_wechat_init(ble_wechat_t * p_wechat, ble_wechat_init_t const * p_wechat_init);

void ble_wechat_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);

uint32_t ble_wechat_indicate_data(ble_wechat_t * p_wechat, uint8_t * p_data, uint16_t * p_length);


#ifdef __cplusplus
}
#endif

#endif // BLE_WECHAT_SERVICE__

/** @} */

在用戶程序中添加服務相關代碼

#if (WECHAT_TEST_EN == 1)
BLE_WECHAT_DEF(m_wechat, NRF_SDH_BLE_TOTAL_LINK_COUNT);   //微信藍牙服務
#endif

// 添加私有服務時,需更改NRF_SDH_BLE_VS_UUID_COUNT
static ble_uuid_t m_adv_uuids[] =                                   /**< Universally unique service identifiers. */
{
    {BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE},
#if (WECHAT_TEST_EN == 1)
    {BLE_UUID_WECHAT_SERVICE, BLE_UUID_TYPE_BLE},
#endif
};

 

在服務初始化 static void services_init(void) 添加

#if (WECHAT_TEST_EN == 1)
    // 添加BLE_WECHAT服務
    ble_wechat_init_t     m_wechat_init;
    memset(&m_wechat_init, 0, sizeof(m_wechat_init));
    m_wechat_init.data_handler = ble_wechat_data_handler;

    err_code = ble_wechat_init(&m_wechat, &m_wechat_init);
    APP_ERROR_CHECK(err_code); 
#endif
#if (WECHAT_TEST_EN == 1)
static void ble_wechat_data_handler(ble_wechat_evt_t * p_evt)   //微信藍牙接收數據處理(write特徵屬性)
{
    if (p_evt->type == BLE_WECHAT_EVT_RX_DATA)    //藍牙有數據接收
    {
        NRF_LOG_INFO("Received data from BLE_WECHAT.");
        NRF_LOG_HEXDUMP_INFO(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
        ble_wechat_indicate_data(&m_wechat, (uint8_t*)p_evt->params.rx_data.p_data, &p_evt->params.rx_data.length);
    }
}
#endif

在廣播初始化 static void advertising_init(void) 中添加

#if (WECHAT_TEST_EN == 1) 
    extern uint8_t m_addl_adv_manuf_data [BLE_GAP_ADDR_LEN];
    ble_advdata_manuf_data_t        manuf_data;
    manuf_data.company_identifier = 0x0088;
    manuf_data.data.size          = 6;
    manuf_data.data.p_data        = m_addl_adv_manuf_data;

    NRF_LOG_INFO("BLE_WECHAT mac addr.");
    NRF_LOG_HEXDUMP_INFO(manuf_data.data.p_data, manuf_data.data.size);
    init.advdata.p_manuf_specific_data = &manuf_data;
#endif

至此 AirSync 的服務相關代碼實現完畢,剩下的是協議方面的處理;

相關文章
相關標籤/搜索