Pubblicità Bluetooth a basso consumo energetico

Bluetooth Low Energy (BLE) risparmia energia rimanendo in modalità sospensione per la maggior parte del tempo. Si attiva solo per creare annunci pubblicitari e connessioni brevi, quindi gli annunci pubblicitari influiscono sia sul consumo energetico che sulla larghezza di banda del trasferimento dati.

Estensione pubblicitaria Bluetooth 5

Android 8.0 supporta Bluetooth 5, che fornisce miglioramenti alla trasmissione e pubblicità dati flessibile per BLE. Bluetooth 5 supporta i livelli fisici BLE (PHY) che mantengono il consumo energetico ridotto di Bluetooth 4.2 e consentono agli utenti di scegliere una maggiore larghezza di banda o portata. Maggiori informazioni sono disponibili nelle specifiche Bluetooth 5 Core .

Implementazione

Le nuove funzionalità Bluetooth 5 sono automaticamente disponibili per i dispositivi con Android 8.0 con controller Bluetooth compatibili. Utilizza questi metodi BluetoothAdapter per verificare se un dispositivo supporta le funzionalità Bluetooth 5:

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

Per disattivare le funzionalità pubblicitarie, collaborare con il fornitore del chip Bluetooth per disattivare il supporto del chipset.

I PHY Bluetooth sono esclusivi l'uno dell'altro e il comportamento di ciascun PHY è predefinito dal Bluetooth SIG. Per impostazione predefinita, Android 8.0 utilizza Bluetooth LE 1M PHY, da Bluetooth 4.2. Il pacchetto android.bluetooth.le espone le funzionalità pubblicitarie di Bluetooth 5 tramite queste API:

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

Crea un AdvertisingSet per modificare le impostazioni degli annunci pubblicitari Bluetooth utilizzando il metodo startAdvertisingSet() in android.bluetooth.le.BluetoothLeAdvertiser . Anche se il supporto per Bluetooth 5 o le sue funzionalità pubblicitarie è disabilitato, le funzionalità API possono applicarsi anche a LE 1M PHY.

Esempi

Questa app di esempio utilizza Bluetooth LE 1M PHY per la pubblicità:

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

Questa app di esempio utilizza BLE 2M PHY per la pubblicità. L'app verifica innanzitutto che il dispositivo supporti le funzionalità utilizzate. Se le funzionalità pubblicitarie sono supportate, l'app configura BLE 2M PHY come PHY primario. Mentre 2M PHY è attivo, la pubblicità non supporta i controller Bluetooth 4.x, quindi setLegacyMode è impostato su false . Questo esempio modifica i parametri durante la pubblicità e mette anche in pausa la pubblicità.

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

Verifica

Esegui test di prodotto Bluetooth applicabili per verificare la compatibilità del dispositivo con Bluetooth 5.

AOSP contiene Android Comms Test Suite (ACTS), che include test per Bluetooth 5. I test ACTS per Bluetooth 5 sono disponibili in tools/test/connectivity/acts/tests/google/ble/bt5 .