blob: 51571b2746a4feb2e6ba5e6cdbc0e8fa8e134e11 [file] [log] [blame]
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -08001/*
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
17package android.bluetooth.le;
18
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070019import android.bluetooth.BluetoothAdapter;
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080020import android.bluetooth.IBluetoothGatt;
21import android.bluetooth.IBluetoothManager;
22import android.bluetooth.le.IAdvertisingSetCallback;
23import android.os.RemoteException;
24import 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 */
37public 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
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070061 * delivered through {@code callback.onAdvertisingEnabled()}.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080062 * <p>
63 * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
64 *
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070065 * @param enable whether the advertising should be enabled (true), or disabled (false)
Jakub Pawlowski260846b2017-03-30 19:10:08 -070066 * @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.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080072 */
Jakub Pawlowski260846b2017-03-30 19:10:08 -070073 public void enableAdvertising(boolean enable, int duration,
74 int maxExtendedAdvertisingEvents) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080075 try {
Jakub Pawlowski260846b2017-03-30 19:10:08 -070076 gatt.enableAdvertisingSet(this.advertiserId, enable, duration,
77 maxExtendedAdvertisingEvents);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080078 } 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.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070089 *
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.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080095 */
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070096 public void setAdvertisingData(AdvertiseData advertiseData) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080097 try {
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070098 gatt.setAdvertisingData(this.advertiserId, advertiseData);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080099 } 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()}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700108 *
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.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800113 */
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700114 public void setScanResponseData(AdvertiseData scanResponse) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800115 try {
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700116 gatt.setScanResponseData(this.advertiserId, scanResponse);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800117 } 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}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700126 *
127 * @param parameters advertising set parameters.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800128 */
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()}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700155 *
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.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800160 */
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700161 public void setPeriodicAdvertisingData(AdvertiseData periodicData) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800162 try {
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700163 gatt.setPeriodicAdvertisingData(this.advertiserId, periodicData);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800164 } 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()}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700172 *
173 * @param enable whether the periodic advertising should be enabled (true), or disabled (false).
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800174 */
Jakub Pawlowski6a55da92017-03-17 15:33:27 -0700175 public void setPeriodicAdvertisingEnable(boolean enable) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800176 try {
Jakub Pawlowski6a55da92017-03-17 15:33:27 -0700177 gatt.setPeriodicAdvertisingEnable(this.advertiserId, enable);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800178 } catch (RemoteException e) {
179 Log.e(TAG, "remote exception - ", e);
180 }
181 }
182
183 /**
184 * Returns advertiserId associated with thsi advertising set.
185 *
186 * @hide
187 */
188 public int getAdvertiserId(){
189 return advertiserId;
190 }
191}