Bluetooth Low Energy-Werbung

Bluetooth Low Energy (BLE) spart Energie, da es die meiste Zeit im Ruhemodus bleibt. Das Gerät wird nur für Werbung und kurze Verbindungen aktiviert. Werbung wirkt sich also sowohl auf den Stromverbrauch als auch auf die Bandbreite für die Datenübertragung aus.

Bluetooth 5-Werbeerweiterung

Android 8.0 unterstützt Bluetooth 5, was Verbesserungen bei der Übertragung und flexible Datenübertragung für BLE ermöglicht. Bluetooth 5 unterstützt BLE-Physical Layers (PHYs), die den geringen Stromverbrauch von Bluetooth 4.2 beibehalten und Nutzern die Möglichkeit geben, zwischen erhöhter Bandbreite oder Reichweite zu wählen. Weitere Informationen finden Sie in den Bluetooth 5 Core Specifications.

Implementierung

Neue Bluetooth 5-Funktionen sind automatisch für Geräte mit Android 8.0 und kompatiblen Bluetooth-Controllern verfügbar. Mit den folgenden BluetoothAdapter-Methoden können Sie prüfen, ob ein Gerät Bluetooth 5-Funktionen unterstützt:

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

Wenn Sie die Werbefunktionen deaktivieren möchten, müssen Sie den Bluetooth-Chipanbieter bitten, die Unterstützung für den Chipsatz zu deaktivieren.

Die Bluetooth-PHYs schließen sich gegenseitig aus und das Verhalten jedes PHY wird von der Bluetooth SIG vorgegeben. Standardmäßig wird in Android 8.0 der Bluetooth LE 1M PHY aus Bluetooth 4.2 verwendet. Das Paket android.bluetooth.le stellt die Bluetooth 5-Werbefunktionen über diese APIs zur Verfügung:

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

Erstellen Sie ein AdvertisingSet, um die Einstellungen für Bluetooth-Marketingmitteilungen mit der Methode startAdvertisingSet() in android.bluetooth.le.BluetoothLeAdvertiser zu ändern. Auch wenn die Unterstützung für Bluetooth 5 oder seine Werbefunktionen deaktiviert ist, können die API-Funktionen auch für LE 1M PHY gelten.

Beispiele

In dieser Beispiel-App wird Bluetooth LE 1M PHY für die Werbung verwendet:

  // 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);
}

In dieser Beispiel-App wird der BLE 2M PHY für die Werbung verwendet. Die App prüft zuerst, ob das Gerät die verwendeten Funktionen unterstützt. Wenn die Werbefunktionen unterstützt werden, konfiguriert die App BLE 2M PHY als primären PHY. Wenn 2M PHY aktiv ist, werden Bluetooth 4.x-Controller nicht unterstützt. Daher wird setLegacyMode auf false gesetzt. In diesem Beispiel werden Parameter während der Werbung geändert und die Werbung wird pausiert.

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 can 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 - for example, 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);
}

Bestätigung

Führen Sie die entsprechenden Bluetooth-Produkttests aus, um die Gerätekompatibilität mit Bluetooth 5 zu prüfen.