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] 規格也將這些值參照為命名代碼附錄中的
MDC_DEV_SPEC_PROFILE_*。如要進一步瞭解 HDP,請參閱健康裝置設定檔。
以下是使用設定檔的基本步驟:
- 按照「藍牙設定」一節的說明,取得預設轉接器。
- 設定
BluetoothProfile.ServiceListener。當BluetoothProfile用戶端連線至服務或與服務中斷連線時,這個事件監聽器會通知用戶端。 - 使用
getProfileProxy()建立與設定檔相關聯的設定檔 Proxy 物件連線。在以下範例中,設定檔 Proxy 物件是BluetoothHeadset的例項。 - 在
onServiceConnected()中,取得設定檔 Proxy 物件的控制代碼。 - 取得設定檔 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(BluetoothProfile.HEADSET, bluetoothHeadset);
供應商專屬 AT 指令
應用程式可以註冊接收耳機傳送的預先定義供應商專屬 AT 指令系統廣播訊息 (例如 Plantronics +XEVENT 指令)。舉例來說,應用程式可以接收廣播,指出連結裝置的電池電量,並視需要通知使用者或採取其他動作。為 ACTION_VENDOR_SPECIFIC_HEADSET_EVENT 意圖建立廣播接收器,處理耳機的供應商專屬 AT 指令。
健康裝置設定檔
Android 支援藍牙健康裝置設定檔 (HDP)。Bluetooth Health API 包含 BluetoothHealth、BluetoothHealthCallback 和 BluetoothHealthAppConfiguration 類別,詳情請參閱「重要類別和介面」。
使用 Bluetooth Health API 時,建議您瞭解以下 HDP 重要概念:
- 來源
- 體重計、血糖儀或溫度計等健康裝置,可將醫療資料傳輸至 Android 手機或平板電腦等智慧型裝置。
- 接收器
- 接收醫療資料的智慧型裝置。在 HDP 應用程式中,接收器會以
BluetoothHealthAppConfiguration物件表示。 - 註冊
- 用於註冊接收器,以便與特定健康裝置通訊的程序。
- 連線
- 在健康裝置 (來源) 和智慧型裝置 (接收端) 之間開啟通道的程序。
建立 HDP 應用程式
以下是建立 HDP 應用程式的基本步驟:
取得
BluetoothHealthProxy 物件的參照。與一般耳機和 A2DP 設定檔裝置一樣,您必須使用BluetoothProfile.ServiceListener和HEALTH設定檔類型呼叫getProfileProxy(),才能與設定檔 Proxy 物件建立連線。建立
BluetoothHealthCallback並註冊應用程式設定 (BluetoothHealthAppConfiguration),做為健康狀態接收器。與健康裝置建立連線。
成功連線至健康裝置後,請使用檔案描述元讀取及寫入健康裝置。接收到的資料必須使用健康管理工具解讀,該工具會實作 IEEE 11073 規格。
完成後,請關閉健康狀態管道並取消註冊應用程式。如果長時間沒有活動,管道也會關閉。