Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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 | |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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 |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 61 | * delivered through {@code callback.onAdvertisingEnabled()}. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 62 | * <p> |
| 63 | * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 64 | * |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 65 | * @param enable whether the advertising should be enabled (true), or disabled (false) |
Jakub Pawlowski | 260846b | 2017-03-30 19:10:08 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 72 | */ |
Jakub Pawlowski | 260846b | 2017-03-30 19:10:08 -0700 | [diff] [blame] | 73 | public void enableAdvertising(boolean enable, int duration, |
| 74 | int maxExtendedAdvertisingEvents) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 75 | try { |
Jakub Pawlowski | 260846b | 2017-03-30 19:10:08 -0700 | [diff] [blame] | 76 | gatt.enableAdvertisingSet(this.advertiserId, enable, duration, |
| 77 | maxExtendedAdvertisingEvents); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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. |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 95 | */ |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 96 | public void setAdvertisingData(AdvertiseData advertiseData) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 97 | try { |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 98 | gatt.setAdvertisingData(this.advertiserId, advertiseData); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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()}. |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 113 | */ |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 114 | public void setScanResponseData(AdvertiseData scanResponse) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 115 | try { |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 116 | gatt.setScanResponseData(this.advertiserId, scanResponse); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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}. |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 126 | * |
| 127 | * @param parameters advertising set parameters. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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()}. |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 160 | */ |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 161 | public void setPeriodicAdvertisingData(AdvertiseData periodicData) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 162 | try { |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 163 | gatt.setPeriodicAdvertisingData(this.advertiserId, periodicData); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 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()}. |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 172 | * |
| 173 | * @param enable whether the periodic advertising should be enabled (true), or disabled (false). |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 174 | */ |
Jakub Pawlowski | 6a55da9 | 2017-03-17 15:33:27 -0700 | [diff] [blame] | 175 | public void setPeriodicAdvertisingEnable(boolean enable) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 176 | try { |
Jakub Pawlowski | 6a55da9 | 2017-03-17 15:33:27 -0700 | [diff] [blame] | 177 | gatt.setPeriodicAdvertisingEnable(this.advertiserId, enable); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 178 | } 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 | } |