تبلیغات کم انرژی بلوتوث

بلوتوث کم انرژی (BLE) با ماندن در حالت خواب بیشتر اوقات در مصرف انرژی صرفه جویی می کند. فقط برای ایجاد تبلیغات و اتصالات کوتاه بیدار می شود، بنابراین تبلیغات هم بر مصرف انرژی و هم بر پهنای باند انتقال داده تأثیر می گذارد.

افزونه تبلیغاتی بلوتوث 5

اندروید 8.0 از بلوتوث 5 پشتیبانی می‌کند که باعث بهبود پخش و تبلیغات داده‌های انعطاف‌پذیر برای BLE می‌شود. بلوتوث 5 از لایه های فیزیکی BLE (PHYs) پشتیبانی می کند که مصرف برق کاهش یافته بلوتوث 4.2 را حفظ می کند و به کاربران اجازه می دهد پهنای باند یا محدوده افزایش یافته را انتخاب کنند. اطلاعات بیشتر را می توانید در مشخصات بلوتوث 5 هسته ای بیابید.

پیاده سازی

ویژگی‌های جدید بلوتوث 5 به‌طور خودکار برای دستگاه‌های دارای اندروید 8.0 با کنترل‌کننده‌های بلوتوث سازگار در دسترس است. از این روش‌های BluetoothAdapter برای بررسی اینکه آیا دستگاهی از ویژگی‌های بلوتوث 5 پشتیبانی می‌کند یا خیر استفاده کنید:

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

برای غیرفعال کردن ویژگی‌های تبلیغاتی، با فروشنده تراشه بلوتوث کار کنید تا پشتیبانی از چیپ‌ست را غیرفعال کنید.

بلوتوث PHYها منحصر به یکدیگر هستند و رفتار هر PHY توسط بلوتوث SIG از پیش تعریف شده است. به طور پیش‌فرض، Android 8.0 از بلوتوث LE 1M PHY از بلوتوث 4.2 استفاده می‌کند. بسته android.bluetooth.le ویژگی های تبلیغاتی بلوتوث 5 را از طریق این API ها نشان می دهد:

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

با استفاده از روش startAdvertisingSet() در android.bluetooth.le.BluetoothLeAdvertiser یک AdvertisingSet ایجاد کنید تا تنظیمات تبلیغات بلوتوث را تغییر دهید. حتی اگر پشتیبانی از بلوتوث 5 یا ویژگی‌های تبلیغاتی آن غیرفعال باشد، ویژگی‌های API می‌توانند برای LE 1M PHY نیز اعمال شوند.

مثال ها

این برنامه نمونه از بلوتوث 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 فعال است، تبلیغات از کنترلرهای بلوتوث 4.x پشتیبانی نمی کند، بنابراین setLegacyMode روی false تنظیم شده است. این مثال هنگام تبلیغات پارامترها را تغییر می دهد و همچنین تبلیغات را متوقف می کند.

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

تایید

برای تأیید سازگاری دستگاه با بلوتوث 5 ، آزمایش‌های محصول بلوتوث قابل اجرا را اجرا کنید.

AOSP شامل Android Comms Test Suite (ACTS) است که شامل تست‌هایی برای بلوتوث 5 می‌شود. تست‌های ACTS برای بلوتوث 5 را می‌توانید در tools/test/connectivity/acts/tests/google/ble/bt5 بیابید.