FileDocCategorySizeDatePackage
BluetoothLeAdvertiser.javaAPI DocAndroid 5.1 API15568Thu Mar 12 22:22:10 GMT 2015android.bluetooth.le

BluetoothLeAdvertiser

public final class BluetoothLeAdvertiser extends Object
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 {@link AdvertiseData}.

To get an instance of {@link BluetoothLeAdvertiser}, call the {@link BluetoothAdapter#getBluetoothLeAdvertiser()} method.

Note: Most of the methods here require {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

see
AdvertiseData

Fields Summary
private static final String
TAG
private static final int
MAX_ADVERTISING_DATA_BYTES
private static final int
OVERHEAD_BYTES_PER_FIELD
private static final int
FLAGS_FIELD_BYTES
private static final int
MANUFACTURER_SPECIFIC_DATA_LENGTH
private static final int
SERVICE_DATA_UUID_LENGTH
private final android.bluetooth.IBluetoothManager
mBluetoothManager
private final android.os.Handler
mHandler
private android.bluetooth.BluetoothAdapter
mBluetoothAdapter
private final Map
mLeAdvertisers
Constructors Summary
public BluetoothLeAdvertiser(android.bluetooth.IBluetoothManager bluetoothManager)
Use BluetoothAdapter.getLeAdvertiser() instead.

param
bluetoothManager BluetoothManager that conducts overall Bluetooth Management
hide


                     
       
        mBluetoothManager = bluetoothManager;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mHandler = new Handler(Looper.getMainLooper());
    
Methods Summary
private intbyteLength(byte[] array)

        return array == null ? 0 : array.length;
    
public voidcleanup()
Cleans up advertise clients. Should be called when bluetooth is down.

hide

        mLeAdvertisers.clear();
    
private voidpostStartFailure(AdvertiseCallback callback, int error)

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onStartFailure(error);
            }
        });
    
private voidpostStartSuccess(AdvertiseCallback callback, AdvertiseSettings settings)

        mHandler.post(new Runnable() {

            @Override
            public void run() {
                callback.onStartSuccess(settings);
            }
        });
    
public voidstartAdvertising(AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseCallback callback)
Start Bluetooth LE Advertising. On success, the {@code advertiseData} will be broadcasted. Returns immediately, the operation status is delivered through {@code callback}.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
settings Settings for Bluetooth LE advertising.
param
advertiseData Advertisement data to be broadcasted.
param
callback Callback for advertising status.

        startAdvertising(settings, advertiseData, null, callback);
    
public voidstartAdvertising(AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseData scanResponse, AdvertiseCallback callback)
Start Bluetooth LE Advertising. The {@code advertiseData} will be broadcasted if the operation succeeds. The {@code scanResponse} is returned when a scanning device sends an active scan request. This method returns immediately, the operation status is delivered through {@code callback}.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}

param
settings Settings for Bluetooth LE advertising.
param
advertiseData Advertisement data to be advertised in advertisement packet.
param
scanResponse Scan response associated with the advertisement data.
param
callback Callback for advertising status.

        synchronized (mLeAdvertisers) {
            BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
            if (callback == null) {
                throw new IllegalArgumentException("callback cannot be null");
            }
            if (!mBluetoothAdapter.isMultipleAdvertisementSupported() &&
                    !mBluetoothAdapter.isPeripheralModeSupported()) {
                postStartFailure(callback,
                        AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED);
                return;
            }
            boolean isConnectable = settings.isConnectable();
            if (totalBytes(advertiseData, isConnectable) > MAX_ADVERTISING_DATA_BYTES ||
                    totalBytes(scanResponse, false) > MAX_ADVERTISING_DATA_BYTES) {
                postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE);
                return;
            }
            if (mLeAdvertisers.containsKey(callback)) {
                postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED);
                return;
            }

            IBluetoothGatt gatt;
            try {
                gatt = mBluetoothManager.getBluetoothGatt();
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
                postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
                return;
            }
            AdvertiseCallbackWrapper wrapper = new AdvertiseCallbackWrapper(callback, advertiseData,
                    scanResponse, settings, gatt);
            wrapper.startRegisteration();
        }
    
public voidstopAdvertising(AdvertiseCallback callback)
Stop Bluetooth LE advertising. The {@code callback} must be the same one use in {@link BluetoothLeAdvertiser#startAdvertising}.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
callback {@link AdvertiseCallback} identifies the advertising instance to stop.

        synchronized (mLeAdvertisers) {
            BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
            if (callback == null) {
                throw new IllegalArgumentException("callback cannot be null");
            }
            AdvertiseCallbackWrapper wrapper = mLeAdvertisers.get(callback);
            if (wrapper == null) return;
            wrapper.stopAdvertising();
        }
    
private inttotalBytes(AdvertiseData data, boolean isFlagsIncluded)

        if (data == null) return 0;
        // Flags field is omitted if the advertising is not connectable.
        int size = (isFlagsIncluded) ? FLAGS_FIELD_BYTES : 0;
        if (data.getServiceUuids() != null) {
            int num16BitUuids = 0;
            int num32BitUuids = 0;
            int num128BitUuids = 0;
            for (ParcelUuid uuid : data.getServiceUuids()) {
                if (BluetoothUuid.is16BitUuid(uuid)) {
                    ++num16BitUuids;
                } else if (BluetoothUuid.is32BitUuid(uuid)) {
                    ++num32BitUuids;
                } else {
                    ++num128BitUuids;
                }
            }
            // 16 bit service uuids are grouped into one field when doing advertising.
            if (num16BitUuids != 0) {
                size += OVERHEAD_BYTES_PER_FIELD +
                        num16BitUuids * BluetoothUuid.UUID_BYTES_16_BIT;
            }
            // 32 bit service uuids are grouped into one field when doing advertising.
            if (num32BitUuids != 0) {
                size += OVERHEAD_BYTES_PER_FIELD +
                        num32BitUuids * BluetoothUuid.UUID_BYTES_32_BIT;
            }
            // 128 bit service uuids are grouped into one field when doing advertising.
            if (num128BitUuids != 0) {
                size += OVERHEAD_BYTES_PER_FIELD +
                        num128BitUuids * BluetoothUuid.UUID_BYTES_128_BIT;
            }
        }
        for (ParcelUuid uuid : data.getServiceData().keySet()) {
            size += OVERHEAD_BYTES_PER_FIELD + SERVICE_DATA_UUID_LENGTH
                    + byteLength(data.getServiceData().get(uuid));
        }
        for (int i = 0; i < data.getManufacturerSpecificData().size(); ++i) {
            size += OVERHEAD_BYTES_PER_FIELD + MANUFACTURER_SPECIFIC_DATA_LENGTH +
                    byteLength(data.getManufacturerSpecificData().valueAt(i));
        }
        if (data.getIncludeTxPowerLevel()) {
            size += OVERHEAD_BYTES_PER_FIELD + 1; // tx power level value is one byte.
        }
        if (data.getIncludeDeviceName() && mBluetoothAdapter.getName() != null) {
            size += OVERHEAD_BYTES_PER_FIELD + mBluetoothAdapter.getName().length();
        }
        return size;