Starting March 27, 2025, we recommend using android-latest-release instead of aosp-main to build and contribute to AOSP. For more information, see Changes to AOSP.
Bluetooth Low Energy advertising
Stay organized with collections
Save and categorize content based on your preferences.
Bluetooth Low Energy (BLE) conserves power by remaining in sleep mode most of the time. It wakes up only to make advertisements and short connections, so advertisements affect both power consumption and data transfer bandwidth.
Bluetooth 5 advertising extension
Android 8.0 supports Bluetooth 5, which provides broadcasting improvements and flexible data advertisement for BLE. Bluetooth 5 supports BLE Physical Layers (PHYs) that retain the reduced power consumption of Bluetooth 4.2 and let users choose increased bandwidth or range. More information can be found in the Bluetooth 5 Core Specifications.
Implementation
New Bluetooth 5 features are automatically available for devices running Android
8.0 with compatible Bluetooth controllers. Use these BluetoothAdapter
methods to check if a device supports Bluetooth 5 features:
isLe2MPhySupported()isLeCodedPhySupported()isLeExtendedAdvertisingSupported()isLePeriodicAdvertisingSupported()
To disable the advertising features, work with the Bluetooth chip vendor to disable chip-set support.
The Bluetooth PHYs are exclusive of one another, and the behavior of each PHY is
predefined by the Bluetooth SIG. By default, Android 8.0 uses Bluetooth LE 1M
PHY, from Bluetooth 4.2. The android.bluetooth.le package exposes the
Bluetooth 5 advertising features through these APIs:
AdvertisingSetAdvertisingSetCallbackAdvertisingSetParametersPeriodicAdvertisingParameters
Create an AdvertisingSet to modify Bluetooth advertisement settings by
using the startAdvertisingSet() method in
android.bluetooth.le.BluetoothLeAdvertiser. Even if support for Bluetooth
5 or its advertising features is disabled, the API features can also apply to LE
1M PHY.
Examples
This example app uses Bluetooth LE 1M PHY for advertising:
// Start legacy advertising. Works for devices with 5.x controllers,
// and devices that support multi-advertising.
voidexample1(){
BluetoothLeAdvertiseradvertiser=
BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertisingSetParametersparameters=(newAdvertisingSetParameters.Builder())
.setLegacyMode(true)// True by default, but set here as a reminder.
.setConnectable(true)
.setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
.setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
.build();
AdvertiseDatadata=(newAdvertiseData.Builder()).setIncludeDeviceName(true).build();
AdvertisingSetCallbackcallback=newAdvertisingSetCallback(){
@Override
publicvoidonAdvertisingSetStarted(AdvertisingSetadvertisingSet,inttxPower,intstatus){
Log.i(LOG_TAG,"onAdvertisingSetStarted(): txPower:"+txPower+" , status: "
+status);
currentAdvertisingSet=advertisingSet;
}
@Override
publicvoidonAdvertisingDataSet(AdvertisingSetadvertisingSet,intstatus){
Log.i(LOG_TAG,"onAdvertisingDataSet() :status:"+status);
}
@Override
publicvoidonScanResponseDataSet(AdvertisingSetadvertisingSet,intstatus){
Log.i(LOG_TAG,"onScanResponseDataSet(): status:"+status);
}
@Override
publicvoidonAdvertisingSetStopped(AdvertisingSetadvertisingSet){
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(newAdvertiseData.Builder().
setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build());
// Wait for onAdvertisingDataSet callback...
currentAdvertisingSet.setScanResponseData(new
AdvertiseData.Builder().addServiceUuid(newParcelUuid(UUID.randomUUID())).build());
// Wait for onScanResponseDataSet callback...
// When done with the advertising:
advertiser.stopAdvertisingSet(callback);
}
This example app uses the BLE 2M PHY for advertising. The app first checks that
the device supports the features being used. If the advertising features are
supported, then the app configures BLE 2M PHY as the primary PHY. While 2M PHY
is active, advertisement does not support Bluetooth 4.x controllers, so
setLegacyMode is set to false. This example modifies parameters while
advertising and also pauses the advertisement.
voidexample2(){
BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter();
BluetoothLeAdvertiseradvertiser=
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;
}
intmaxDataLength=adapter.getLeMaximumAdvertisingDataLength();
AdvertisingSetParameters.Builderparameters=(newAdvertisingSetParameters.Builder())
.setLegacyMode(false)
.setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
.setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
.setPrimaryPhy(BluetoothDevice.PHY_LE_1M)
.setSecondaryPhy(BluetoothDevice.PHY_LE_2M);
AdvertiseDatadata=(newAdvertiseData.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();
AdvertisingSetCallbackcallback=newAdvertisingSetCallback(){
@Override
publicvoidonAdvertisingSetStarted(AdvertisingSetadvertisingSet,inttxPower,intstatus){
Log.i(LOG_TAG,"onAdvertisingSetStarted(): txPower:"+txPower+" , status: "
+status);
currentAdvertisingSet=advertisingSet;
}
@Override
publicvoidonAdvertisingSetStopped(AdvertisingSetadvertisingSet){
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(newParcelUuid(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);
}
Verification
Run applicable Bluetooth product tests to verify device compatibility with Bluetooth 5.