Home | History | Annotate | Download | only in le
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.bluetooth.le;
     18 
     19 import android.bluetooth.BluetoothAdapter;
     20 import android.bluetooth.IBluetoothGatt;
     21 import android.bluetooth.IBluetoothManager;
     22 import android.bluetooth.le.IAdvertisingSetCallback;
     23 import android.os.RemoteException;
     24 import android.util.Log;
     25 
     26 /**
     27  * This class provides a way to control single Bluetooth LE advertising instance.
     28  * <p>
     29  * To get an instance of {@link AdvertisingSet}, call the
     30  * {@link BluetoothLeAdvertiser#startAdvertisingSet} method.
     31  * <p>
     32  * <b>Note:</b> Most of the methods here require {@link android.Manifest.permission#BLUETOOTH_ADMIN}
     33  * permission.
     34  *
     35  * @see AdvertiseData
     36  */
     37 public final class AdvertisingSet {
     38     private static final String TAG = "AdvertisingSet";
     39 
     40     private final IBluetoothGatt gatt;
     41     private int advertiserId;
     42 
     43     /* package */ AdvertisingSet(int advertiserId,
     44                                  IBluetoothManager bluetoothManager) {
     45         this.advertiserId = advertiserId;
     46 
     47         try {
     48           this.gatt = bluetoothManager.getBluetoothGatt();
     49         } catch (RemoteException e) {
     50           Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
     51           throw new IllegalStateException("Failed to get Bluetooth");
     52         }
     53     }
     54 
     55     /* package */ void setAdvertiserId(int advertiserId) {
     56       this.advertiserId = advertiserId;
     57     }
     58 
     59     /**
     60      * Enables Advertising. This method returns immediately, the operation status is
     61      * delivered through {@code callback.onAdvertisingEnabled()}.
     62      * <p>
     63      * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
     64      *
     65      * @param enable whether the advertising should be enabled (true), or disabled (false)
     66      * @param duration advertising duration, in 10ms unit. Valid range is from 1 (10ms) to
     67      *                     65535 (655,350 ms)
     68      * @param maxExtendedAdvertisingEvents maximum number of extended advertising events the
     69      *                     controller shall attempt to send prior to terminating the extended
     70      *                     advertising, even if the duration has not expired. Valid range is
     71      *                     from 1 to 255.
     72      */
     73     public void enableAdvertising(boolean enable, int duration,
     74             int maxExtendedAdvertisingEvents) {
     75         try {
     76             gatt.enableAdvertisingSet(this.advertiserId, enable, duration,
     77                                       maxExtendedAdvertisingEvents);
     78         } catch (RemoteException e) {
     79             Log.e(TAG, "remote exception - ", e);
     80         }
     81     }
     82 
     83     /**
     84      * Set/update data being Advertised. Make sure that data doesn't exceed the size limit for
     85      * specified AdvertisingSetParameters. This method returns immediately, the operation status is
     86      * delivered through {@code callback.onAdvertisingDataSet()}.
     87      * <p>
     88      * Advertising data must be empty if non-legacy scannable advertising is used.
     89      *
     90      * @param advertiseData Advertisement data to be broadcasted. Size must not exceed
     91      *                     {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the
     92      *                     advertisement is connectable, three bytes will be added for flags. If the
     93      *                     update takes place when the advertising set is enabled, the data can be
     94      *                     maximum 251 bytes long.
     95      */
     96     public void setAdvertisingData(AdvertiseData advertiseData) {
     97         try {
     98             gatt.setAdvertisingData(this.advertiserId, advertiseData);
     99         } catch (RemoteException e) {
    100             Log.e(TAG, "remote exception - ", e);
    101         }
    102     }
    103 
    104     /**
    105      * Set/update scan response data. Make sure that data doesn't exceed the size limit for
    106      * specified AdvertisingSetParameters. This method returns immediately, the operation status
    107      * is delivered through {@code callback.onScanResponseDataSet()}.
    108      *
    109      * @param scanResponse Scan response associated with the advertisement data. Size must not
    110      *                     exceed {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the
    111      *                     update takes place when the advertising set is enabled, the data can be
    112      *                     maximum 251 bytes long.
    113      */
    114     public void setScanResponseData(AdvertiseData scanResponse) {
    115         try {
    116             gatt.setScanResponseData(this.advertiserId, scanResponse);
    117         } catch (RemoteException e) {
    118             Log.e(TAG, "remote exception - ", e);
    119         }
    120     }
    121 
    122     /**
    123      * Update advertising parameters associated with this AdvertisingSet. Must be called when
    124      * advertising is not active. This method returns immediately, the operation status is delivered
    125      * through {@code callback.onAdvertisingParametersUpdated}.
    126      *
    127      * @param parameters advertising set parameters.
    128      */
    129     public void setAdvertisingParameters(AdvertisingSetParameters parameters) {
    130         try {
    131             gatt.setAdvertisingParameters(this.advertiserId, parameters);
    132         } catch (RemoteException e) {
    133             Log.e(TAG, "remote exception - ", e);
    134         }
    135     }
    136 
    137     /**
    138      * Update periodic advertising parameters associated with this set. Must be called when
    139      * periodic advertising is not enabled. This method returns immediately, the operation
    140      * status is delivered through {@code callback.onPeriodicAdvertisingParametersUpdated()}.
    141      */
    142     public void setPeriodicAdvertisingParameters(PeriodicAdvertisingParameters parameters) {
    143         try {
    144             gatt.setPeriodicAdvertisingParameters(this.advertiserId, parameters);
    145         } catch (RemoteException e) {
    146             Log.e(TAG, "remote exception - ", e);
    147         }
    148     }
    149 
    150     /**
    151      * Used to set periodic advertising data, must be called after setPeriodicAdvertisingParameters,
    152      * or after advertising was started with periodic advertising data set. This method returns
    153      * immediately, the operation status is delivered through
    154      * {@code callback.onPeriodicAdvertisingDataSet()}.
    155      *
    156      * @param periodicData Periodic advertising data. Size must not exceed
    157      *                     {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the
    158      *                     update takes place when the periodic advertising is enabled for this set,
    159      *                     the data can be maximum 251 bytes long.
    160      */
    161     public void setPeriodicAdvertisingData(AdvertiseData periodicData) {
    162         try {
    163             gatt.setPeriodicAdvertisingData(this.advertiserId, periodicData);
    164         } catch (RemoteException e) {
    165             Log.e(TAG, "remote exception - ", e);
    166         }
    167     }
    168 
    169     /**
    170      * Used to enable/disable periodic advertising. This method returns immediately, the operation
    171      * status is delivered through {@code callback.onPeriodicAdvertisingEnable()}.
    172      *
    173      * @param enable whether the periodic advertising should be enabled (true), or disabled (false).
    174      */
    175     public void setPeriodicAdvertisingEnabled(boolean enable) {
    176         try {
    177             gatt.setPeriodicAdvertisingEnable(this.advertiserId, enable);
    178         } catch (RemoteException e) {
    179             Log.e(TAG, "remote exception - ", e);
    180         }
    181     }
    182 
    183     /**
    184      * Returns address associated with this advertising set.
    185      * This method is exposed only for Bluetooth PTS tests, no app or system service
    186      * should ever use it.
    187      *
    188      * This method requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED} permission.
    189      * @hide
    190      */
    191     public void getOwnAddress(){
    192         try {
    193             gatt.getOwnAddress(this.advertiserId);
    194         } catch (RemoteException e) {
    195             Log.e(TAG, "remote exception - ", e);
    196         }
    197     }
    198 
    199     /**
    200      * Returns advertiserId associated with this advertising set.
    201      *
    202      * @hide
    203      */
    204     public int getAdvertiserId(){
    205       return advertiserId;
    206     }
    207 }