Suitable For
- 有 Programming 經驗,但沒有接觸過 BLE
- 想要透過 BLE 來取得資料,但不知道從何下手
- 著重說明理論 與 如何取得資料
Concepts
Generic Access Profile (GAP)
在藍牙核心規範中提到 Generic Access Profile (GAP), GAP 主要負責控制藍牙裝置的 連接模式 和 可見性,同時定義了藍牙裝置如何發現其他裝置、如何建立和終止連接,也管理著裝置的角色(如中央設備或外圍設備)、模式(如可發現模式或連接模式)和安全機制(如配對和加密)
在 GAP 中定義了四種角色,分別為:
- Broadcaster
- 透過廣播的方式廣播資料,不可連線,但可以被掃描到
- Observer
- 利用掃描方式來取得 Broadcaster 廣播的資料
- Peripheral
- 接受 Central 的連線,並提供資料給 Central
- Central
- 創建與 Peripheral 的連線,並取得資料
其中 Central 與 Peripheral 是本次說明的重點, Peripheral 代表周邊的藍牙裝置(溫度/溼度計), Central 則是接受資料的藍牙裝置,例如:手機、電腦等等。
GAP 相關的實作內容通常由藍牙晶片廠商或操作系統處理,本篇專注於資料介接,不討論連接模式、可見性等等的問題
未來有機會再另開一篇討論
Attribute Protocol (ATT)
GATT 是主要討論的重點,GATT 是一種基於 ATT(Attribute Protocol)實現,在介紹 GATT 之前,先介紹 ATT
ATT 主要的格式是 Attritbute ,每個 Attribute 由三個部分組成:
- 16-bit 的 handle
- 一個數字,用來識別 Attribute
- 定義 Attribute Types 的 UUID
- 表示裝置公開狀態資訊的 Attribute Value
ATT 本身不定義 Attribute Types 具體的意義,此部分由更高層 ( higher-level ) 的 Profiles、GATT 定義
Attribute 可能包含安全性設定,這個設定存在 Attribute Value 中,ATT 本身不處理這個部分,而是由更高層的 Profiles、GATT 負責
大部分的 Attribute Protocol 是採用 Client / Server 模型
Client ( GAP 中的 Central, 手機、電腦等 ) 會發送 Request 給 Server ( GAP 中的 Peripheral, 周邊藍芽裝置 ) ,Server 則會回傳 Response 給 Client
Generic Attribute Profile (GATT)
ATT 是一個非常通用的協定,簡單但是在多服務的裝置上可能會有衝突產生
GATT 則是在 ATT 的基礎上定義了一個層級的結構,並且給予了一堆 ATT Attributes 具體的意義
GATT Profile 由三個部分組成:
- Service
- Characteristic
- Descriptor
結構的概念如下圖:
[3]
最外層是一個 Profile,Profile 由一個或多個 Service 組成
每個 Service 中會有多個 Characteristics,每個 Characteristics 中會有一個 Properties 和 Value 以及 多個 Descriptors
Profiles
Profile 是一個集合,包含了一個或多個 Services,這些 Services 可能由 Bluetooth SIG 定義,也可能是由開發者自行定義
Profile 通常會定義一個特定的應用,例如:心率監測、體重計、溫度計等等,像是 Heart Rate Profile 中包含了 Heart Rate Service 和 Device Information Service,並描述了這種情境下各自的 Roles 需要做甚麼、連線建立的流程等等
Services
Service 也是一個集合,包含了一個或多個 Characteristics,具體描述其中的內容,例如:
- Service 是做甚麼用的
- 傳輸的依賴 ( Transport Dependencies )
- 錯誤的代碼 ( Error Codes )
- Service 的宣告 ( Service Declaration )
- Service 包含哪些 Characteristics
- …
例如 Heart Rate Service 中的 Service Declaration 如下:
[5]
Service Characteristics 描述如下
[5]
說明這個 Service 底下包含以下 Characteristics 以及其所需要實現的功能:
- Heart Rate Measurement
- Heart Rate Measurement Client Characteristic Configuration descriptor
- Body Sensor Location
- Heart Rate Control Point
Characteristics
Characteristics 就是具體資料如何傳輸的描述,同樣以上面的 Heart Rate Service 為例,其部分 Characteristics 定義如下:
[5]
可以看到其中描述了 Heart Rate Measurement 這個 Characteristics 包含了:
- heart reate measurement value field
- energy expended field
- RR-Interval field
- 解釋了 RR-Interval field 的意義是 Electrocardiogram (ECG) 波形中兩個連續 R 波
其部分 Flags Field 定義如下:
[5]
說明了這個 Flags Field 的第一個 bit 代表 Heart Rate Measurement Value field 是使用 UINT8 還是 UINT16 來表示
如果是 UINT8 則為 0,如果是 UINT16 則為 1
後面還有 Energy Expended Field, RR-Interval Field 等等
Descriptors
在上述範例中還有描述 Characteristic Descriptors 的內容
[5]
其中的具體意義描述在
[5]
Summary
介紹了 BLE 會利用 GAP 來建立連線,並利用 GATT 來傳輸資料
而 GATT Profiles 是一個應用情境,其中包含多個 Services
Service 包含了一個或多個 Characteristics,具體描述其中的 Characteristic, Descriptor 的內容
實際上使用時,會透過 ATT Handle 來取得 Characteristic, Descriptor 的資料
並依照 Service 中描述的格式來解析並取得資料
下一篇會具體介紹如何取得資料
Reference
- https://learn.adafruit.com/introduction-to-bluetooth-low-energy?view=all#gatt
- https://epxx.co/artigos/bluetooth_gatt.html
- https://www.bluetooth.com/specifications/specs/core-specification-4-0/
- https://www.bluetooth.com/specifications/specs/heart-rate-profile-1-0/
- https://www.bluetooth.com/specifications/specs/heart-rate-service-1-0/