Bluetooth Low Energy のアドバタイジング

Bluetooth Low Energy(BLE)は、ほとんどの時間にスリープモードを維持することで電力を節約します。スリープモードから復帰するのはアドバタイズメントと短時間の接続のためのみであることから、アドバタイズメントが消費電力とデータ転送帯域幅の両方に影響します。

Bluetooth 5 のアドバタイジング拡張

Android 8.0 は Bluetooth 5 をサポートしています。Bluetooth 5 では、BLE のブロードキャストの向上と柔軟なデータ アドバタイズメントが実現されています。Bluetooth 4.2 で導入された消費電力を抑制する BLE 物理レイヤ(PHY)をサポートしており、ユーザーは帯域幅または範囲のいずれかの拡大を選択できます。詳細については、Bluetooth 5 コア仕様をご覧ください。

実装

Bluetooth 5 の新機能は、互換性のある Bluetooth コントローラを備えた Android 8.0 デバイスであれば、自動的に利用可能となります。以下の BluetoothAdapter メソッドを使用すると、デバイスが Bluetooth 5 の機能に対応しているかどうかをチェックできます。

  • isLe2MPhySupported()
  • isLeCodedPhySupported()
  • isLeExtendedAdvertisingSupported()
  • isLePeriodicAdvertisingSupported()

アドバタイジング機能を無効にするには、Bluetooth チップベンダーと連携してチップセットのサポートを無効にします。

Bluetooth の PHY は互いに排他的であり、各 PHY の動作は Bluetooth SIG によって事前に定義されています。デフォルトでは、Android 8.0 は Bluetooth 4.2 以降で導入されている Bluetooth LE 1M PHY を使用します。 android.bluetooth.le パッケージでは、Bluetooth 5 のアドバタイジング機能を以下の API を通して公開しています。

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

Bluetooth のアドバタイズメント設定を変更するには、 android.bluetooth.le.BluetoothLeAdvertiser startAdvertisingSet() メソッドを使用して、 AdvertisingSet を作成します。Bluetooth 5 またはそのアドバタイジング機能のサポートが無効になっている場合でも、API の機能を LE 1M PHY に適用することが可能です。

このサンプルアプリでは、Bluetooth LE 1M PHY をアドバタイジングに使用しています。

  // Start legacy advertising. Works for devices with 5.x controllers,
  and devices that support multi-advertising.

  void example1() {
   BluetoothLeAdvertiser advertiser =
      BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();

   AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder())
           .setLegacyMode(true) // True by default, but set here as a reminder.
           .setConnectable(true)
           .setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
           .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
           .build();

   AdvertiseData data = (new AdvertiseData.Builder()).setIncludeDeviceName(true).build();

   AdvertisingSetCallback callback = new AdvertisingSetCallback() {
       @Override
       public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
           Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
             + status);
           currentAdvertisingSet = advertisingSet;
       }

       @Override
       public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
           Log.i(LOG_TAG, "onAdvertisingDataSet() :status:" + status);
       }

       @Override
       public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {
           Log.i(LOG_TAG, "onScanResponseDataSet(): status:" + status);
       }

       @Override
       public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
           Log.i(LOG_TAG, "onAdvertisingSetStopped():");
       }
   };

   advertiser.startAdvertisingSet(parameters, data, null, null, null, callback);

   // After onAdvertisingSetStarted callback is called, you can modify the
   // advertising data and scan response data:
   currentAdvertisingSet.setAdvertisingData(new AdvertiseData.Builder().
     setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build());
   // Wait for onAdvertisingDataSet callback...
   currentAdvertisingSet.setScanResponseData(new
     AdvertiseData.Builder().addServiceUuid(new ParcelUuid(UUID.randomUUID())).build());
   // Wait for onScanResponseDataSet callback...

   // When done with the advertising:
   advertiser.stopAdvertisingSet(callback);
}

次のサンプルアプリでは、BLE 2M PHY をアドバタイジングに使用しています。アプリでは、使用する機能がデバイスでサポートされているかをまずチェックします。アドバタイジング機能がサポートされていれば、アプリは BLE 2M PHY をプライマリ PHY として構成します。2M PHY はアクティブですが、アドバタイズメントは Bluetooth 4.x コントローラをサポートしていません。したがって、setLegacyModefalse に設定されます。このサンプルでは、アドバタイジング中にパラメータを変更し、さらにアドバタイズメントを一時停止します。

void example2() {
   BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
   BluetoothLeAdvertiser advertiser =
     BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();

   // Check if all features are supported
   if (!adapter.isLe2MPhySupported()) {
       Log.e(LOG_TAG, "2M PHY not supported!");
       return;
   }
   if (!adapter.isLeExtendedAdvertisingSupported()) {
       Log.e(LOG_TAG, "LE Extended Advertising not supported!");
       return;
   }

   int maxDataLength = adapter.getLeMaximumAdvertisingDataLength();

   AdvertisingSetParameters.Builder parameters = (new AdvertisingSetParameters.Builder())
           .setLegacyMode(false)
           .setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
           .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
           .setPrimaryPhy(BluetoothDevice.PHY_LE_1M)
           .setSecondaryPhy(BluetoothDevice.PHY_LE_2M);

   AdvertiseData data = (new AdvertiseData.Builder()).addServiceData(new
     ParcelUuid(UUID.randomUUID()),
           "You should be able to fit large amounts of data up to maxDataLength. This goes
           up to 1650 bytes. For legacy advertising this would not
           work".getBytes()).build();

   AdvertisingSetCallback callback = new AdvertisingSetCallback() {
       @Override
       public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
           Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
            + status);
           currentAdvertisingSet = advertisingSet;
       }

       @Override
       public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
           Log.i(LOG_TAG, "onAdvertisingSetStopped():");
       }
   };

   advertiser.startAdvertisingSet(parameters.build(), data, null, null, null, callback);

   // After the set starts, you can modify the data and parameters of currentAdvertisingSet.
   currentAdvertisingSet.setAdvertisingData((new
     AdvertiseData.Builder()).addServiceData(new ParcelUuid(UUID.randomUUID()),
           "Without disabling the advertiser first, you can set the data, if new data is
            less than 251 bytes long.".getBytes()).build());

   // Wait for onAdvertisingDataSet callback...

   // Can also stop and restart the advertising
   currentAdvertisingSet.enableAdvertising(false, 0, 0);
   // Wait for onAdvertisingEnabled callback...
   currentAdvertisingSet.enableAdvertising(true, 0, 0);
   // Wait for onAdvertisingEnabled callback...

   // Or modify the parameters - i.e. lower the tx power
   currentAdvertisingSet.enableAdvertising(false, 0, 0);
   // Wait for onAdvertisingEnabled callback...
   currentAdvertisingSet.setAdvertisingParameters(parameters.setTxPowerLevel
     (AdvertisingSetParameters.TX_POWER_LOW).build());
   // Wait for onAdvertisingParametersUpdated callback...
   currentAdvertisingSet.enableAdvertising(true, 0, 0);
   // Wait for onAdvertisingEnabled callback...

   // When done with the advertising:
   advertiser.stopAdvertisingSet(callback);
}

検証

適切な Bluetooth プロダクト テストを実施して、デバイスと Bluetooth 5 の互換性を検証します。

AOSP には、Bluetooth 5 用のテストを含む Android Comms テストスイート(ACTS)が用意されています。Bluetooth 5 用の ACTS テストは、 tools/test/connectivity/acts/tests/google/ble/bt5 にあります。