โฆษณาบลูทูธพลังงานต่ำ

Bluetooth Low Energy (BLE) ประหยัดพลังงานโดยส่วนใหญ่อยู่ในโหมดสลีป โดยจะเริ่มทำงานเพื่อสร้างโฆษณาและการเชื่อมต่อสั้นๆ เท่านั้น ดังนั้นโฆษณาจึงส่งผลต่อทั้งการใช้พลังงานและแบนด์วิธการถ่ายโอนข้อมูล

ส่วนขยายโฆษณา Bluetooth 5

Android 8.0 รองรับ Bluetooth 5 ซึ่งให้การปรับปรุงการออกอากาศและการโฆษณาข้อมูลที่ยืดหยุ่นสำหรับ BLE Bluetooth 5 รองรับ BLE Physical Layers (PHY) ที่จะคงการใช้พลังงานที่ลดลงของ Bluetooth 4.2 และให้ผู้ใช้เลือกแบนด์วิดท์หรือช่วงที่เพิ่มขึ้น สามารถดูข้อมูลเพิ่มเติมได้ใน ข้อมูลจำเพาะของ Bluetooth 5 Core

การนำไปปฏิบัติ

คุณสมบัติ Bluetooth 5 ใหม่จะพร้อมใช้งานโดยอัตโนมัติสำหรับอุปกรณ์ที่ใช้ Android 8.0 พร้อมตัวควบคุม Bluetooth ที่ใช้งานร่วมกันได้ ใช้วิธี BluetoothAdapter เหล่านี้เพื่อตรวจสอบว่าอุปกรณ์รองรับคุณสมบัติ Bluetooth 5 หรือไม่:

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

หากต้องการปิดใช้งานคุณลักษณะการโฆษณา ให้ทำงานร่วมกับผู้จำหน่ายชิป Bluetooth เพื่อปิดใช้งานการสนับสนุนชุดชิป

Bluetooth PHY เป็นเอกสิทธิ์ของกันและกัน และลักษณะการทำงานของ PHY แต่ละรายการถูกกำหนดไว้ล่วงหน้าโดย Bluetooth SIG ตามค่าเริ่มต้น Android 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