Added in API level 21

BluetoothLeAdvertiser


public final class BluetoothLeAdvertiser
extends Object

java.lang.Object
   ↳ android.bluetooth.le.BluetoothLeAdvertiser


This class provides a way to perform Bluetooth LE advertise operations, such as starting and stopping advertising. An advertiser can broadcast up to 31 bytes of advertisement data represented by AdvertiseData.

To get an instance of BluetoothLeAdvertiser, call the BluetoothAdapter.getBluetoothLeAdvertiser() method.

See also:

Summary

Public methods

void startAdvertising(AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseCallback callback)

Start Bluetooth LE Advertising.

void startAdvertising(AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseData scanResponse, AdvertiseCallback callback)

Start Bluetooth LE Advertising.

void startAdvertisingSet(AdvertisingSetParameters parameters, AdvertiseData advertiseData, AdvertiseData scanResponse, PeriodicAdvertisingParameters periodicParameters, AdvertiseData periodicData, int duration, int maxExtendedAdvertisingEvents, AdvertisingSetCallback callback)

Creates a new advertising set.

void startAdvertisingSet(AdvertisingSetParameters parameters, AdvertiseData advertiseData, AdvertiseData scanResponse, PeriodicAdvertisingParameters periodicParameters, AdvertiseData periodicData, int duration, int maxExtendedAdvertisingEvents, AdvertisingSetCallback callback, Handler handler)

Creates a new advertising set.

void startAdvertisingSet(AdvertisingSetParameters parameters, AdvertiseData advertiseData, AdvertiseData scanResponse, PeriodicAdvertisingParameters periodicParameters, AdvertiseData periodicData, AdvertisingSetCallback callback)

Creates a new advertising set.

void startAdvertisingSet(AdvertisingSetParameters parameters, AdvertiseData advertiseData, AdvertiseData scanResponse, PeriodicAdvertisingParameters periodicParameters, AdvertiseData periodicData, AdvertisingSetCallback callback, Handler handler)

Creates a new advertising set.

void stopAdvertising(AdvertiseCallback callback)

Stop Bluetooth LE advertising.

void stopAdvertisingSet(AdvertisingSetCallback callback)

Used to dispose of a AdvertisingSet object, obtained with startAdvertisingSet(AdvertisingSetParameters, AdvertiseData, AdvertiseData, PeriodicAdvertisingParameters, AdvertiseData, AdvertisingSetCallback).

Inherited methods

Public methods

startAdvertising

Added in API level 21
public void startAdvertising (AdvertiseSettings settings, 
                AdvertiseData advertiseData, 
                AdvertiseCallback callback)

Start Bluetooth LE Advertising. On success, the advertiseData will be broadcasted. Returns immediately, the operation status is delivered through callback.

Requires the Manifest.permission.BLUETOOTH_PRIVILEGED permission only when settings.getOwnAddressType() is different from AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT.

The Manifest.permission.BLUETOOTH_ADVERTISE permission is always enforced.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).

Parameters
settings AdvertiseSettings: Settings for Bluetooth LE advertising.

advertiseData AdvertiseData: Advertisement data to be broadcasted.

callback AdvertiseCallback: Callback for advertising status.

startAdvertising

Added in API level 21
public void startAdvertising (AdvertiseSettings settings, 
                AdvertiseData advertiseData, 
                AdvertiseData scanResponse, 
                AdvertiseCallback callback)

Start Bluetooth LE Advertising. The advertiseData will be broadcasted if the operation succeeds. The scanResponse is returned when a scanning device sends an active scan request. This method returns immediately, the operation status is delivered through callback.

Requires the Manifest.permission.BLUETOOTH_PRIVILEGED permission only when settings.getOwnAddressType() is different from AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT.

The Manifest.permission.BLUETOOTH_ADVERTISE permission is always enforced.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).

Parameters
settings AdvertiseSettings: Settings for Bluetooth LE advertising.

advertiseData AdvertiseData: Advertisement data to be advertised in advertisement packet.

scanResponse AdvertiseData: Scan response associated with the advertisement data.

