Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 19 | import android.bluetooth.BluetoothAdapter; |
| 20 | import android.bluetooth.BluetoothGatt; |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 21 | import android.bluetooth.BluetoothUuid; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 22 | import android.bluetooth.IBluetoothGatt; |
Wei Wang | 9fb1791 | 2014-07-01 15:10:06 -0700 | [diff] [blame] | 23 | import android.bluetooth.IBluetoothManager; |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 24 | import android.bluetooth.le.IAdvertiserCallback; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
| 27 | import android.os.ParcelUuid; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
| 30 | |
| 31 | import java.util.HashMap; |
| 32 | import java.util.Map; |
| 33 | import java.util.UUID; |
| 34 | |
| 35 | /** |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 36 | * This class provides a way to perform Bluetooth LE advertise operations, such as starting and |
| 37 | * stopping advertising. An advertiser can broadcast up to 31 bytes of advertisement data |
| 38 | * represented by {@link AdvertiseData}. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 39 | * <p> |
| 40 | * To get an instance of {@link BluetoothLeAdvertiser}, call the |
| 41 | * {@link BluetoothAdapter#getBluetoothLeAdvertiser()} method. |
| 42 | * <p> |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 43 | * <b>Note:</b> Most of the methods here require {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 44 | * permission. |
| 45 | * |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 46 | * @see AdvertiseData |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 47 | */ |
| 48 | public final class BluetoothLeAdvertiser { |
| 49 | |
| 50 | private static final String TAG = "BluetoothLeAdvertiser"; |
| 51 | |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 52 | private static final int MAX_ADVERTISING_DATA_BYTES = 31; |
| 53 | // Each fields need one byte for field length and another byte for field type. |
| 54 | private static final int OVERHEAD_BYTES_PER_FIELD = 2; |
| 55 | // Flags field will be set by system. |
| 56 | private static final int FLAGS_FIELD_BYTES = 3; |
| 57 | private static final int MANUFACTURER_SPECIFIC_DATA_LENGTH = 2; |
| 58 | private static final int SERVICE_DATA_UUID_LENGTH = 2; |
| 59 | |
Wei Wang | 9fb1791 | 2014-07-01 15:10:06 -0700 | [diff] [blame] | 60 | private final IBluetoothManager mBluetoothManager; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 61 | private final Handler mHandler; |
Prerepa Viswanadham | 8e5270f | 2014-07-08 16:33:34 -0700 | [diff] [blame] | 62 | private BluetoothAdapter mBluetoothAdapter; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 63 | private final Map<AdvertiseCallback, AdvertiseCallbackWrapper> |
| 64 | mLeAdvertisers = new HashMap<AdvertiseCallback, AdvertiseCallbackWrapper>(); |
| 65 | |
| 66 | /** |
| 67 | * Use BluetoothAdapter.getLeAdvertiser() instead. |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 68 | * |
| 69 | * @param bluetoothManager BluetoothManager that conducts overall Bluetooth Management |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 70 | * @hide |
| 71 | */ |
Wei Wang | 9fb1791 | 2014-07-01 15:10:06 -0700 | [diff] [blame] | 72 | public BluetoothLeAdvertiser(IBluetoothManager bluetoothManager) { |
| 73 | mBluetoothManager = bluetoothManager; |
Prerepa Viswanadham | 8e5270f | 2014-07-08 16:33:34 -0700 | [diff] [blame] | 74 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 75 | mHandler = new Handler(Looper.getMainLooper()); |
| 76 | } |
| 77 | |
| 78 | /** |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 79 | * Start Bluetooth LE Advertising. On success, the {@code advertiseData} will be broadcasted. |
| 80 | * Returns immediately, the operation status is delivered through {@code callback}. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 81 | * <p> |
| 82 | * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 83 | * |
| 84 | * @param settings Settings for Bluetooth LE advertising. |
| 85 | * @param advertiseData Advertisement data to be broadcasted. |
| 86 | * @param callback Callback for advertising status. |
| 87 | */ |
| 88 | public void startAdvertising(AdvertiseSettings settings, |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 89 | AdvertiseData advertiseData, final AdvertiseCallback callback) { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 90 | startAdvertising(settings, advertiseData, null, callback); |
| 91 | } |
| 92 | |
| 93 | /** |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 94 | * Start Bluetooth LE Advertising. The {@code advertiseData} will be broadcasted if the |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 95 | * operation succeeds. The {@code scanResponse} is returned when a scanning device sends an |
| 96 | * active scan request. This method returns immediately, the operation status is delivered |
| 97 | * through {@code callback}. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 98 | * <p> |
| 99 | * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 100 | * |
| 101 | * @param settings Settings for Bluetooth LE advertising. |
| 102 | * @param advertiseData Advertisement data to be advertised in advertisement packet. |
| 103 | * @param scanResponse Scan response associated with the advertisement data. |
| 104 | * @param callback Callback for advertising status. |
| 105 | */ |
| 106 | public void startAdvertising(AdvertiseSettings settings, |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 107 | AdvertiseData advertiseData, AdvertiseData scanResponse, |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 108 | final AdvertiseCallback callback) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 109 | synchronized (mLeAdvertisers) { |
Wei Wang | 833559d | 2014-08-29 10:26:13 -0700 | [diff] [blame] | 110 | BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter); |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 111 | if (callback == null) { |
| 112 | throw new IllegalArgumentException("callback cannot be null"); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 113 | } |
Tom Turney | 29230ce | 2014-11-12 16:26:41 -0800 | [diff] [blame] | 114 | boolean isConnectable = settings.isConnectable(); |
| 115 | if (totalBytes(advertiseData, isConnectable) > MAX_ADVERTISING_DATA_BYTES || |
Prerepa Viswanadham | e77adab | 2015-01-16 10:40:11 -0800 | [diff] [blame] | 116 | totalBytes(scanResponse, false) > MAX_ADVERTISING_DATA_BYTES) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 117 | postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE); |
| 118 | return; |
| 119 | } |
| 120 | if (mLeAdvertisers.containsKey(callback)) { |
| 121 | postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | IBluetoothGatt gatt; |
| 126 | try { |
| 127 | gatt = mBluetoothManager.getBluetoothGatt(); |
| 128 | } catch (RemoteException e) { |
| 129 | Log.e(TAG, "Failed to get Bluetooth gatt - ", e); |
| 130 | postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR); |
| 131 | return; |
| 132 | } |
| 133 | AdvertiseCallbackWrapper wrapper = new AdvertiseCallbackWrapper(callback, advertiseData, |
| 134 | scanResponse, settings, gatt); |
| 135 | wrapper.startRegisteration(); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Stop Bluetooth LE advertising. The {@code callback} must be the same one use in |
| 141 | * {@link BluetoothLeAdvertiser#startAdvertising}. |
| 142 | * <p> |
| 143 | * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 144 | * |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 145 | * @param callback {@link AdvertiseCallback} identifies the advertising instance to stop. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 146 | */ |
| 147 | public void stopAdvertising(final AdvertiseCallback callback) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 148 | synchronized (mLeAdvertisers) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 149 | if (callback == null) { |
| 150 | throw new IllegalArgumentException("callback cannot be null"); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 151 | } |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 152 | AdvertiseCallbackWrapper wrapper = mLeAdvertisers.get(callback); |
| 153 | if (wrapper == null) return; |
| 154 | wrapper.stopAdvertising(); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Wei Wang | ee80922 | 2014-08-12 22:16:32 -0700 | [diff] [blame] | 158 | /** |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 159 | * Cleans up advertisers. Should be called when bluetooth is down. |
Wei Wang | ee80922 | 2014-08-12 22:16:32 -0700 | [diff] [blame] | 160 | * |
| 161 | * @hide |
| 162 | */ |
| 163 | public void cleanup() { |
| 164 | mLeAdvertisers.clear(); |
| 165 | } |
| 166 | |
Prerepa Viswanadham | e77adab | 2015-01-16 10:40:11 -0800 | [diff] [blame] | 167 | // Compute the size of advertisement data or scan resp |
| 168 | private int totalBytes(AdvertiseData data, boolean isFlagsIncluded) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 169 | if (data == null) return 0; |
Tom Turney | 29230ce | 2014-11-12 16:26:41 -0800 | [diff] [blame] | 170 | // Flags field is omitted if the advertising is not connectable. |
Prerepa Viswanadham | e77adab | 2015-01-16 10:40:11 -0800 | [diff] [blame] | 171 | int size = (isFlagsIncluded) ? FLAGS_FIELD_BYTES : 0; |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 172 | if (data.getServiceUuids() != null) { |
| 173 | int num16BitUuids = 0; |
| 174 | int num32BitUuids = 0; |
| 175 | int num128BitUuids = 0; |
| 176 | for (ParcelUuid uuid : data.getServiceUuids()) { |
| 177 | if (BluetoothUuid.is16BitUuid(uuid)) { |
| 178 | ++num16BitUuids; |
| 179 | } else if (BluetoothUuid.is32BitUuid(uuid)) { |
| 180 | ++num32BitUuids; |
| 181 | } else { |
| 182 | ++num128BitUuids; |
| 183 | } |
| 184 | } |
| 185 | // 16 bit service uuids are grouped into one field when doing advertising. |
| 186 | if (num16BitUuids != 0) { |
| 187 | size += OVERHEAD_BYTES_PER_FIELD + |
| 188 | num16BitUuids * BluetoothUuid.UUID_BYTES_16_BIT; |
| 189 | } |
| 190 | // 32 bit service uuids are grouped into one field when doing advertising. |
| 191 | if (num32BitUuids != 0) { |
| 192 | size += OVERHEAD_BYTES_PER_FIELD + |
| 193 | num32BitUuids * BluetoothUuid.UUID_BYTES_32_BIT; |
| 194 | } |
| 195 | // 128 bit service uuids are grouped into one field when doing advertising. |
| 196 | if (num128BitUuids != 0) { |
| 197 | size += OVERHEAD_BYTES_PER_FIELD + |
| 198 | num128BitUuids * BluetoothUuid.UUID_BYTES_128_BIT; |
| 199 | } |
| 200 | } |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 201 | for (ParcelUuid uuid : data.getServiceData().keySet()) { |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 202 | size += OVERHEAD_BYTES_PER_FIELD + SERVICE_DATA_UUID_LENGTH |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 203 | + byteLength(data.getServiceData().get(uuid)); |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 204 | } |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 205 | for (int i = 0; i < data.getManufacturerSpecificData().size(); ++i) { |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 206 | size += OVERHEAD_BYTES_PER_FIELD + MANUFACTURER_SPECIFIC_DATA_LENGTH + |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 207 | byteLength(data.getManufacturerSpecificData().valueAt(i)); |
Wei Wang | 0e81ca2 | 2014-07-23 20:33:31 -0700 | [diff] [blame] | 208 | } |
| 209 | if (data.getIncludeTxPowerLevel()) { |
| 210 | size += OVERHEAD_BYTES_PER_FIELD + 1; // tx power level value is one byte. |
| 211 | } |
| 212 | if (data.getIncludeDeviceName() && mBluetoothAdapter.getName() != null) { |
| 213 | size += OVERHEAD_BYTES_PER_FIELD + mBluetoothAdapter.getName().length(); |
| 214 | } |
| 215 | return size; |
| 216 | } |
| 217 | |
| 218 | private int byteLength(byte[] array) { |
| 219 | return array == null ? 0 : array.length; |
| 220 | } |
| 221 | |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Bluetooth GATT interface callbacks for advertising. |
| 224 | */ |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 225 | private class AdvertiseCallbackWrapper extends IAdvertiserCallback.Stub { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 226 | private static final int LE_CALLBACK_TIMEOUT_MILLIS = 2000; |
| 227 | private final AdvertiseCallback mAdvertiseCallback; |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 228 | private final AdvertiseData mAdvertisement; |
| 229 | private final AdvertiseData mScanResponse; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 230 | private final AdvertiseSettings mSettings; |
| 231 | private final IBluetoothGatt mBluetoothGatt; |
| 232 | |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 233 | // mAdvertiserId -1: not registered |
| 234 | // -2: advertise stopped or registration timeout |
| 235 | // >=0: registered and advertising started |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 236 | private int mAdvertiserId; |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 237 | private boolean mIsAdvertising = false; |
Jakub Pawlowski | 76f517a | 2016-11-22 12:44:22 -0800 | [diff] [blame] | 238 | private int registrationError = AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 239 | |
| 240 | public AdvertiseCallbackWrapper(AdvertiseCallback advertiseCallback, |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 241 | AdvertiseData advertiseData, AdvertiseData scanResponse, |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 242 | AdvertiseSettings settings, |
| 243 | IBluetoothGatt bluetoothGatt) { |
| 244 | mAdvertiseCallback = advertiseCallback; |
| 245 | mAdvertisement = advertiseData; |
| 246 | mScanResponse = scanResponse; |
| 247 | mSettings = settings; |
| 248 | mBluetoothGatt = bluetoothGatt; |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 249 | mAdvertiserId = -1; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 252 | public void startRegisteration() { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 253 | synchronized (this) { |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 254 | if (mAdvertiserId == -2) return; |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 255 | |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 256 | try { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 257 | mBluetoothGatt.registerAdvertiser(this); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 258 | wait(LE_CALLBACK_TIMEOUT_MILLIS); |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 259 | } catch (InterruptedException | RemoteException e) { |
| 260 | Log.e(TAG, "Failed to start registeration", e); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 261 | } |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 262 | if (mAdvertiserId >= 0 && mIsAdvertising) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 263 | mLeAdvertisers.put(mAdvertiseCallback, this); |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 264 | } else if (mAdvertiserId < 0) { |
Wei Wang | 02bc008 | 2015-11-09 19:45:53 -0800 | [diff] [blame] | 265 | |
Jakub Pawlowski | 76f517a | 2016-11-22 12:44:22 -0800 | [diff] [blame] | 266 | // Registration timeout, reset mClientIf to -2 so no subsequent operations can |
Wei Wang | 02bc008 | 2015-11-09 19:45:53 -0800 | [diff] [blame] | 267 | // proceed. |
Jakub Pawlowski | 76f517a | 2016-11-22 12:44:22 -0800 | [diff] [blame] | 268 | if (mAdvertiserId == -1) mAdvertiserId = -2; |
Wei Wang | 833559d | 2014-08-29 10:26:13 -0700 | [diff] [blame] | 269 | // Post internal error if registration failed. |
Jakub Pawlowski | 76f517a | 2016-11-22 12:44:22 -0800 | [diff] [blame] | 270 | postStartFailure(mAdvertiseCallback, registrationError); |
Wei Wang | 833559d | 2014-08-29 10:26:13 -0700 | [diff] [blame] | 271 | } else { |
| 272 | // Unregister application if it's already registered but advertise failed. |
| 273 | try { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 274 | mBluetoothGatt.unregisterAdvertiser(mAdvertiserId); |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 275 | mAdvertiserId = -2; |
Wei Wang | 833559d | 2014-08-29 10:26:13 -0700 | [diff] [blame] | 276 | } catch (RemoteException e) { |
| 277 | Log.e(TAG, "remote exception when unregistering", e); |
| 278 | } |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 279 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 280 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 283 | public void stopAdvertising() { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 284 | synchronized (this) { |
| 285 | try { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 286 | mBluetoothGatt.stopMultiAdvertising(mAdvertiserId); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 287 | wait(LE_CALLBACK_TIMEOUT_MILLIS); |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 288 | } catch (InterruptedException | RemoteException e) { |
| 289 | Log.e(TAG, "Failed to stop advertising", e); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 290 | } |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 291 | // Advertise callback should have been removed from LeAdvertisers when |
| 292 | // onMultiAdvertiseCallback was called. In case onMultiAdvertiseCallback is never |
| 293 | // invoked and wait timeout expires, remove callback here. |
| 294 | if (mLeAdvertisers.containsKey(mAdvertiseCallback)) { |
| 295 | mLeAdvertisers.remove(mAdvertiseCallback); |
| 296 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 301 | * Advertiser interface registered - app is ready to go |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 302 | */ |
| 303 | @Override |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 304 | public void onAdvertiserRegistered(int status, int advertiserId) { |
| 305 | Log.d(TAG, "onAdvertiserRegistered() - status=" + status + " advertiserId=" + advertiserId); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 306 | synchronized (this) { |
| 307 | if (status == BluetoothGatt.GATT_SUCCESS) { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 308 | try { |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 309 | if (mAdvertiserId == -2) { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 310 | // Registration succeeds after timeout, unregister advertiser. |
| 311 | mBluetoothGatt.unregisterAdvertiser(advertiserId); |
Wei Wang | 02bc008 | 2015-11-09 19:45:53 -0800 | [diff] [blame] | 312 | } else { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 313 | mAdvertiserId = advertiserId; |
| 314 | mBluetoothGatt.startMultiAdvertising(mAdvertiserId, mAdvertisement, |
Wei Wang | 02bc008 | 2015-11-09 19:45:53 -0800 | [diff] [blame] | 315 | mScanResponse, mSettings); |
| 316 | } |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 317 | return; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 318 | } catch (RemoteException e) { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 319 | Log.e(TAG, "failed to start advertising", e); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 320 | } |
Jakub Pawlowski | 76f517a | 2016-11-22 12:44:22 -0800 | [diff] [blame] | 321 | } else if (status == AdvertiseCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS) { |
| 322 | registrationError = status; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 323 | } |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 324 | // Registration failed. |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 325 | mAdvertiserId = -2; |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 326 | notifyAll(); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | @Override |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 331 | public void onMultiAdvertiseCallback(int status, boolean isStart, |
| 332 | AdvertiseSettings settings) { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 333 | synchronized (this) { |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 334 | if (isStart) { |
| 335 | if (status == AdvertiseCallback.ADVERTISE_SUCCESS) { |
| 336 | // Start success |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 337 | mIsAdvertising = true; |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 338 | postStartSuccess(mAdvertiseCallback, settings); |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 339 | } else { |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 340 | // Start failure. |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 341 | postStartFailure(mAdvertiseCallback, status); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 342 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 343 | } else { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 344 | // unregister advertiser for stop. |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 345 | try { |
Jakub Pawlowski | a480f7f | 2016-08-05 06:40:31 -0700 | [diff] [blame] | 346 | mBluetoothGatt.unregisterAdvertiser(mAdvertiserId); |
Jakub Pawlowski | 3d83b0e | 2016-11-04 15:25:57 -0700 | [diff] [blame] | 347 | mAdvertiserId = -2; |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 348 | mIsAdvertising = false; |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 349 | mLeAdvertisers.remove(mAdvertiseCallback); |
Wei Wang | e0d4afb | 2014-07-29 21:34:25 -0700 | [diff] [blame] | 350 | } catch (RemoteException e) { |
| 351 | Log.e(TAG, "remote exception when unregistering", e); |
| 352 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 353 | } |
| 354 | notifyAll(); |
| 355 | } |
| 356 | |
| 357 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 360 | private void postStartFailure(final AdvertiseCallback callback, final int error) { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 361 | mHandler.post(new Runnable() { |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 362 | @Override |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 363 | public void run() { |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 364 | callback.onStartFailure(error); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 365 | } |
| 366 | }); |
| 367 | } |
Wei Wang | 9a974be | 2014-08-18 21:58:32 -0700 | [diff] [blame] | 368 | |
| 369 | private void postStartSuccess(final AdvertiseCallback callback, |
| 370 | final AdvertiseSettings settings) { |
| 371 | mHandler.post(new Runnable() { |
| 372 | |
| 373 | @Override |
| 374 | public void run() { |
| 375 | callback.onStartSuccess(settings); |
| 376 | } |
| 377 | }); |
| 378 | } |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 379 | } |