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