callback AdvertiseCallback: Callback for advertising status.

startAdvertisingSet

Added in API level 26
public void startAdvertisingSet (AdvertisingSetParameters parameters, 
                AdvertiseData advertiseData, 
                AdvertiseData scanResponse, 
                PeriodicAdvertisingParameters periodicParameters, 
                AdvertiseData periodicData, 
                int duration, 
                int maxExtendedAdvertisingEvents, 
                AdvertisingSetCallback callback)

Creates a new advertising set. If operation succeed, device will start advertising. This method returns immediately, the operation status is delivered through callback.onAdvertisingSetStarted().

Requires the Manifest.permission.BLUETOOTH_PRIVILEGED permission only when parameters.getOwnAddressType() is different from AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT.

The Manifest.permission.BLUETOOTH_ADVERTISE permission is always enforced.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).

Parameters
parameters AdvertisingSetParameters: advertising set parameters.

advertiseData AdvertiseData: Advertisement data to be broadcasted. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength(). If the advertisement is connectable, three bytes will be added for flags.

scanResponse AdvertiseData: Scan response associated with the advertisement data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength.

periodicParameters PeriodicAdvertisingParameters: periodic advertisng parameters. If null, periodic advertising will not be started.

periodicData AdvertiseData: Periodic advertising data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength().

duration int: advertising duration, in 10ms unit. Valid range is from 1 (10ms) to 65535 (655,350 ms). 0 means advertising should continue until stopped.

maxExtendedAdvertisingEvents int: maximum number of extended advertising events the controller shall attempt to send prior to terminating the extended advertising, even if the duration has not expired. Valid range is from 1 to 255. 0 means no maximum.

callback AdvertisingSetCallback: Callback for advertising set.

Throws
IllegalArgumentException when any of the data parameter exceed the maximum allowable size, or unsupported advertising PHY is selected, or when attempt to use Periodic Advertising feature is made when it's not supported by the controller.

startAdvertisingSet

Added in API level 26
public void startAdvertisingSet (AdvertisingSetParameters parameters, 
                AdvertiseData advertiseData, 
                AdvertiseData scanResponse, 
                PeriodicAdvertisingParameters periodicParameters, 
                AdvertiseData periodicData, 
                int duration, 
                int maxExtendedAdvertisingEvents, 
                AdvertisingSetCallback callback, 
                Handler handler)

Creates a new advertising set. If operation succeed, device will start advertising. This method returns immediately, the operation status is delivered through callback.onAdvertisingSetStarted().

Requires the Manifest.permission.BLUETOOTH_PRIVILEGED permission only when parameters.getOwnAddressType() is different from AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT.

The Manifest.permission.BLUETOOTH_ADVERTISE permission is always enforced.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).

Parameters
parameters AdvertisingSetParameters: Advertising set parameters.

advertiseData AdvertiseData: Advertisement data to be broadcasted. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength(). If the advertisement is connectable, three bytes will be added for flags.

scanResponse AdvertiseData: Scan response associated with the advertisement data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength

periodicParameters PeriodicAdvertisingParameters: Periodic advertisng parameters. If null, periodic advertising will not be started.

periodicData AdvertiseData: Periodic advertising data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength()

duration int: advertising duration, in 10ms unit. Valid range is from 1 (10ms) to 65535 (655,350 ms). 0 means advertising should continue until stopped.

maxExtendedAdvertisingEvents int: maximum number of extended advertising events the controller shall attempt to send prior to terminating the extended advertising, even if the duration has not expired. Valid range is from 1 to 255. 0 means no maximum.

callback AdvertisingSetCallback: Callback for advertising set.

handler Handler: Thread upon which the callbacks will be invoked.

Throws
IllegalArgumentException When any of the data parameter exceed the maximum allowable size, or unsupported advertising PHY is selected, or when attempt to use Periodic Advertising feature is made when it's not supported by the controller, or when maxExtendedAdvertisingEvents is used on a controller that doesn't support the LE Extended Advertising

