blob: d6991bf8767decece3e2e394b4ddbdd607526db3 [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)
66 * @param timeoutMillis duration for which that advertising set is enabled.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080067 */
Jakub Pawlowskiadbf2ee2017-03-17 11:12:15 -070068 public void enableAdvertising(boolean enable, int timeout) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080069 try {
Jakub Pawlowski6a55da92017-03-17 15:33:27 -070070 gatt.enableAdvertisingSet(this.advertiserId, enable, timeout);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080071 } catch (RemoteException e) {
72 Log.e(TAG, "remote exception - ", e);
73 }
74 }
75
76 /**
77 * Set/update data being Advertised. Make sure that data doesn't exceed the size limit for
78 * specified AdvertisingSetParameters. This method returns immediately, the operation status is
79 * delivered through {@code callback.onAdvertisingDataSet()}.
80 * <p>
81 * Advertising data must be empty if non-legacy scannable advertising is used.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070082 *
83 * @param advertiseData Advertisement data to be broadcasted. Size must not exceed
84 * {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the
85 * advertisement is connectable, three bytes will be added for flags. If the
86 * update takes place when the advertising set is enabled, the data can be
87 * maximum 251 bytes long.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080088 */
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070089 public void setAdvertisingData(AdvertiseData advertiseData) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080090 try {
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -070091 gatt.setAdvertisingData(this.advertiserId, advertiseData);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -080092 } catch (RemoteException e) {
93 Log.e(TAG, "remote exception - ", e);
94 }
95 }
96
97 /**
98 * Set/update scan response data. Make sure that data doesn't exceed the size limit for
99 * specified AdvertisingSetParameters. This method returns immediately, the operation status
100 * is delivered through {@code callback.onScanResponseDataSet()}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700101 *
102 * @param scanResponse Scan response associated with the advertisement data. Size must not
103 * exceed {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the
104 * update takes place when the advertising set is enabled, the data can be
105 * maximum 251 bytes long.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800106 */
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700107 public void setScanResponseData(AdvertiseData scanResponse) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800108 try {
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700109 gatt.setScanResponseData(this.advertiserId, scanResponse);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800110 } catch (RemoteException e) {
111 Log.e(TAG, "remote exception - ", e);
112 }
113 }
114
115 /**
116 * Update advertising parameters associated with this AdvertisingSet. Must be called when
117 * advertising is not active. This method returns immediately, the operation status is delivered
118 * through {@code callback.onAdvertisingParametersUpdated}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700119 *
120 * @param parameters advertising set parameters.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800121 */
122 public void setAdvertisingParameters(AdvertisingSetParameters parameters) {
123 try {
124 gatt.setAdvertisingParameters(this.advertiserId, parameters);
125 } catch (RemoteException e) {
126 Log.e(TAG, "remote exception - ", e);
127 }
128 }
129
130 /**
131 * Update periodic advertising parameters associated with this set. Must be called when
132 * periodic advertising is not enabled. This method returns immediately, the operation
133 * status is delivered through {@code callback.onPeriodicAdvertisingParametersUpdated()}.
134 */
135 public void setPeriodicAdvertisingParameters(PeriodicAdvertisingParameters parameters) {
136 try {
137 gatt.setPeriodicAdvertisingParameters(this.advertiserId, parameters);
138 } catch (RemoteException e) {
139 Log.e(TAG, "remote exception - ", e);
140 }
141 }
142
143 /**
144 * Used to set periodic advertising data, must be called after setPeriodicAdvertisingParameters,
145 * or after advertising was started with periodic advertising data set. This method returns
146 * immediately, the operation status is delivered through
147 * {@code callback.onPeriodicAdvertisingDataSet()}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700148 *
149 * @param periodicData Periodic advertising data. Size must not exceed
150 * {@link BluetoothAdapter#getLeMaximumAdvertisingDataLength}. If the
151 * update takes place when the periodic advertising is enabled for this set,
152 * the data can be maximum 251 bytes long.
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800153 */
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700154 public void setPeriodicAdvertisingData(AdvertiseData periodicData) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800155 try {
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700156 gatt.setPeriodicAdvertisingData(this.advertiserId, periodicData);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800157 } catch (RemoteException e) {
158 Log.e(TAG, "remote exception - ", e);
159 }
160 }
161
162 /**
163 * Used to enable/disable periodic advertising. This method returns immediately, the operation
164 * status is delivered through {@code callback.onPeriodicAdvertisingEnable()}.
Jakub Pawlowskid89e4ba2017-03-30 11:19:24 -0700165 *
166 * @param enable whether the periodic advertising should be enabled (true), or disabled (false).
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800167 */
Jakub Pawlowski6a55da92017-03-17 15:33:27 -0700168 public void setPeriodicAdvertisingEnable(boolean enable) {
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800169 try {
Jakub Pawlowski6a55da92017-03-17 15:33:27 -0700170 gatt.setPeriodicAdvertisingEnable(this.advertiserId, enable);
Jakub Pawlowskia9d1a322017-01-10 06:15:54 -0800171 } catch (RemoteException e) {
172 Log.e(TAG, "remote exception - ", e);
173 }
174 }
175
176 /**
177 * Returns advertiserId associated with thsi advertising set.
178 *
179 * @hide
180 */
181 public int getAdvertiserId(){
182 return advertiserId;
183 }
184}