一、介紹
BLEGATT是基於藍牙 Low Energy 技術的一種通信協議。BLEGATT協議提供了一種通用的方式來連接多個不同的設備,將數據傳輸到和從這些設備中讀取數據。這個協議是使用搭建在標準藍牙技術之上的標準,具有可靠性、低功耗和安全性。
二、BLEGATT的架構
BLEGATT協議之間的通信採用了基於客戶端和服務端的數據模型。通常情況下,服務端提供數據和操作,而客戶端將請求發送到服務端,等待查詢結果。服務端可以報告數據更新、新事件或通知客戶端需要採取行動的事件。在BLEGATT的框架下,數據模型包含一個服務層,服務層包含特徵層,特徵層包含描述符。每一個層級的數據模型都包含一些數字屬性,如UUID,用於標識數據層級和服務類型。通過一個專用的區分符號(“:”)來將這些屬性從服務層到數據層逐個接收成鏈接起來:
/*
* Note: The fields of this struct are structured in such a way that
* the common members with the GattCharacteristic struct are all
* contiguous, which makes the two structs easy to correlate in the code.
*
* If additions are made to this struct please consider if the way it's
* laid out makes it harder to read.
*/
struct GattAttribute {
UUID uuid;
GattAttribute *next;
GattAttribute *prev;
GattAttributeType_t type : 8;
uint16_t value_handle;
uint16_t decl_handle;
uint16_t *cached_value_attr_data;
uint16_t cached_value_attr_len;
uint8_t *user_value_buf;
uint16_t user_value_buf_len;
uint16_t handle;
};
三、BLEGATT服務類型
BLEGATT服務類型 經常會被認為是一種用於互聯網的服務,實際上,BLEGATT服務是操作BLE設備的一個基本單元。每個服務代表一個可通過BLE進行訪問的可用數據集合。這些數據集合可以是任何類型和大小的數據類型,包括字符、浮點數等。你可以使用BLEGATT協議訪問這些數據,操作設備或從設備讀取數據。
四、BLEGATT特性和描述符
BLEGATT特性和描述符是兩種不同的數據類型,這兩種數據類型分別用於服務中的數據元素和數據元素屬性。特性是數據元素的基本單位,通常可以是浮點數、整數或字符串。描述符是用於定義特性某些屬性及其行為的數據元素。你可以使用BLEGATT協議來自定義服務中的所有內容,包括特性和描述符。
class GattCharacteristic {
public:
enum {
BLE_GATT_CHAR_PROPERTIES_NONE = 0x00, /**< Characteristic has no property set. */
BLE_GATT_CHAR_PROPERTIES_BROADCAST = 0x01, /**< Characteristic is broadcastable. */
BLE_GATT_CHAR_PROPERTIES_READ = 0x02, /**< Characteristic is readable. */
BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE = 0x04, /**< Characteristic can be written without response. */
BLE_GATT_CHAR_PROPERTIES_WRITE = 0x08, /**< Characteristic can be written. */
BLE_GATT_CHAR_PROPERTIES_NOTIFY = 0x10, /**< Characteristic can be notified. */
BLE_GATT_CHAR_PROPERTIES_INDICATE = 0x20, /**< Characteristic can be indicated. */
BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES = 0x40, /**< Characteristic requires authentication for write access. */
BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES = 0x80 /**< Characteristic has extended properties. */
};
enum {
REPORT_REF_DEFAULT_VAL_HANDLE_RANGE_START = 0x0001,
REPORT_REF_DEFAULT_VAL_HANDLE_RANGE_END = 0xffff
};
enum {
CCCD_DEFAULT_VAL_HANDLE_RANGE_START = 0x0001,
CCCD_DEFAULT_VAL_HANDLE_RANGE_END = 0xffff
};
/**
* Constructor.
*
* @param[in] uuid The UUID of this characteristic. You can use @p UUID::ShortUUIDBytes_t to create
* a UUID from a 16-bit bluetooth.org assigned number.
* @param[in] valuePtr The initial value of the characteristic
* @param[in] valueSize The size of the initial value.
* @param[in] valueCapacity The maximum capacity of the value attribute (default: @p valueSize + 1); including null-termination.
* @param[in] security The security requirement associated with this characteristic (default: SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM)
* @param[in] properties The property of this characteristic (default:
* BLE_GATT_CHAR_PROPERTIES_READ | BLE_GATT_CHAR_PROPERTIES_WRITE)
*/
GattCharacteristic(UUID uuid, uint8_t *valuePtr = NULL, uint16_t valueLength = 0,
uint16_t valueCapacity = 1, uint8_t properties = BLE_GATT_CHAR_PROPERTIES_READ,
SecurityManager::SecurityMode_t security = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
};
五、BLEGATT的應用
BLEGATT協議在智能家居和醫療監測等領域應用廣泛,可以幫助開發人員在手錶上實現讀取心率數據、與智能家居設備通信等功能,同時低功耗的設計也讓 BLEGATT 協議在 IoT 等資源有限的場景中極具優勢。
原創文章,作者:ZGRZ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130950.html