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) |
| 66 | * @param timeoutMillis duration for which that advertising set is enabled. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 67 | */ |
Jakub Pawlowski | adbf2ee | 2017-03-17 11:12:15 -0700 | [diff] [blame] | 68 | public void enableAdvertising(boolean enable, int timeout) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 69 | try { |
Jakub Pawlowski | 6a55da9 | 2017-03-17 15:33:27 -0700 | [diff] [blame] | 70 | gatt.enableAdvertisingSet(this.advertiserId, enable, timeout); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 71 | } 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 Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 82 | * |
| 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 Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 88 | */ |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 89 | public void setAdvertisingData(AdvertiseData advertiseData) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 90 | try { |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 91 | gatt.setAdvertisingData(this.advertiserId, advertiseData); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 92 | } 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 Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 101 | * |
| 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 Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 106 | */ |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 107 | public void setScanResponseData(AdvertiseData scanResponse) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 108 | try { |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 109 | gatt.setScanResponseData(this.advertiserId, scanResponse); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 110 | } 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 Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 119 | * |
| 120 | * @param parameters advertising set parameters. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 121 | */ |
| 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 Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 148 | * |
| 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 Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 153 | */ |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 154 | public void setPeriodicAdvertisingData(AdvertiseData periodicData) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 155 | try { |
Jakub Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 156 | gatt.setPeriodicAdvertisingData(this.advertiserId, periodicData); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 157 | } 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 Pawlowski | d89e4ba | 2017-03-30 11:19:24 -0700 | [diff] [blame^] | 165 | * |
| 166 | * @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] | 167 | */ |
Jakub Pawlowski | 6a55da9 | 2017-03-17 15:33:27 -0700 | [diff] [blame] | 168 | public void setPeriodicAdvertisingEnable(boolean enable) { |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 169 | try { |
Jakub Pawlowski | 6a55da9 | 2017-03-17 15:33:27 -0700 | [diff] [blame] | 170 | gatt.setPeriodicAdvertisingEnable(this.advertiserId, enable); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 171 | } 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 | } |