藍牙低功耗 (BLE) 功能可在睡眠模式的剩餘時間內節省電力 多數時候都能提供這樣的體驗醒來後只在意廣告及簡短 因此,廣告會影響耗電量和 以及資料移轉頻寬
藍牙 5 廣告擴充功能
Android 8.0 支援藍牙 5,可提供廣播功能 以改善 BLE 的資料廣告與靈活性藍牙 5 支援 BLE Physical 保留 藍牙 4.2 的耗電量更低,讓使用者選擇 增加頻寬或範圍如需更多資訊,請前往 Bluetooth 5 Core 規格。
實作
裝置會自動使用新的藍牙 5 功能
搭載 Android 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
建立
AdvertisingSet
使用 startAdvertisingSet()
修改藍牙廣告設定
android.bluetooth.le.BluetoothLeAdvertiser
中的方法。即使
對藍牙 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。當 200 萬 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.