Bluetooth プロファイル

Bluetooth API は、Bluetooth プロファイルの操作をサポートしています。Bluetooth プロファイルは、Hands-Free プロファイルなど、デバイス間の Bluetooth ベースの通信のためのワイヤレス インターフェース仕様です。モバイル デバイスをワイヤレス ヘッドセットに接続するには、両方のデバイスがハンズフリー プロファイルをサポートしている必要があります。

Bluetooth API には、次の Bluetooth プロファイルの実装が用意されています。

  • ヘッドセット: ヘッドセット プロファイルは、スマートフォンで使用する Bluetooth ヘッドセットをサポートします。Android には、Bluetooth ヘッドセット サービスを制御するためのプロキシである BluetoothHeadset クラスが用意されています。これには、Bluetooth ヘッドセットとハンズフリー(v1.5)の両方のプロファイルが含まれます。BluetoothHeadset クラスは AT コマンドをサポートしています。このトピックの詳細については、ベンダー固有の AT コマンドをご覧ください。
  • A2DP。Advanced Audio Distribution Profile(A2DP)プロファイルは、Bluetooth 接続を介してデバイス間で高品質のオーディオをストリーミングする方法を定義します。Android には、Bluetooth A2DP サービスを制御するためのプロキシである BluetoothA2dp クラスが用意されています。
  • Health Device:Android は、Bluetooth Health Device Profile(HDP)をサポートしています。これにより、心拍数モニター、血液計、体温計、体重計など、Bluetooth をサポートする健康機器と Bluetooth で通信するアプリを作成できます。サポートされているデバイスと、対応するデバイスデータ特殊コードのリストについては、Bluetooth の HDP デバイスデータ スペシャライゼーションをご覧ください。これらの値は、ISO/IEEE 11073-20601 [7] 仕様の Nomenclature Codes Annex の 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 コマンドなど)のシステム ブロードキャストを受信するように登録できます。たとえば、アプリは接続済みデバイスのバッテリー残量を示すブロードキャストを受信し、必要に応じてユーザーに通知したり、その他のアクションを実行したりできます。ヘッドセットのベンダー固有の AT コマンドを処理するには、ACTION_VENDOR_SPECIFIC_HEADSET_EVENT インテントのブロードキャスト レシーバを作成します。

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

Android は、Bluetooth Health Device Profile(HDP)をサポートしています。Bluetooth Health API には、主要なクラスとインターフェースで説明されている BluetoothHealthBluetoothHealthCallbackBluetoothHealthAppConfiguration の各クラスが含まれています。

Bluetooth Health API を使用する場合は、次の重要な HDP のコンセプトを理解することをおすすめします。

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

HDP アプリを作成する

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

  1. BluetoothHealth プロキシ オブジェクトへの参照を取得します。通常のヘッドセットや A2DP プロファイル デバイスと同様に、BluetoothProfile.ServiceListenerHEALTH プロファイル タイプを指定して getProfileProxy() を呼び出し、プロファイル プロキシ オブジェクトとの接続を確立する必要があります。

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

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

  4. ヘルスデバイスに正常に接続されたら、ファイル記述子を使用してヘルスデバイスの読み取りと書き込みを行います。受信したデータは、IEEE 11073 仕様を実装したヘルス マネージャーを使用して解釈する必要があります。

  5. 完了したら、ヘルス チャンネルを閉じてアプリの登録を解除します。チャンネルは、非アクティブが長期間続いた場合も終了します。