Bluetooth Düşük Enerji reklamcılığı

Bluetooth Düşük Enerji (BDE), çoğu zaman uyku modunda kalarak güç tasarrufu sağlar. Yalnızca reklam ve kısa bağlantı oluşturmak için uyandığından reklamlar hem güç tüketimini hem de veri aktarımı bant genişliğini etkiler.

Bluetooth 5 reklam uzantısı

Android 8.0, BDE için yayın iyileştirmeleri ve esnek veri reklamı sağlayan Bluetooth 5'i destekler. Bluetooth 5, Bluetooth 4.2'nin düşük güç tüketimini koruyan ve kullanıcıların daha yüksek bant genişliği veya aralık seçmesine olanak tanıyan BDE Fiziksel Katmanları (PHY) destekler. Bluetooth 5 Core Spesifikasyonları bölümünde daha fazla bilgi bulabilirsiniz.

Uygulama

Yeni Bluetooth 5 özellikleri, uyumlu Bluetooth kumandalara sahip Android 8.0 çalıştıran cihazlarda otomatik olarak kullanılabilir. Bir cihazın Bluetooth 5 özelliklerini destekleyip desteklemediğini kontrol etmek için şu BluetoothAdapter yöntemlerinden yararlanın:

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

Reklamcılık özelliklerini devre dışı bırakmak için Bluetooth çip hizmeti sağlayıcısıyla birlikte çalışarak yonga seti desteğini devre dışı bırakın.

Bluetooth PHY'leri birbirini dışlar ve her bir PHY'nin davranışı Bluetooth SIG tarafından önceden tanımlanır. Android 8.0 varsayılan olarak Bluetooth 4.2'den Bluetooth LE 1M PHY'yi kullanır. android.bluetooth.le paketi, şu API'ler aracılığıyla Bluetooth 5 reklamcılık özelliklerini sunar:

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

Bluetooth reklam ayarlarını değiştirmek için android.bluetooth.le.BluetoothLeAdvertiser içinde startAdvertisingSet() yöntemini kullanarak bir AdvertisingSet oluşturun. Bluetooth 5 desteği veya reklamcılık özellikleri devre dışı bırakılmış olsa bile API özellikleri LE 1M PHY için de geçerli olabilir.

Örnekler

Şu örnek uygulamada reklamcılık için Bluetooth LE 1M PHY kullanılmaktadır:

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

Bu örnek uygulama, reklamcılık için BLE 2M PHY yöntemini kullanmaktadır. Uygulama öncelikle cihazın, kullanılan özellikleri destekleyip desteklemediğini kontrol eder. Reklamcılık özellikleri destekleniyorsa uygulama, BDE 2M PHY'yi birincil PHY olarak yapılandırır. 2M PHY etkinken, reklam Bluetooth 4.x denetleyicilerini desteklemez. Bu nedenle setLegacyMode, false olarak ayarlanır. Bu örnek, reklam sırasında parametreleri değiştirir ve reklamı duraklatır.

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

Doğrulama

Cihazın Bluetooth 5 ile uyumluluğunu doğrulamak için geçerli Bluetooth ürün testlerini gerçekleştirin.