פרסום בלוטות' באנרגיה נמוכה

Bluetooth Low Energy (BLE) חוסך חשמל על ידי הישארות במצב שינה רוב הזמן. הוא מתעורר רק כדי ליצור פרסומות וחיבורים קצרים, כך שפרסומות משפיעות גם על צריכת החשמל וגם על רוחב הפס של העברת הנתונים.

תוסף פרסום בלוטות' 5

אנדרואיד 8.0 תומך ב-Bluetooth 5, המספק שיפורי שידור ופרסום נתונים גמישים עבור BLE. Bluetooth 5 תומך ב-BLE Physical Layers (PHYs) השומרות על צריכת החשמל המופחתת של Bluetooth 4.2 ומאפשרות למשתמשים לבחור רוחב פס או טווח מוגדלים. מידע נוסף ניתן למצוא במפרטי Bluetooth 5 Core .

יישום

תכונות חדשות של Bluetooth 5 זמינות אוטומטית עבור מכשירים עם אנדרואיד 8.0 עם בקרי Bluetooth תואמים. השתמש בשיטות BluetoothAdapter אלה כדי לבדוק אם מכשיר תומך בתכונות Bluetooth 5:

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

כדי להשבית את תכונות הפרסום, עבוד עם ספק שבבי ה-Bluetooth כדי להשבית את התמיכה בערכת שבבים.

ה-Bluetooth PHYs בלעדי זה מזה, וההתנהגות של כל PHY מוגדרת מראש על ידי ה-Bluetooth SIG. כברירת מחדל, אנדרואיד 8.0 משתמש ב-Bluetooth LE 1M PHY, מ-Bluetooth 4.2. חבילת android.bluetooth.le חושפת את תכונות הפרסום של Bluetooth 5 באמצעות ממשקי API אלה:

  • 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 Test Suite (ACTS), הכוללת בדיקות עבור Bluetooth 5. את מבחני ACTS עבור Bluetooth 5 ניתן למצוא ב- tools/test/connectivity/acts/tests/google/ble/bt5 .