إعلانات بلوتوث منخفضة الطاقة

تعمل تقنية Bluetooth Low Energy (BLE) على الحفاظ على الطاقة من خلال البقاء في وضع السكون معظم الوقت. يتم تنشيطه فقط لعمل إعلانات واتصالات قصيرة، لذا تؤثر الإعلانات على استهلاك الطاقة وعرض النطاق الترددي لنقل البيانات.

امتداد إعلان بلوتوث 5

يدعم Android 8.0 تقنية Bluetooth 5، التي توفر تحسينات في البث وإعلان بيانات مرن لـ BLE. يدعم Bluetooth 5 الطبقات المادية BLE (PHYs) التي تحافظ على استهلاك الطاقة المنخفض لـ Bluetooth 4.2 وتتيح للمستخدمين اختيار النطاق الترددي أو النطاق المتزايد. يمكن العثور على مزيد من المعلومات في مواصفات Bluetooth 5 الأساسية .

تطبيق

تتوفر ميزات Bluetooth 5 الجديدة تلقائيًا للأجهزة التي تعمل بنظام التشغيل Android 8.0 والمزودة بوحدات تحكم Bluetooth متوافقة. استخدم طرق BluetoothAdapter هذه للتحقق مما إذا كان الجهاز يدعم ميزات Bluetooth 5:

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

لتعطيل ميزات الإعلان، اعمل مع بائع شرائح Bluetooth لتعطيل دعم مجموعة الشرائح.

تعد وحدات PHY الخاصة بالبلوتوث حصرية لبعضها البعض، ويتم تحديد سلوك كل طبقة PHY مسبقًا بواسطة Bluetooth SIG. افتراضيًا، يستخدم Android 8.0 تقنية Bluetooth LE 1M PHY، بدءًا من Bluetooth 4.2. تعرض حزمة android.bluetooth.le ميزات إعلان Bluetooth 5 من خلال واجهات برمجة التطبيقات التالية:

  • AdvertisingSet
  • AdvertisingSetCallback
  • AdvertisingSetParameters
  • PeriodicAdvertisingParameters

قم بإنشاء AdvertisingSet لتعديل إعدادات إعلانات Bluetooth باستخدام الأسلوب startAdvertisingSet() في android.bluetooth.le.BluetoothLeAdvertiser . حتى إذا تم تعطيل دعم Bluetooth 5 أو ميزاته الإعلانية، يمكن أن تنطبق ميزات واجهة برمجة التطبيقات (API) أيضًا على LE 1M PHY.

أمثلة

يستخدم هذا التطبيق المثال Bluetooth 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، لا يدعم الإعلان وحدات تحكم Bluetooth 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);
}

تَحَقّق

قم بإجراء اختبارات منتج Bluetooth المعمول بها للتحقق من توافق الجهاز مع Bluetooth 5.

يحتوي AOSP على مجموعة اختبار Android Comms (ACTS)، والتي تتضمن اختبارات لـ Bluetooth 5. يمكن العثور على اختبارات ACTS لـ Bluetooth 5 في tools/test/connectivity/acts/tests/google/ble/bt5 .