Bluetooth プロファイル

Bluetooth API は、Bluetooth プロファイルの操作をサポートしています。 Bluetooth プロファイルは、Bluetooth ベースの無線インターフェース仕様で、 デバイス間通信(ハンズフリー プロファイルなど)にも使用されます。モバイルの場合 ワイヤレス ヘッドセットに接続するには、両方のデバイスが ハンズフリー プロファイル。

Bluetooth API は、以下の Bluetooth の実装を提供します。 プロファイル:

  • ヘッドセット: Headset プロファイルでは、Bluetooth ヘッドセットを 使用されますAndroid は、 BluetoothHeadset クラス、 これは Bluetooth ヘッドセット サービスをコントロールするプロキシです。例 Bluetooth ヘッドセットとハンズフリー(v1.5)プロファイルの両方に対応しています。BluetoothHeadset クラスは AT コマンドをサポートしています。このトピックについて詳しくは、 ベンダー固有の AT コマンド
  • A2DP。Advanced Audio Distribution Profile(A2DP)プロファイルでは、 高音質オーディオを Bluetooth 経由でデバイス間でストリーミングできる 接続しますAndroid は、 BluetoothA2dp クラス: Bluetooth A2DP サービスを制御するプロキシ。
  • ヘルスデバイス:Android は Bluetooth ヘルスデバイスのサポートを提供します。 プロファイル(HDP)。これにより、Bluetooth を使用して通信するアプリを作成できます 心拍数モニター、血液、血液、血液、血液、 メートル、温度計、体重計などが含まれます。サポートされているデバイスと 対応するデバイスデータのスペシャライゼーション コードを確認し、Bluetooth の HDP デバイスのデータ スペシャライゼーションをご覧ください。 これらの値は、ISO/IEEE 11073-20601 [7] 仕様でも参照されています。 命名規約の付録では MDC_DEV_SPEC_PROFILE_* としています。詳細 HDP の詳細については、Health Device Profile をご覧ください。

プロファイルを使用する基本的な手順は次のとおりです。

  1. 次の説明に沿って、デフォルトのアダプターを取得します。 Bluetooth のセットアップ
  2. セットアップする BluetoothProfile.ServiceListener。 このリスナーは、 BluetoothProfile クライアント 接続状態または切断された状態を記録します。
  3. 使用 getProfileProxy() 関連付けられているプロファイル プロキシ オブジェクトへの接続を確立するために、 選択します。次の例では、プロファイル プロキシ オブジェクトは BluetoothHeadset
  4. イン onServiceConnected() プロファイル プロキシ オブジェクトに対するハンドルを取得します。
  5. プロファイル プロキシ オブジェクトを取得したら、そのオブジェクトを使用して、 そのプロファイルに関連するその他の操作を実行できます。

次のコード スニペットは、BluetoothHeadset プロキシに接続する方法を示しています。 これを使用して、ヘッドセット プロファイルを制御できるようにします。

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 コマンド

アプリは、ベンダー固有の事前定義済み AT のシステム ブロードキャストを受信するよう登録できる ヘッドセットから送信されたコマンド(Plantronics +XEVENT コマンドなど)たとえば 接続されているデバイスのバッテリー残量を示すブロードキャストをアプリで受信できる ユーザーに通知したり、必要に応じて他の措置を講じたりできます。ブロードキャストを作成する 受信 ACTION_VENDOR_SPECIFIC_HEADSET_EVENT インテントを使用して、ベンダー固有の AT コマンドを処理できます。

ヘルスデバイス プロファイル

Android は Bluetooth Health Device Profile(HDP)をサポートしています。Bluetooth ヘルス API に含まれるクラス BluetoothHealth BluetoothHealthCallback, および BluetoothHealthAppConfiguration, これについては、主なクラスと 説明します

Bluetooth Health API を使用する際には、以下の主要な HDP について理解しておくと便利です。 概念:

ソース
体重計、血糖値計、体温計など、健康に関する機器。 Android スマートフォンやタブレットなどのスマート デバイスに医療データを送信します。
洗面所
医療データを受信するスマート デバイス。HDP アプリでは、 シンクは BluetoothHealthAppConfiguration オブジェクトで表されます。
登録
特定の健全性と通信するためにシンクを登録するプロセス 提供します
接続
医療機器(ソース)と医療機器の間でチャネルを開くために使用されるプロセス スマート デバイス(シンク)。

HDP アプリを作成する

HDP アプリを作成する基本的な手順は次のとおりです。

  1. BluetoothHealth プロキシ オブジェクトへの参照を取得します。通常の ヘッドセットと A2DP プロファイル デバイスを使用する場合は、getProfileProxy()BluetoothProfile.ServiceListenerHEALTH プロファイル タイプ プロファイル プロキシ オブジェクトとの接続を確立します。

  2. BluetoothHealthCallback を作成してアプリ構成を登録する (BluetoothHealthAppConfiguration)。ヘルスシンクとして機能します。

  3. ヘルスデバイスへの接続を確立します。

  4. ヘルスデバイスに正常に接続されると、ヘルスデータを読み書きします デバイスと通信できるようになります。受信したデータを解釈する必要がある Health Manager を使用して、IEEE 11073 仕様をご覧ください。

  5. 完了したら、健康チャンネルを閉じて、アプリの登録を解除します。このチャンネルは 非アクティブ状態が長期間続くと終了します。