diff options
author | Googler <noreply@google.com> | 2024-04-23 15:30:06 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-04-23 15:30:42 -0700 |
commit | f962e53e401fc88fef263e4ee8168cd977f9b8af (patch) | |
tree | 04896789ce30d79070cda26a0315ac76d35303fc | |
parent | cb095e275d0d38786e9690426c106b41aa17bb9d (diff) | |
download | robolectric-f962e53e401fc88fef263e4ee8168cd977f9b8af.tar.gz |
Add #startAdvertisingSet API support in ShadowBluetoothLeAdvertiser.
Extends ShadowBluetoothLeAdvertiser minSdk from O to L.
PiperOrigin-RevId: 627523972
2 files changed, 196 insertions, 324 deletions
diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiserTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiserTest.java index 9b2ffb50b..7dc4fe798 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiserTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiserTest.java @@ -1,13 +1,15 @@ package org.robolectric.shadows; -import static android.os.Build.VERSION_CODES.LOLLIPOP; import static android.os.Build.VERSION_CODES.O; +import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import static org.robolectric.Shadows.shadowOf; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothGattServer; +import android.bluetooth.BluetoothManager; import android.bluetooth.le.AdvertiseCallback; import android.bluetooth.le.AdvertiseData; import android.bluetooth.le.AdvertiseSettings; @@ -16,9 +18,11 @@ import android.bluetooth.le.AdvertisingSetCallback; import android.bluetooth.le.AdvertisingSetParameters; import android.bluetooth.le.BluetoothLeAdvertiser; import android.bluetooth.le.PeriodicAdvertisingParameters; +import android.content.Context; import android.os.Handler; import android.os.Looper; import android.os.ParcelUuid; +import androidx.test.core.app.ApplicationProvider; import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -28,7 +32,7 @@ import org.robolectric.annotation.Config; /** Unit tests for {@link ShadowBluetoothLeAdvertiser}. */ @RunWith(RobolectricTestRunner.class) -@Config(minSdk = LOLLIPOP) +@Config(minSdk = O) public class ShadowBluetoothLeAdvertiserTest { private static final String ADVERTISE_DATA_UUID1 = "00000000-0000-0000-0000-0000000000A1"; @@ -39,6 +43,13 @@ public class ShadowBluetoothLeAdvertiserTest { private static final String CALLBACK1_FAILURE_RESULT = "c1f"; private static final String CALLBACK2_SUCCESS_RESULT = "c2s"; private static final String CALLBACK2_FAILURE_RESULT = "c2f"; + + private final Context context = ApplicationProvider.getApplicationContext(); + private final BluetoothManager bluetoothManager = + context.getSystemService(BluetoothManager.class); + private final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); + private final ShadowBluetoothAdapter shadowBluetoothAdapter = shadowOf(bluetoothAdapter); + private final BluetoothGattServer gattServer = bluetoothManager.openGattServer(context, null); private final Handler mainLooperHandler = new Handler(Looper.getMainLooper()); private BluetoothLeAdvertiser bluetoothLeAdvertiser; @@ -51,6 +62,33 @@ public class ShadowBluetoothLeAdvertiserTest { private AdvertiseData scanResponse2; private AdvertiseCallback advertiseCallback1; private AdvertiseCallback advertiseCallback2; + + private Optional<Integer> advertisingSetStartStatusOptional; + private boolean advertisingSetStopped; + private AdvertisingSetCallback advertisingSetCallback = + new AdvertisingSetCallback() { + @Override + public void onAdvertisingSetStarted( + AdvertisingSet advertisingSet, int txPower, int status) { + advertisingSetStartStatusOptional = Optional.of(status); + /* switch (status) { + case AdvertisingSetCallback.ADVERTISE_SUCCESS: + advertisingSetStartStatusOptional. + break; + case AdvertisingSetCallback.ADVERTISE_FAILED_DATA_TOO_LARGE: + break; + case AdvertisingSetCallback.ADVERTISE_FAILED_ALREADY_STARTED: + break; + default: + break; */ + } + + @Override + public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) { + advertisingSetStopped = true; + } + }; + private String result; private int error; private AdvertiseSettings settings; @@ -132,6 +170,9 @@ public class ShadowBluetoothLeAdvertiserTest { .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_LOW) .setConnectable(false) .build(); + + advertisingSetStartStatusOptional = Optional.empty(); + advertisingSetStopped = false; } @Test @@ -205,10 +246,10 @@ public class ShadowBluetoothLeAdvertiserTest { .addServiceUuid(ParcelUuid.fromString("EEEEEEEE-FFFF-FFFF-FFFF-FFFFFFFFFFFF")) .build(); bluetoothLeAdvertiser.startAdvertising( - advertiseSettings1, /* advertiseData= */ null, oversizedData, advertiseCallback1); + advertiseSettings1, null, oversizedData, advertiseCallback1); assertThat(error).isEqualTo(AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE); bluetoothLeAdvertiser.startAdvertising( - advertiseSettings1, oversizedData, /* scanResponse= */ null, advertiseCallback1); + advertiseSettings1, oversizedData, null, advertiseCallback1); assertThat(error).isEqualTo(AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE); } @@ -461,86 +502,84 @@ public class ShadowBluetoothLeAdvertiserTest { } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet() { - AdvertisingStatus status = new AdvertisingStatus(); bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, - /* scanResponse= */ null, - /* periodicParameters= */ null, - /* periodicData= */ null, - createAdvertisingSetCallback(status)); - - assertThat(status.isStarted).isTrue(); + null, + null, + null, + 0, + 0, + gattServer, + advertisingSetCallback, + mainLooperHandler); + + assertThat(advertisingSetStartStatusOptional.get()) + .isEqualTo(AdvertisingSetCallback.ADVERTISE_SUCCESS); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(1); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_advertisingAlreadyStarted_invokeOnAdvertisingSetStartedWithAlreadyStartedStatusCode() { - AdvertisingStatus status = new AdvertisingStatus(); - AdvertisingSetCallback callback = createAdvertisingSetCallback(status); bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, - /* scanResponse= */ null, - /* periodicParameters= */ null, - /* periodicData= */ null, - callback); + null, + null, + null, + 0, + 0, + gattServer, + advertisingSetCallback, + mainLooperHandler); bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, - /* scanResponse= */ null, - /* periodicParameters= */ null, - /* periodicData= */ null, - callback); - - assertThat(status.getStatusCode()) + null, + null, + null, + 0, + 0, + gattServer, + advertisingSetCallback, + mainLooperHandler); + + assertThat(advertisingSetStartStatusOptional.get()) .isEqualTo(AdvertisingSetCallback.ADVERTISE_FAILED_ALREADY_STARTED); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(1); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_nullCallback_throwsIllegalArgumentException() { assertThrows( IllegalArgumentException.class, () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, - /* scanResponse= */ null, - /* periodicParameters= */ null, - /* periodicData= */ null, - /* callback= */ null)); + null, + null, + null, + 0, + 0, + gattServer, + null, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_legacyModeWithTooBigAdvertiseData_throwsIllegalArgumentException() { AdvertiseData oversizedData = @@ -554,22 +593,21 @@ public class ShadowBluetoothLeAdvertiserTest { () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), oversizedData, /* scanResponse= */ null, /* periodicParameters= */ null, /* periodicData= */ null, - createAdvertisingSetCallback(), + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_legacyModeWithTooBigScanResponse_throwsIllegalArgumentException() { AdvertiseData oversizedData = @@ -583,72 +621,72 @@ public class ShadowBluetoothLeAdvertiserTest { () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, oversizedData, /* periodicParameters= */ null, /* periodicData= */ null, - createAdvertisingSetCallback())); + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_nonLegacyModePrimaryPhySetButNotSupported_throwsIllegalArgumentException() { - shadowOf(BluetoothAdapter.getDefaultAdapter()).setIsLeCodedPhySupported(false); + shadowBluetoothAdapter.setIsLeCodedPhySupported(false); assertThrows( IllegalArgumentException.class, () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_1M), + false, true, false, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_1M), advertiseData1, /* scanResponse= */ null, /* periodicParameters= */ null, /* periodicData= */ null, - createAdvertisingSetCallback())); + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_nonLegacyModeSecondaryPhySetButNotSupported_throwsIllegalArgumentException() { - shadowOf(BluetoothAdapter.getDefaultAdapter()).setIsLeCodedPhySupported(false); + shadowBluetoothAdapter.setIsLeCodedPhySupported(false); assertThrows( IllegalArgumentException.class, () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_1M, - BluetoothDevice.PHY_LE_CODED), + false, true, false, BluetoothDevice.PHY_LE_1M, BluetoothDevice.PHY_LE_CODED), advertiseData1, /* scanResponse= */ null, /* periodicParameters= */ null, /* periodicData= */ null, - createAdvertisingSetCallback())); + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_nonLegacyModeWithTooBigAdvertiseData_throwsIllegalArgumentException() { - shadowOf(BluetoothAdapter.getDefaultAdapter()).setIsLeExtendedAdvertisingSupported(false); + shadowBluetoothAdapter.setIsLeExtendedAdvertisingSupported(false); AdvertiseData oversizedData = new AdvertiseData.Builder() .addServiceUuid(ParcelUuid.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")) @@ -660,24 +698,24 @@ public class ShadowBluetoothLeAdvertiserTest { () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + false, true, false, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), oversizedData, /* scanResponse= */ null, /* periodicParameters= */ null, /* periodicData= */ null, - createAdvertisingSetCallback())); + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_nonLegacyModeWithTooBigScanResponse_throwsIllegalArgumentException() { - shadowOf(BluetoothAdapter.getDefaultAdapter()).setIsLeExtendedAdvertisingSupported(false); + shadowBluetoothAdapter.setIsLeExtendedAdvertisingSupported(false); AdvertiseData oversizedData = new AdvertiseData.Builder() .addServiceUuid(ParcelUuid.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")) @@ -689,24 +727,24 @@ public class ShadowBluetoothLeAdvertiserTest { () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + false, true, false, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, oversizedData, /* periodicParameters= */ null, /* periodicData= */ null, - createAdvertisingSetCallback())); + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_nonLegacyModeWithTooBigPeriodicData_throwsIllegalArgumentException() { - shadowOf(BluetoothAdapter.getDefaultAdapter()).setIsLeExtendedAdvertisingSupported(false); + shadowBluetoothAdapter.setIsLeExtendedAdvertisingSupported(false); AdvertiseData oversizedData = new AdvertiseData.Builder() .addServiceUuid(ParcelUuid.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")) @@ -718,21 +756,21 @@ public class ShadowBluetoothLeAdvertiserTest { () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + false, true, false, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, /* scanResponse= */ null, new PeriodicAdvertisingParameters.Builder().setInterval(1).build(), oversizedData, - createAdvertisingSetCallback())); + /* duration= */ 0, + /* maxExtendedAdvertisingEvents= */ 0, + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_maxExtendedAdvertisingEventsOutOfRange_throwsIllegalArgumentException() { assertThrows( @@ -740,73 +778,65 @@ public class ShadowBluetoothLeAdvertiserTest { () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + false, true, false, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, /* scanResponse= */ null, /* periodicParameters= */ null, /* periodicData= */ null, /* duration= */ 0, - /* maxExtendedAdvertisingEvents= */ -1, - createAdvertisingSetCallback(), + -1, + gattServer, + advertisingSetCallback, mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void startAdvertisingSet_durationOutOfRange_throwsIllegalArgumentException() { assertThrows( IllegalArgumentException.class, () -> bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ false, - /* isConnectable= */ true, - /* isScannable= */ false, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + false, true, false, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, /* scanResponse= */ null, /* periodicParameters= */ null, /* periodicData= */ null, - /* duration= */ -1, + -1, /* maxExtendedAdvertisingEvents= */ 0, - createAdvertisingSetCallback())); + gattServer, + advertisingSetCallback, + mainLooperHandler)); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void stopAdvertisingSet() { - AdvertisingStatus status = new AdvertisingStatus(); - AdvertisingSetCallback callback = createAdvertisingSetCallback(status); bluetoothLeAdvertiser.startAdvertisingSet( buildAdvertisingSetParams( - /* isLegacy= */ true, - /* isConnectable= */ true, - /* isScannable= */ true, - BluetoothDevice.PHY_LE_CODED, - BluetoothDevice.PHY_LE_CODED), + true, true, true, BluetoothDevice.PHY_LE_CODED, BluetoothDevice.PHY_LE_CODED), advertiseData1, - /* scanResponse= */ null, - /* periodicParameters= */ null, - /* periodicData= */ null, - callback); + null, + null, + null, + 0, + 0, + gattServer, + advertisingSetCallback, + mainLooperHandler); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(1); - assertThat(status.isStarted).isTrue(); - bluetoothLeAdvertiser.stopAdvertisingSet(callback); + bluetoothLeAdvertiser.stopAdvertisingSet(advertisingSetCallback); - assertThat(status.isStarted).isFalse(); + assertThat(advertisingSetStopped).isTrue(); assertThat(shadowOf(bluetoothLeAdvertiser).getAdvertisingSetRequestCount()).isEqualTo(0); } @Test - @Config(minSdk = O) + @Config(minSdk = UPSIDE_DOWN_CAKE) public void stopAdvertisingSet_nullCallback_throwsIllegalArgumentException() { assertThrows( IllegalArgumentException.class, () -> bluetoothLeAdvertiser.stopAdvertisingSet(null)); @@ -826,51 +856,4 @@ public class ShadowBluetoothLeAdvertiserTest { .setSecondaryPhy(secondaryPhy) .build(); } - - private AdvertisingSetCallback createAdvertisingSetCallback() { - return createAdvertisingSetCallback(new AdvertisingStatus()); - } - - private AdvertisingSetCallback createAdvertisingSetCallback(AdvertisingStatus advertisingStatus) { - return new AdvertisingSetCallback() { - @Override - public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) { - advertisingStatus.setStatusCode(status); - if (status == AdvertisingSetCallback.ADVERTISE_SUCCESS) { - advertisingStatus.setStarted(true); - } - } - - @Override - public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) { - advertisingStatus.setStarted(false); - } - }; - } - - static class AdvertisingStatus { - private Optional<Integer> statusCode; - private boolean isStarted; - - AdvertisingStatus() { - isStarted = false; - statusCode = Optional.empty(); - } - - public void setStatusCode(int statusCode) { - this.statusCode = Optional.of(statusCode); - } - - public void setStarted(boolean started) { - this.isStarted = started; - } - - public int getStatusCode() { - return statusCode.get(); - } - - public boolean isStarted() { - return isStarted; - } - } } diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiser.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiser.java index ae8d16d46..c4e614dd5 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiser.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiser.java @@ -1,6 +1,5 @@ package org.robolectric.shadows; -import static android.os.Build.VERSION_CODES.LOLLIPOP; import static android.os.Build.VERSION_CODES.O; import static android.os.Build.VERSION_CODES.R; import static android.os.Build.VERSION_CODES.S; @@ -20,10 +19,7 @@ import android.bluetooth.le.AdvertisingSetParameters; import android.bluetooth.le.BluetoothLeAdvertiser; import android.bluetooth.le.PeriodicAdvertisingParameters; import android.content.AttributionSource; -import android.os.Build; -import android.os.Build.VERSION_CODES; import android.os.Handler; -import android.os.Looper; import android.os.ParcelUuid; import java.util.HashMap; import java.util.HashSet; @@ -40,7 +36,7 @@ import org.robolectric.util.reflector.Direct; import org.robolectric.util.reflector.ForType; /** Shadow implementation of {@link BluetoothLeAdvertiser}. */ -@Implements(value = BluetoothLeAdvertiser.class, minSdk = LOLLIPOP) +@Implements(value = BluetoothLeAdvertiser.class, minSdk = O) public class ShadowBluetoothLeAdvertiser { private static final String CALLBACK_NULL_MESSAGE = "callback cannot be null."; @@ -139,116 +135,23 @@ public class ShadowBluetoothLeAdvertiser { } /** - * Creates a new advertising set. If operation succeed, device will start advertising. This method - * returns immediately, the operation status is delivered through {@code - * callback.onAdvertisingSetStarted()} on the caller thread. - */ - @Implementation(minSdk = O) - protected void startAdvertisingSet( - AdvertisingSetParameters parameters, - AdvertiseData advertiseData, - AdvertiseData scanResponse, - PeriodicAdvertisingParameters periodicParameters, - AdvertiseData periodicData, - AdvertisingSetCallback callback) { - startAdvertisingSet( - parameters, - advertiseData, - scanResponse, - periodicParameters, - periodicData, - /* duration= */ 0, - /* maxExtendedAdvertisingEvents= */ 0, - callback, - new Handler(Looper.getMainLooper())); - } - - /** - * Creates a new advertising set. If operation succeed, device will start advertising. This method - * returns immediately, the operation status is delivered through {@code - * callback.onAdvertisingSetStarted()} on the caller thread instead of the {@code handler}. - */ - @Implementation(minSdk = O) - protected void startAdvertisingSet( - AdvertisingSetParameters parameters, - AdvertiseData advertiseData, - AdvertiseData scanResponse, - PeriodicAdvertisingParameters periodicParameters, - AdvertiseData periodicData, - AdvertisingSetCallback callback, - Handler handler) { - startAdvertisingSet( - parameters, - advertiseData, - scanResponse, - periodicParameters, - periodicData, - /* duration= */ 0, - /* maxExtendedAdvertisingEvents= */ 0, - callback, - handler); - } - - /** - * Creates a new advertising set. If operation succeed, device will start advertising. This method - * returns immediately, the operation status is delivered through {@code - * callback.onAdvertisingSetStarted()} on the caller thread. - */ - @Implementation(minSdk = O) - protected void startAdvertisingSet( - AdvertisingSetParameters parameters, - AdvertiseData advertiseData, - AdvertiseData scanResponse, - PeriodicAdvertisingParameters periodicParameters, - AdvertiseData periodicData, - int duration, - int maxExtendedAdvertisingEvents, - AdvertisingSetCallback callback) { - startAdvertisingSet( - parameters, - advertiseData, - scanResponse, - periodicParameters, - periodicData, - duration, - maxExtendedAdvertisingEvents, - callback, - new Handler(Looper.getMainLooper())); - } - - /** - * Creates a new advertising set. If operation succeed, device will start advertising. This method - * returns immediately, the operation status is delivered through {@code - * callback.onAdvertisingSetStarted()} on the caller thread instead of the {@code handler}. - */ - @Implementation(minSdk = O) - protected void startAdvertisingSet( - AdvertisingSetParameters parameters, - AdvertiseData advertiseData, - AdvertiseData scanResponse, - PeriodicAdvertisingParameters periodicParameters, - AdvertiseData periodicData, - int duration, - int maxExtendedAdvertisingEvents, - AdvertisingSetCallback callback, - Handler handler) { - startAdvertisingSet( - parameters, - advertiseData, - scanResponse, - periodicParameters, - periodicData, - duration, - maxExtendedAdvertisingEvents, - /* gattServer= */ null, - callback, - handler); - } - - /** - * Creates a new advertising set. If operation succeed, device will start advertising. This method - * returns immediately, the operation status is delivered through {@code - * callback.onAdvertisingSetStarted()} on the caller thread instead of the {@code handler}. + * Start Bluetooth LE Advertising Set. This method returns immediately, the operation status is + * delivered through {@code callback}. + * + * @param parameters Advertising set parameters. + * @param advertiseData Advertisement data to be broadcasted. + * @param scanResponse Scan response associated with the advertisement data. + * @param periodicParameters Periodic advertisng parameters. + * @param periodicData Periodic advertising data. + * @param duration Advertising duration, in 10ms unit. + * @param maxExtendedAdvertisingEvents Maximum number of extended advertising events the + * controller shall attempt to send prior to terminating the extended advertising, even if the + * duration has not expired. + * @param gattServer GattServer the GATT server that will "own" connections derived from this + * advertising. + * @param callback Callback for advertising set. + * @param handler Thread upon which the callbacks will be invoked. + * @throws IllegalArgumentException When {@code callback} is not present. */ @Implementation(minSdk = UPSIDE_DOWN_CAKE) protected void startAdvertisingSet( @@ -267,10 +170,7 @@ public class ShadowBluetoothLeAdvertiser { } boolean isConnectable = parameters.isConnectable(); - boolean isDiscoverable = true; - if (Build.VERSION.SDK_INT >= UPSIDE_DOWN_CAKE) { - isDiscoverable = parameters.isDiscoverable(); - } + boolean isDiscoverable = parameters.isDiscoverable(); boolean hasFlags = isConnectable && isDiscoverable; if (parameters.isLegacy()) { if (getTotalBytes(advertiseData, hasFlags) > MAX_LEGACY_ADVERTISING_DATA_BYTES) { @@ -331,27 +231,16 @@ public class ShadowBluetoothLeAdvertiser { return; } - AdvertisingSet advertisingSet; - if (Build.VERSION.SDK_INT >= VERSION_CODES.S) { - advertisingSet = - ReflectionHelpers.callConstructor( - AdvertisingSet.class, - ClassParameter.from(int.class, advertiserId.getAndAdd(1)), - ClassParameter.from( - IBluetoothManager.class, - ReflectionHelpers.createNullProxy(IBluetoothManager.class)), - ClassParameter.from( - AttributionSource.class, - ReflectionHelpers.callInstanceMethod(bluetoothAdapter, "getAttributionSource"))); - } else { - advertisingSet = - ReflectionHelpers.callConstructor( - AdvertisingSet.class, - ClassParameter.from(int.class, advertiserId.getAndAdd(1)), - ClassParameter.from( - IBluetoothManager.class, - ReflectionHelpers.createNullProxy(IBluetoothManager.class))); - } + AdvertisingSet advertisingSet = + ReflectionHelpers.callConstructor( + AdvertisingSet.class, + ClassParameter.from(int.class, advertiserId.getAndAdd(1)), + ClassParameter.from( + IBluetoothManager.class, + ReflectionHelpers.createNullProxy(IBluetoothManager.class)), + ClassParameter.from( + AttributionSource.class, + ReflectionHelpers.callInstanceMethod(bluetoothAdapter, "getAttributionSource"))); callback.onAdvertisingSetStarted( advertisingSet, parameters.getTxPowerLevel(), AdvertisingSetCallback.ADVERTISE_SUCCESS); @@ -366,7 +255,7 @@ public class ShadowBluetoothLeAdvertiser { * @param callback Callback for advertising set. * @throws IllegalArgumentException When {@code callback} is not present. */ - @Implementation(minSdk = O) + @Implementation(minSdk = UPSIDE_DOWN_CAKE) protected void stopAdvertisingSet(AdvertisingSetCallback callback) { if (callback == null) { throw new IllegalArgumentException("callback cannot be null"); |