diff options
author | Jakub Pawlowski <jpawlowski@google.com> | 2017-03-08 19:05:12 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-03-08 19:05:12 +0000 |
commit | 99ec98e2f328f24c78dac07afa2aff745e13a48f (patch) | |
tree | 9878d62e90d16982e0ac42a0a73de0853d5a6538 /framework/java/android/bluetooth/le/AdvertisingSet.java | |
parent | 95e9c5c0f2d620f8d6bd065133b9349e1dc4b47d (diff) | |
parent | 258e43c15602c8246bc565150158b1c56e3c0814 (diff) |
Merge "Bluetooth 5 Advertising API"
Diffstat (limited to 'framework/java/android/bluetooth/le/AdvertisingSet.java')
-rw-r--r-- | framework/java/android/bluetooth/le/AdvertisingSet.java | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/framework/java/android/bluetooth/le/AdvertisingSet.java b/framework/java/android/bluetooth/le/AdvertisingSet.java new file mode 100644 index 0000000000..1524022b1f --- /dev/null +++ b/framework/java/android/bluetooth/le/AdvertisingSet.java @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.bluetooth.le; + +import android.bluetooth.IBluetoothGatt; +import android.bluetooth.IBluetoothManager; +import android.bluetooth.le.IAdvertisingSetCallback; +import android.os.RemoteException; +import android.util.Log; + +/** + * This class 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 class AdvertisingSet { + private static final String TAG = "AdvertisingSet"; + + private final IBluetoothGatt gatt; + private int advertiserId; + + /* package */ AdvertisingSet(int advertiserId, + IBluetoothManager bluetoothManager) { + this.advertiserId = advertiserId; + + try { + this.gatt = 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) { + this.advertiserId = advertiserId; + } + + /** + * Enables Advertising. This method returns immediately, the operation status is + * delivered + * through {@code callback.onAdvertisingEnabled()}. + * <p> + * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} + * + */ + public void enableAdvertising(boolean enable) { + try { + gatt.enableAdverisingSet(this.advertiserId, enable); + } 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. + */ + public void setAdvertisingData(AdvertiseData data) { + try { + gatt.setAdvertisingData(this.advertiserId, data); + } 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()}. + */ + public void setScanResponseData(AdvertiseData data) { + try { + gatt.setScanResponseData(this.advertiserId, data); + } catch (RemoteException e) { + Log.e(TAG, "remote exception - ", e); + } + } + + /** + * Update advertising parameters associated with this AdvertisingSet. Must be called when + * advertising is not active. This method returns immediately, the operation status is delivered + * through {@code callback.onAdvertisingParametersUpdated}. + */ + public void setAdvertisingParameters(AdvertisingSetParameters parameters) { + try { + gatt.setAdvertisingParameters(this.advertiserId, parameters); + } catch (RemoteException e) { + Log.e(TAG, "remote exception - ", e); + } + } + + /** + * Update periodic advertising parameters associated 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 { + gatt.setPeriodicAdvertisingParameters(this.advertiserId, 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()}. + */ + public void setPeriodicAdvertisingData(AdvertiseData data) { + try { + gatt.setPeriodicAdvertisingData(this.advertiserId, data); + } 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()}. + */ + public void periodicAdvertisingEnable(boolean enable) { + try { + gatt.periodicAdvertisingEnable(this.advertiserId, enable); + } catch (RemoteException e) { + Log.e(TAG, "remote exception - ", e); + } + } + + /** + * Returns advertiserId associated with thsi advertising set. + * + * @hide + */ + public int getAdvertiserId(){ + return advertiserId; + } +}
\ No newline at end of file |