تبلیغات کم مصرف بلوتوث

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

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

اندروید ۸.۰ از بلوتوث ۵ پشتیبانی می‌کند که بهبودهایی در پخش و توزیع انعطاف‌پذیر داده‌ها برای BLE ارائه می‌دهد. بلوتوث ۵ از لایه‌های فیزیکی BLE (PHY) پشتیبانی می‌کند که مصرف برق کاهش‌یافته بلوتوث ۴.۲ را حفظ کرده و به کاربران اجازه می‌دهد پهنای باند یا برد افزایش‌یافته را انتخاب کنند. اطلاعات بیشتر را می‌توانید در مشخصات اصلی بلوتوث ۵ بیابید.

پیاده‌سازی

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

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

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

لایه‌های فیزیکی بلوتوث (PHY) منحصر به یکدیگر هستند و رفتار هر لایه فیزیکی توسط Bluetooth SIG از پیش تعریف شده است. به طور پیش‌فرض، اندروید ۸.۰ از Bluetooth LE 1M PHY، از بلوتوث ۴.۲، استفاده می‌کند. بسته android.bluetooth.le ویژگی‌های تبلیغاتی بلوتوث ۵ را از طریق این APIها در معرض نمایش قرار می‌دهد:

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

برای تغییر تنظیمات تبلیغات بلوتوث، با استفاده از متد startAdvertisingSet() در android.bluetooth.le.BluetoothLeAdvertiser ، یک AdvertisingSet ایجاد کنید. حتی اگر پشتیبانی از بلوتوث ۵ یا ویژگی‌های تبلیغاتی آن غیرفعال باشد، ویژگی‌های 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 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);
}

تأیید

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