1 /* 2 * Copyright (C) 2022 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.car.builtin.bluetooth.le; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.bluetooth.le.AdvertisingSet; 22 import android.bluetooth.le.AdvertisingSetCallback; 23 import android.car.builtin.annotation.AddedIn; 24 import android.car.builtin.annotation.PlatformVersion; 25 26 /** 27 * Provides access to {@code onOwnAddressRead} in 28 * {@code android.bluetooth.le.AdvertisingSetCallback}. 29 * @hide 30 */ 31 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 32 public final class AdvertisingSetCallbackHelper { 33 34 /** 35 * A proxy to {@code android.bluetooth.le.AdvertisingSetCallback}, since one of its methods 36 * is a hidden API. {@code AdvertisingSetCallback} is the Bluetooth LE advertising set 37 * callbacks, used to deliver advertising operation status. 38 */ 39 public abstract static class Callback { 40 41 /** 42 * Callback triggered in response to {@link BluetoothLeAdvertiser#startAdvertisingSet} 43 * indicating result of the operation. If status is ADVERTISE_SUCCESS, then advertisingSet 44 * contains the started set and it is advertising. If error occurred, advertisingSet is 45 * null, and status will be set to proper error code. 46 * 47 * @param advertisingSet The advertising set that was started or null if error. 48 * @param txPower tx power that will be used for this set. 49 * @param status Status of the operation. 50 */ 51 @AddedIn(PlatformVersion.TIRAMISU_1) onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status)52 public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, 53 int status) { 54 } 55 56 /** 57 * Callback triggered in response to {@link BluetoothLeAdvertiser#stopAdvertisingSet} 58 * indicating advertising set is stopped. 59 * 60 * @param advertisingSet The advertising set. 61 */ 62 @AddedIn(PlatformVersion.TIRAMISU_1) onAdvertisingSetStopped(AdvertisingSet advertisingSet)63 public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) { 64 } 65 66 /** 67 * Callback triggered in response to {@link BluetoothLeAdvertiser#startAdvertisingSet} 68 * indicating result of the operation. If status is ADVERTISE_SUCCESS, then advertising 69 * set is advertising. 70 * 71 * @param advertisingSet The advertising set. 72 * @param status Status of the operation. 73 */ 74 @AddedIn(PlatformVersion.TIRAMISU_1) onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status)75 public void onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, 76 int status) { 77 } 78 79 /** 80 * Callback triggered in response to {@link AdvertisingSet#setAdvertisingData} indicating 81 * result of the operation. If status is ADVERTISE_SUCCESS, then data was changed. 82 * 83 * @param advertisingSet The advertising set. 84 * @param status Status of the operation. 85 */ 86 @AddedIn(PlatformVersion.TIRAMISU_1) onAdvertisingDataSet(AdvertisingSet advertisingSet, int status)87 public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) { 88 } 89 90 /** 91 * Callback triggered in response to {@link AdvertisingSet#setAdvertisingData} indicating 92 * result of the operation. 93 * 94 * @param advertisingSet The advertising set. 95 * @param status Status of the operation. 96 */ 97 @AddedIn(PlatformVersion.TIRAMISU_1) onScanResponseDataSet(AdvertisingSet advertisingSet, int status)98 public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) { 99 } 100 101 /** 102 * Callback triggered in response to {@link AdvertisingSet#setAdvertisingParameters} 103 * indicating result of the operation. 104 * 105 * @param advertisingSet The advertising set. 106 * @param txPower tx power that will be used for this set. 107 * @param status Status of the operation. 108 */ 109 @AddedIn(PlatformVersion.TIRAMISU_1) onAdvertisingParametersUpdated(AdvertisingSet advertisingSet, int txPower, int status)110 public void onAdvertisingParametersUpdated(AdvertisingSet advertisingSet, 111 int txPower, int status) { 112 } 113 114 /** 115 * Callback triggered in response to {@link 116 * AdvertisingSet#setPeriodicAdvertisingParameters} indicating result of the operation. 117 * 118 * @param advertisingSet The advertising set. 119 * @param status Status of the operation. 120 */ 121 @AddedIn(PlatformVersion.TIRAMISU_1) onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet, int status)122 public void onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet, 123 int status) { 124 } 125 126 /** 127 * Callback triggered in response to {@link AdvertisingSet#setPeriodicAdvertisingData} 128 * indicating result of the operation. 129 * 130 * @param advertisingSet The advertising set. 131 * @param status Status of the operation. 132 */ 133 @AddedIn(PlatformVersion.TIRAMISU_1) onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet, int status)134 public void onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet, 135 int status) { 136 } 137 138 /** 139 * Callback triggered in response to {@link AdvertisingSet#setPeriodicAdvertisingEnabled} 140 * indicating result of the operation. 141 * 142 * @param advertisingSet The advertising set. 143 * @param status Status of the operation. 144 */ 145 @AddedIn(PlatformVersion.TIRAMISU_1) onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status)146 public void onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, 147 int status) { 148 } 149 150 /** 151 * Callback triggered in response to {@link AdvertisingSet#getOwnAddress()} 152 * indicating result of the operation. (In the real callback, this was hidden API). 153 * 154 * @param advertisingSet The advertising set. 155 * @param addressType type of address. 156 * @param address advertising set bluetooth address. 157 */ 158 @AddedIn(PlatformVersion.TIRAMISU_1) onOwnAddressRead(AdvertisingSet advertisingSet, int addressType, String address)159 public void onOwnAddressRead(AdvertisingSet advertisingSet, int addressType, 160 String address) { 161 } 162 } 163 AdvertisingSetCallbackHelper()164 private AdvertisingSetCallbackHelper() { 165 throw new UnsupportedOperationException("contains only static members"); 166 } 167 168 /** 169 * Creates a real {@link AdvertisingSetCallback} by wrapping a {@link Callback}. 170 * Wrapping is needed because some of the methods in {@link AdvertisingSetCallback} are 171 * hidden APIs, so cannot be overridden within a Mainline module. 172 * 173 * @param proxy The proxy of {@link AdvertisingSetCallback} that is to be wrapped into a 174 * real one. 175 * @return A real {@link AdvertisingSetCallback}. 176 */ 177 @AddedIn(PlatformVersion.TIRAMISU_1) createRealCallbackFromProxy( @onNull Callback proxy)178 public static AdvertisingSetCallback createRealCallbackFromProxy( 179 @NonNull Callback proxy) { 180 181 AdvertisingSetCallback realCallback = new AdvertisingSetCallback() { 182 @Override 183 public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, 184 int status) { 185 proxy.onAdvertisingSetStarted(advertisingSet, txPower, status); 186 } 187 188 @Override 189 public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) { 190 proxy.onAdvertisingSetStopped(advertisingSet); 191 } 192 193 @Override 194 public void onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, 195 int status) { 196 proxy.onAdvertisingEnabled(advertisingSet, enable, status); 197 } 198 199 @Override 200 public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) { 201 proxy.onAdvertisingDataSet(advertisingSet, status); 202 } 203 204 @Override 205 public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) { 206 proxy.onScanResponseDataSet(advertisingSet, status); 207 } 208 209 @Override 210 public void onAdvertisingParametersUpdated(AdvertisingSet advertisingSet, 211 int txPower, int status) { 212 proxy.onAdvertisingParametersUpdated(advertisingSet, txPower, status); 213 } 214 215 @Override 216 public void onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet, 217 int status) { 218 proxy.onPeriodicAdvertisingParametersUpdated(advertisingSet, status); 219 } 220 221 @Override 222 public void onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet, 223 int status) { 224 proxy.onPeriodicAdvertisingDataSet(advertisingSet, status); 225 } 226 227 @Override 228 public void onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, 229 int status) { 230 proxy.onPeriodicAdvertisingEnabled(advertisingSet, enable, status); 231 } 232 233 @Override 234 public void onOwnAddressRead(AdvertisingSet advertisingSet, int addressType, 235 String address) { 236 proxy.onOwnAddressRead(advertisingSet, addressType, address); 237 } 238 }; 239 240 return realCallback; 241 } 242 } 243