藍牙設定檔

Bluetooth API 包含使用藍牙設定檔的支援。藍牙設定檔是裝置之間的藍牙介面規格 (例如免持功能設定檔),可用於進行藍牙通訊。為了讓行動裝置連線至無線耳機,兩部裝置都必須支援手持式設定檔。

Bluetooth API 提供下列藍牙設定檔的實作方式:

  • 耳機。耳機設定檔支援搭配手機使用的藍牙耳機。Android 提供 BluetoothHeadset 類別,這是用於控制藍牙頭戴式服務的 Proxy。這包括藍牙耳機和免持功能 (v1.5) 設定檔。BluetoothHeadset 類別支援 AT 指令。如要進一步瞭解這個主題,請參閱供應商專屬的 AT 指令
  • A2DP。進階音訊發布設定檔 (A2DP) 設定檔會定義如何透過藍牙連線將高品質音訊從一個裝置串流至另一部裝置。Android 提供 BluetoothA2dp 類別,這是用於控制藍牙 A2DP 服務的 Proxy。
  • 健康裝置。Android 支援藍牙健康裝置設定檔 (HDP)。這可讓您建立使用藍牙與支援藍牙的健康裝置 (例如心率監測器、血計、溫度計、體重計等) 的應用程式進行通訊的應用程式。如需支援的裝置及其對應裝置資料專業化代碼的清單,請參閱藍牙的 HDP 裝置資料專業認證。這些值也會在 ISO/IEEE 11073-20601 [7] 規格中參照為 Nomenclature Codes 附錄中的 MDC_DEV_SPEC_PROFILE_*。如要進一步瞭解 HDP,請參閱「健康裝置設定檔」。

使用付款資料的基本步驟如下:

  1. 取得預設轉接器,如藍牙設定所述。
  2. 設定 BluetoothProfile.ServiceListener。當 BluetoothProfile 用戶端連線至服務或中斷連線時,這個事件監聽器會通知該用戶端。
  3. 請使用 getProfileProxy() 建立與設定檔相關聯的設定檔 Proxy 物件連線。在以下範例中,設定檔 Proxy 物件是 BluetoothHeadset 的例項。
  4. onServiceConnected() 中,取得設定檔 Proxy 物件的控制代碼。
  5. 取得設定檔 Proxy 物件後,請使用這個物件監控連線狀態,並執行與該設定檔相關的其他作業。

下列程式碼片段說明如何連線至 BluetoothHeadset Proxy 物件,以便控制耳機設定檔:

Kotlin

var bluetoothHeadset: BluetoothHeadset? = null

// Get the default adapter
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

private val profileListener = object : BluetoothProfile.ServiceListener {

    override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = proxy as BluetoothHeadset
        }
    }

    override fun onServiceDisconnected(profile: Int) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = null
        }
    }
}

// Establish connection to the proxy.
bluetoothAdapter?.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET)

// ... call functions on bluetoothHeadset

// Close proxy connection after use.
bluetoothAdapter?.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset)

Java

BluetoothHeadset bluetoothHeadset;

// Get the default adapter
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

private BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = (BluetoothHeadset) proxy;
        }
    }
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = null;
        }
    }
};

// Establish connection to the proxy.
bluetoothAdapter.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET);

// ... call functions on bluetoothHeadset

// Close proxy connection after use.
bluetoothAdapter.closeProfileProxy(bluetoothHeadset);

供應商專屬的 AT 指令

應用程式可以註冊,以接收由耳機 (例如 Plantronics +XEVENT 指令) 傳送的預先定義廠商 AT 指令的系統廣播訊息。舉例來說,應用程式可以接收廣播訊息,指出已連結裝置的電池電量,並在必要時通知使用者或採取其他動作。為 ACTION_VENDOR_SPECIFIC_HEADSET_EVENT 意圖建立廣播接收器,為耳機處理廠商專屬的 AT 指令。

健康裝置設定檔

Android 支援藍牙健康裝置設定檔 (HDP)。Bluetooth Health API 包含 BluetoothHealthBluetoothHealthCallbackBluetoothHealthAppConfiguration 類別,詳情請參閱「重要類別和介面」

使用 Bluetooth Health API 時,瞭解下列主要 HDP 概念會很有幫助:

來源
健康裝置 (例如體重體重計、葡萄糖測量儀或溫度計) 可將醫療資料傳輸至智慧型裝置,例如 Android 手機或平板電腦。
水槽
負責接收醫療資料的智慧型裝置。在 HDP 應用程式中,接收器會以 BluetoothHealthAppConfiguration 物件表示。
註冊
用於註冊接收器的程序,以與特定健康裝置通訊。
情感交流
在健康裝置 (來源) 和智慧型裝置 (接收器) 之間開啟管道的程序。

建立 HDP 應用程式

以下是建立 HDP 應用程式的基本步驟:

  1. 取得 BluetoothHealth Proxy 物件的參照。與一般頭戴式裝置和 A2DP 設定檔裝置一樣,您必須使用 BluetoothProfile.ServiceListenerHEALTH 設定檔類型呼叫 getProfileProxy(),才能與設定檔 Proxy 物件建立連線。

  2. 建立 BluetoothHealthCallback,並註冊做為健康狀態接收器的應用程式設定 (BluetoothHealthAppConfiguration)。

  3. 與健康裝置建立連線。

  4. 成功連線至健康裝置後,請使用檔案描述元讀取及寫入健康裝置。收到的資料需要使用健康管理員 (實作 IEEE 11073 規格) 來解讀。

  5. 完成後,關閉健康管道並取消註冊應用程式。當頻道長時間閒置時,系統也會關閉管道。