android.bluetooth.IBluetoothGatt

Here are the examples of the java api android.bluetooth.IBluetoothGatt taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

7 Examples 7

19 Source : AdvertisingSet.java
with Apache License 2.0
from lulululbj

/**
 * This clreplaced provides a way to control single Bluetooth LE advertising instance.
 * <p>
 * To get an instance of {@link AdvertisingSet}, call the
 * {@link BluetoothLeAdvertiser#startAdvertisingSet} method.
 * <p>
 * <b>Note:</b> Most of the methods here require {@link android.Manifest.permission#BLUETOOTH_ADMIN}
 * permission.
 *
 * @see AdvertiseData
 */
public final clreplaced AdvertisingSet {

    private static final String TAG = "AdvertisingSet";

    private final IBluetoothGatt mGatt;

    private int mAdvertiserId;

    /* package */
    AdvertisingSet(int advertiserId, IBluetoothManager bluetoothManager) {
        mAdvertiserId = advertiserId;
        try {
            mGatt = bluetoothManager.getBluetoothGatt();
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
            throw new IllegalStateException("Failed to get Bluetooth");
        }
    }

    /* package */
    void setAdvertiserId(int advertiserId) {
        mAdvertiserId = advertiserId;
    }

    /**
     * Enables Advertising. This method returns immediately, the operation status is
     * delivered through {@code callback.onAdvertisingEnabled()}.
     * <p>
     * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
     *
     * @param enable whether the advertising should be enabled (true), or disabled (false)
     * @param duration advertising duration, in 10ms unit. Valid range is from 1 (10ms) to 65535
     * (655,350 ms)
     * @param maxExtendedAdvertisingEvents 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.
     */
    public void enableAdvertising(boolean enable, int duration, int maxExtendedAdvertisingEvents) {
        try {
            mGatt.enableAdvertisingSet(mAdvertiserId, enable, duration, maxExtendedAdvertisingEvents);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Set/update data being Advertised. Make sure that data doesn't exceed the size limit for
     * specified AdvertisingSetParameters. This method returns immediately, the operation status is
     * delivered through {@code callback.onAdvertisingDataSet()}.
     * <p>
     * Advertising data must be empty if non-legacy scannable advertising is used.
     *
     * @param advertiseData Advertisement data to be broadcasted. Size must not exceed {@link
     * BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the advertisement is connectable,
     * three bytes will be added for flags. If the update takes place when the advertising set is
     * enabled, the data can be maximum 251 bytes long.
     */
    public void setAdvertisingData(AdvertiseData advertiseData) {
        try {
            mGatt.setAdvertisingData(mAdvertiserId, advertiseData);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Set/update scan response data. Make sure that data doesn't exceed the size limit for
     * specified AdvertisingSetParameters. This method returns immediately, the operation status
     * is delivered through {@code callback.onScanResponseDataSet()}.
     *
     * @param scanResponse Scan response replacedociated with the advertisement data. Size must not
     * exceed {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the update takes place
     * when the advertising set is enabled, the data can be maximum 251 bytes long.
     */
    public void setScanResponseData(AdvertiseData scanResponse) {
        try {
            mGatt.setScanResponseData(mAdvertiserId, scanResponse);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Update advertising parameters replacedociated with this AdvertisingSet. Must be called when
     * advertising is not active. This method returns immediately, the operation status is delivered
     * through {@code callback.onAdvertisingParametersUpdated}.
     *
     * @param parameters advertising set parameters.
     */
    public void setAdvertisingParameters(AdvertisingSetParameters parameters) {
        try {
            mGatt.setAdvertisingParameters(mAdvertiserId, parameters);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Update periodic advertising parameters replacedociated with this set. Must be called when
     * periodic advertising is not enabled. This method returns immediately, the operation
     * status is delivered through {@code callback.onPeriodicAdvertisingParametersUpdated()}.
     */
    public void setPeriodicAdvertisingParameters(PeriodicAdvertisingParameters parameters) {
        try {
            mGatt.setPeriodicAdvertisingParameters(mAdvertiserId, parameters);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Used to set periodic advertising data, must be called after setPeriodicAdvertisingParameters,
     * or after advertising was started with periodic advertising data set. This method returns
     * immediately, the operation status is delivered through
     * {@code callback.onPeriodicAdvertisingDataSet()}.
     *
     * @param periodicData Periodic advertising data. Size must not exceed {@link
     * BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the update takes place when the
     * periodic advertising is enabled for this set, the data can be maximum 251 bytes long.
     */
    public void setPeriodicAdvertisingData(AdvertiseData periodicData) {
        try {
            mGatt.setPeriodicAdvertisingData(mAdvertiserId, periodicData);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Used to enable/disable periodic advertising. This method returns immediately, the operation
     * status is delivered through {@code callback.onPeriodicAdvertisingEnable()}.
     *
     * @param enable whether the periodic advertising should be enabled (true), or disabled
     * (false).
     */
    public void setPeriodicAdvertisingEnabled(boolean enable) {
        try {
            mGatt.setPeriodicAdvertisingEnable(mAdvertiserId, enable);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Returns address replacedociated with this advertising set.
     * This method is exposed only for Bluetooth PTS tests, no app or system service
     * should ever use it.
     *
     * This method requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED} permission.
     *
     * @hide
     */
    public void getOwnAddress() {
        try {
            mGatt.getOwnAddress(mAdvertiserId);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception - ", e);
        }
    }

    /**
     * Returns advertiserId replacedociated with this advertising set.
     *
     * @hide
     */
    public int getAdvertiserId() {
        return mAdvertiserId;
    }
}

17 Source : PeriodicAdvertisingManager.java
with Apache License 2.0
from lulululbj

/**
 * Cancel pending attempt to create sync, or terminate existing sync.
 *
 * @param callback Callback used to deliver all operations status.
 * @throws IllegalArgumentException if {@code callback} is null, or not a properly registered
 * callback.
 */
public void unregisterSync(PeriodicAdvertisingCallback callback) {
    if (callback == null) {
        throw new IllegalArgumentException("callback can't be null");
    }
    IBluetoothGatt gatt;
    try {
        gatt = mBluetoothManager.getBluetoothGatt();
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
        return;
    }
    IPeriodicAdvertisingCallback wrapper = mCallbackWrappers.remove(callback);
    if (wrapper == null) {
        throw new IllegalArgumentException("callback was not properly registered");
    }
    try {
        gatt.unregisterSync(wrapper);
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to cancel sync creation - ", e);
        return;
    }
}

16 Source : BluetoothLeAdvertiser.java
with Apache License 2.0
from lulululbj

/**
 * Used to dispose of a {@link AdvertisingSet} object, obtained with {@link
 * BluetoothLeAdvertiser#startAdvertisingSet}.
 */
public void stopAdvertisingSet(AdvertisingSetCallback callback) {
    if (callback == null) {
        throw new IllegalArgumentException("callback cannot be null");
    }
    IAdvertisingSetCallback wrapped = mCallbackWrappers.remove(callback);
    if (wrapped == null) {
        return;
    }
    IBluetoothGatt gatt;
    try {
        gatt = mBluetoothManager.getBluetoothGatt();
        gatt.stopAdvertisingSet(wrapped);
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to stop advertising - ", e);
    }
}

15 Source : PeriodicAdvertisingManager.java
with Apache License 2.0
from lulululbj

/**
 * Synchronize with periodic advertising pointed to by the {@code scanResult}.
 * The {@code scanResult} used must contain a valid advertisingSid. First
 * call to registerSync will use the {@code skip} and {@code timeout} provided.
 * Subsequent calls from other apps, trying to sync with same set will reuse
 * existing sync, thus {@code skip} and {@code timeout} values will not take
 * effect. The values in effect will be returned in
 * {@link PeriodicAdvertisingCallback#onSyncEstablished}.
 *
 * @param scanResult Scan result containing advertisingSid.
 * @param skip The number of periodic advertising packets that can be skipped after a successful
 * receive. Must be between 0 and 499.
 * @param timeout Synchronization timeout for the periodic advertising. One unit is 10ms. Must
 * be between 10 (100ms) and 16384 (163.84s).
 * @param callback Callback used to deliver all operations status.
 * @param handler thread upon which the callbacks will be invoked.
 * @throws IllegalArgumentException if {@code scanResult} is null or {@code skip} is invalid or
 * {@code timeout} is invalid or {@code callback} is null.
 */
public void registerSync(ScanResult scanResult, int skip, int timeout, PeriodicAdvertisingCallback callback, Handler handler) {
    if (callback == null) {
        throw new IllegalArgumentException("callback can't be null");
    }
    if (scanResult == null) {
        throw new IllegalArgumentException("scanResult can't be null");
    }
    if (scanResult.getAdvertisingSid() == ScanResult.SID_NOT_PRESENT) {
        throw new IllegalArgumentException("scanResult must contain a valid sid");
    }
    if (skip < SKIP_MIN || skip > SKIP_MAX) {
        throw new IllegalArgumentException("timeout must be between " + TIMEOUT_MIN + " and " + TIMEOUT_MAX);
    }
    if (timeout < TIMEOUT_MIN || timeout > TIMEOUT_MAX) {
        throw new IllegalArgumentException("timeout must be between " + TIMEOUT_MIN + " and " + TIMEOUT_MAX);
    }
    IBluetoothGatt gatt;
    try {
        gatt = mBluetoothManager.getBluetoothGatt();
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
        callback.onSyncEstablished(0, scanResult.getDevice(), scanResult.getAdvertisingSid(), skip, timeout, PeriodicAdvertisingCallback.SYNC_NO_RESOURCES);
        return;
    }
    if (handler == null) {
        handler = new Handler(Looper.getMainLooper());
    }
    IPeriodicAdvertisingCallback wrapped = wrap(callback, handler);
    mCallbackWrappers.put(callback, wrapped);
    try {
        gatt.registerSync(scanResult, skip, timeout, wrapped);
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to register sync - ", e);
        return;
    }
}

15 Source : BluetoothLeScanner.java
with Apache License 2.0
from lulululbj

/**
 * Stops an ongoing Bluetooth LE scan started using a PendingIntent. When creating the
 * PendingIntent parameter, please do not use the FLAG_CANCEL_CURRENT flag. Otherwise, the stop
 * scan may have no effect.
 *
 * @param callbackIntent The PendingIntent that was used to start the scan.
 * @see #startScan(List, ScanSettings, PendingIntent)
 */
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public void stopScan(PendingIntent callbackIntent) {
    BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
    IBluetoothGatt gatt;
    try {
        gatt = mBluetoothManager.getBluetoothGatt();
        gatt.stopScanForIntent(callbackIntent, ActivityThread.currentOpPackageName());
    } catch (RemoteException e) {
    }
}

15 Source : BluetoothLeAdvertiser.java
with Apache License 2.0
from lulululbj

/**
 * Creates a new advertising set. If operation succeed, device will start advertising. This
 * method returns immediately, the operation status is delivered through
 * {@code callback.onAdvertisingSetStarted()}.
 * <p>
 *
 * @param parameters Advertising set parameters.
 * @param advertiseData Advertisement data to be broadcasted. Size must not exceed {@link
 * BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the advertisement is connectable,
 * three bytes will be added for flags.
 * @param scanResponse Scan response replacedociated with the advertisement data. Size must not
 * exceed {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}
 * @param periodicParameters Periodic advertisng parameters. If null, periodic advertising will
 * not be started.
 * @param periodicData Periodic advertising data. Size must not exceed {@link
 * BluetoothAdapter#getLeMaximumAdvertisingDataLength}
 * @param duration advertising duration, in 10ms unit. Valid range is from 1 (10ms) to 65535
 * (655,350 ms). 0 means advertising should continue until stopped.
 * @param maxExtendedAdvertisingEvents 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.
 * @param callback Callback for advertising set.
 * @param 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
 */
public void startAdvertisingSet(AdvertisingSetParameters parameters, AdvertiseData advertiseData, AdvertiseData scanResponse, PeriodicAdvertisingParameters periodicParameters, AdvertiseData periodicData, int duration, int maxExtendedAdvertisingEvents, AdvertisingSetCallback callback, Handler handler) {
    BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
    if (callback == null) {
        throw new IllegalArgumentException("callback cannot be null");
    }
    boolean isConnectable = parameters.isConnectable();
    if (parameters.isLegacy()) {
        if (totalBytes(advertiseData, isConnectable) > MAX_LEGACY_ADVERTISING_DATA_BYTES) {
            throw new IllegalArgumentException("Legacy advertising data too big");
        }
        if (totalBytes(scanResponse, false) > MAX_LEGACY_ADVERTISING_DATA_BYTES) {
            throw new IllegalArgumentException("Legacy scan response data too big");
        }
    } else {
        boolean supportCodedPhy = mBluetoothAdapter.isLeCodedPhySupported();
        boolean support2MPhy = mBluetoothAdapter.isLe2MPhySupported();
        int pphy = parameters.getPrimaryPhy();
        int sphy = parameters.getSecondaryPhy();
        if (pphy == BluetoothDevice.PHY_LE_CODED && !supportCodedPhy) {
            throw new IllegalArgumentException("Unsupported primary PHY selected");
        }
        if ((sphy == BluetoothDevice.PHY_LE_CODED && !supportCodedPhy) || (sphy == BluetoothDevice.PHY_LE_2M && !support2MPhy)) {
            throw new IllegalArgumentException("Unsupported secondary PHY selected");
        }
        int maxData = mBluetoothAdapter.getLeMaximumAdvertisingDataLength();
        if (totalBytes(advertiseData, isConnectable) > maxData) {
            throw new IllegalArgumentException("Advertising data too big");
        }
        if (totalBytes(scanResponse, false) > maxData) {
            throw new IllegalArgumentException("Scan response data too big");
        }
        if (totalBytes(periodicData, false) > maxData) {
            throw new IllegalArgumentException("Periodic advertising data too big");
        }
        boolean supportPeriodic = mBluetoothAdapter.isLePeriodicAdvertisingSupported();
        if (periodicParameters != null && !supportPeriodic) {
            throw new IllegalArgumentException("Controller does not support LE Periodic Advertising");
        }
    }
    if (maxExtendedAdvertisingEvents < 0 || maxExtendedAdvertisingEvents > 255) {
        throw new IllegalArgumentException("maxExtendedAdvertisingEvents out of range: " + maxExtendedAdvertisingEvents);
    }
    if (maxExtendedAdvertisingEvents != 0 && !mBluetoothAdapter.isLePeriodicAdvertisingSupported()) {
        throw new IllegalArgumentException("Can't use maxExtendedAdvertisingEvents with controller that don't support " + "LE Extended Advertising");
    }
    if (duration < 0 || duration > 65535) {
        throw new IllegalArgumentException("duration out of range: " + duration);
    }
    IBluetoothGatt gatt;
    try {
        gatt = mBluetoothManager.getBluetoothGatt();
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
        postStartSetFailure(handler, callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
        return;
    }
    IAdvertisingSetCallback wrapped = wrap(callback, handler);
    if (mCallbackWrappers.putIfAbsent(callback, wrapped) != null) {
        throw new IllegalArgumentException("callback instance already replacedociated with advertising");
    }
    try {
        gatt.startAdvertisingSet(parameters, advertiseData, scanResponse, periodicParameters, periodicData, duration, maxExtendedAdvertisingEvents, wrapped);
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to start advertising set - ", e);
        postStartSetFailure(handler, callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
        return;
    }
}

11 Source : BluetoothLeScanner.java
with Apache License 2.0
from lulululbj

private int startScan(List<ScanFilter> filters, ScanSettings settings, final WorkSource workSource, final ScanCallback callback, final PendingIntent callbackIntent, List<List<ResultStorageDescriptor>> resultStorages) {
    BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
    if (callback == null && callbackIntent == null) {
        throw new IllegalArgumentException("callback is null");
    }
    if (settings == null) {
        throw new IllegalArgumentException("settings is null");
    }
    synchronized (mLeScanClients) {
        if (callback != null && mLeScanClients.containsKey(callback)) {
            return postCallbackErrorOrReturn(callback, ScanCallback.SCAN_FAILED_ALREADY_STARTED);
        }
        IBluetoothGatt gatt;
        try {
            gatt = mBluetoothManager.getBluetoothGatt();
        } catch (RemoteException e) {
            gatt = null;
        }
        if (gatt == null) {
            return postCallbackErrorOrReturn(callback, ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
        }
        if (!isSettingsConfigAllowedForScan(settings)) {
            return postCallbackErrorOrReturn(callback, ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED);
        }
        if (!isHardwareResourcesAvailableForScan(settings)) {
            return postCallbackErrorOrReturn(callback, ScanCallback.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES);
        }
        if (!isSettingsAndFilterComboAllowed(settings, filters)) {
            return postCallbackErrorOrReturn(callback, ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED);
        }
        if (callback != null) {
            BleScanCallbackWrapper wrapper = new BleScanCallbackWrapper(gatt, filters, settings, workSource, callback, resultStorages);
            wrapper.startRegistration();
        } else {
            try {
                gatt.startScanForIntent(callbackIntent, settings, filters, ActivityThread.currentOpPackageName());
            } catch (RemoteException e) {
                return ScanCallback.SCAN_FAILED_INTERNAL_ERROR;
            }
        }
    }
    return ScanCallback.NO_ERROR;
}