startAdvertisingSet

Added in API level 26
public void startAdvertisingSet (AdvertisingSetParameters parameters, 
                AdvertiseData advertiseData, 
                AdvertiseData scanResponse, 
                PeriodicAdvertisingParameters periodicParameters, 
                AdvertiseData periodicData, 
                AdvertisingSetCallback callback)

Creates a new advertising set. If operation succeed, device will start advertising. This method returns immediately, the operation status is delivered through callback.onAdvertisingSetStarted().

Requires the Manifest.permission.BLUETOOTH_PRIVILEGED permission only when parameters.getOwnAddressType() is different from AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT.

The Manifest.permission.BLUETOOTH_ADVERTISE permission is always enforced.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).

Parameters
parameters AdvertisingSetParameters: advertising set parameters.

advertiseData AdvertiseData: Advertisement data to be broadcasted. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength(). If the advertisement is connectable, three bytes will be added for flags.

scanResponse AdvertiseData: Scan response associated with the advertisement data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength.

periodicParameters PeriodicAdvertisingParameters: periodic advertisng parameters. If null, periodic advertising will not be started.

periodicData AdvertiseData: Periodic advertising data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength().

callback AdvertisingSetCallback: Callback for advertising set.

Throws
IllegalArgumentException when any of the data parameter exceed the maximum allowable size, or unsupported advertising PHY is selected, or when attempt to use Periodic Advertising feature is made when it's not supported by the controller.

startAdvertisingSet

Added in API level 26
public void startAdvertisingSet (AdvertisingSetParameters parameters, 
                AdvertiseData advertiseData, 
                AdvertiseData scanResponse, 
                PeriodicAdvertisingParameters periodicParameters, 
                AdvertiseData periodicData, 
                AdvertisingSetCallback callback, 
                Handler handler)

Creates a new advertising set. If operation succeed, device will start advertising. This method returns immediately, the operation status is delivered through callback.onAdvertisingSetStarted().

Requires the Manifest.permission.BLUETOOTH_PRIVILEGED permission only when parameters.getOwnAddressType() is different from AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT.

The Manifest.permission.BLUETOOTH_ADVERTISE permission is always enforced.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).

Parameters
parameters AdvertisingSetParameters: advertising set parameters.

advertiseData AdvertiseData: Advertisement data to be broadcasted. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength(). If the advertisement is connectable, three bytes will be added for flags.

scanResponse AdvertiseData: Scan response associated with the advertisement data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength.

periodicParameters PeriodicAdvertisingParameters: periodic advertisng parameters. If null, periodic advertising will not be started.

periodicData AdvertiseData: Periodic advertising data. Size must not exceed BluetoothAdapter.getLeMaximumAdvertisingDataLength().

callback AdvertisingSetCallback: Callback for advertising set.

handler Handler: thread upon which the callbacks will be invoked.

Throws
IllegalArgumentException when any of the data parameter exceed the maximum allowable size, or unsupported advertising PHY is selected, or when attempt to use Periodic Advertising feature is made when it's not supported by the controller.

stopAdvertising

Added in API level 21
public void stopAdvertising (AdvertiseCallback callback)

Stop Bluetooth LE advertising. The callback must be the same one use in startAdvertising(AdvertiseSettings, AdvertiseData, AdvertiseCallback).
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).
Requires Manifest.permission.BLUETOOTH_ADVERTISE

Parameters
callback AdvertiseCallback: AdvertiseCallback identifies the advertising instance to stop.

stopAdvertisingSet

Added in API level 26
public void stopAdvertisingSet (AdvertisingSetCallback callback)

Used to dispose of a AdvertisingSet object, obtained with startAdvertisingSet(AdvertisingSetParameters, AdvertiseData, AdvertiseData, PeriodicAdvertisingParameters, AdvertiseData, AdvertisingSetCallback).
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_ADVERTISE permission which can be gained with Activity.requestPermissions(String[], int).
Requires Manifest.permission.BLUETOOTH_ADVERTISE

Parameters
callback AdvertisingSetCallback