Bluetooth BLE in Android

一、 BLE

現在低功耗藍牙(BLE)連接都是建立在 GATT (Generic Attribute Profile) 協議之上。GATT 是一個在藍牙連接之上的發送和接收很短的數據段的通用規範,這些很短的數據段被稱爲屬性(Attribute)。

二、 GAP

詳細介紹 GATT 之前,需要了解 GAP(Generic Access Profile),它在用來控制設備連接和廣播。GAP 使你的設備被其他設備可見,並決定了你的設備是否可以或者怎樣與合同設備進行交互。例如 Beacon 設備就只是向外廣播,不支持連接,小米手環就等設備就可以與中心設備連接。

1. 設備角色

GAP 給設備定義了若干角色,其中主要的兩個是:外圍設備(Peripheral)和中心設備(Central)。

  • 外圍設備:這一般就是非常小或者簡單的低功耗設備,用來提供數據,並連接到一個更加相對強大的中心設備。例如小米手環。
  • 中心設備:中心設備相對比較強大,用來連接其他外圍設備。例如手機等。

microcontrollers_BroadcastTopology.png

 

三、GATT

GATT 的全名是 Generic Attribute Profile(姑且翻譯成:普通屬性協議),它定義兩個 BLE 設備通過叫做 Service 和 Characteristic 的東西進行通信。GATT 就是使用了 ATT(Attribute Protocol)協議,ATT 協議把 Service, Characteristic遺蹟對應的數據保存在一個查找表中,次查找表使用 16 bit ID 作爲每一項的索引。

一旦兩個設備建立起了連接,GATT 就開始起作用了,這也意味着,你必需完成前面的 GAP 協議。這裏需要說明的是,GATT 連接,必需先經過 GAP 協議。實際上,我們在 Android 開發中,可以直接使用設備的 MAC 地址,發起連接,可以不經過掃描的步驟。這並不意味不需要經過 GAP,實際上在芯片級別已經給你做好了,藍牙芯片發起連接,總是先掃描設備,掃描到了纔會發起連接。

GATT 連接需要特別注意的是:GATT 連接是獨佔的。也就是一個 BLE 外設同時只能被一箇中心設備連接。一旦外設被連接,它就會馬上停止廣播,這樣它就對其他設備不可見了。當設備斷開,它又開始廣播。

中心設備和外設需要雙向通信的話,唯一的方式就是建立 GATT 連接。

microcontrollers_ConnectedTopology.png

一個外設只能連接一箇中心設備,而一箇中心設備可以連接多個外設。一旦建立起了連接,通信就是雙向的了,對比前面的 GAP 廣播的網絡拓撲,GAP 通信是單向的。如果你要讓兩個設備外設能通信,就只能通過中心設備中轉。

GATT 事務是建立在嵌套的Profiles, Services 和 Characteristics之上的

microcontrollers_GattStructure.png

  • Profile Profile 並不是實際存在於 BLE 外設上的,它只是一個被 Bluetooth SIG 或者外設設計者預先定義的 Service 的集合。例如心率Profile(Heart Rate Profile)就是結合了 Heart Rate Service 和 Device Information Service。所有官方通過 GATT Profile 的列表可以從這裏找到。

  • Service Service 是把數據分成一個個的獨立邏輯項,它包含一個或者多個 Characteristic。每個 Service 有一個 UUID 唯一標識。 UUID 有 16 bit 的,或者 128 bit 的。16 bit 的 UUID 是官方通過認證的,需要花錢購買,128 bit 是自定義的,這個就可以自己隨便設置。

  • Characteristic 在 GATT 事務中的最低界別的是 Characteristic,Characteristic 是最小的邏輯數據單元,當然它可能包含一個組關聯的數據,例如加速度計的 X/Y/Z 三軸值。

 

Here are the roles and responsibilities that apply when an Android device interacts with a BLE device:

  • Central vs. peripheral. This applies to the BLE connection itself. The device in the central role scans, looking for advertisement, and the device in the peripheral role makes the advertisement.
  • GATT server vs. GATT client. This determines how two devices talk to each other once they've established the connection.

To understand the distinction, imagine that you have an Android phone and an activity tracker that is a BLE device. The phone supports the central role; the activity tracker supports the peripheral role (to establish a BLE connection you need one of each—two things that only support peripheral couldn't talk to each other, nor could two things that only support central).

Once the phone and the activity tracker have established a connection, they start transferring GATT metadata to one another. Depending on the kind of data they transfer, one or the other might act as the server. For example, if the activity tracker wants to report sensor data to the phone, it might make sense for the activity tracker to act as the server. If the activity tracker wants to receive updates from the phone, then it might make sense for the phone to act as the server.

In the example used in this document, the Android app (running on an Android device) is the GATT client. The app gets data from the GATT server, which is a BLE heart rate monitor that supports the Heart Rate Profile. But you could alternatively design your Android app to play the GATT server role. See BluetoothGattServer for more information.

 

總結一下: phone                       BLE device(小米手環)

                    central                     peripheral

                    scan                        advertiser

                    gatt server   ===》  gatt client

                    gatt client   《===  gatt server   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章