From ecc2ee7bd4e3425bb7b4f4cb9cdd6696a2be674e Mon Sep 17 00:00:00 2001 From: jwang1013 <37950852+jwang1013@users.noreply.github.com> Date: Wed, 12 Feb 2020 14:09:01 -0800 Subject: update btBecomeDiscoverable to handle api level (#129) Android SDK beyond 29 changed the BluetoothAdapter.setScanMode signature to (Integer, Long). change btBecomeDiscoverable to handle different api level. --- .../bundled/bluetooth/BluetoothAdapterSnippet.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/main/java') diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/bluetooth/BluetoothAdapterSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/bluetooth/BluetoothAdapterSnippet.java index 14393be..f2ff677 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/bluetooth/BluetoothAdapterSnippet.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/bluetooth/BluetoothAdapterSnippet.java @@ -22,6 +22,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.os.Build; import android.os.Bundle; import androidx.test.platform.app.InstrumentationRegistry; import com.google.android.mobly.snippet.Snippet; @@ -202,14 +203,26 @@ public class BluetoothAdapterSnippet implements Snippet { throw new BluetoothAdapterSnippetException( "Bluetooth is not enabled, cannot become discoverable."); } - if (!(boolean) + // TODO(jwang1013): change it to SDK version for R after R is released. + if (Build.VERSION.CODENAME.equals("R") || Build.VERSION.SDK_INT > 29) { + if (!(boolean) + Utils.invokeByReflection( + mBluetoothAdapter, + "setScanMode", + BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, + (long) duration * 1000)) { + throw new BluetoothAdapterSnippetException("Failed to become discoverable."); + } else { + if (!(boolean) Utils.invokeByReflection( mBluetoothAdapter, "setScanMode", BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, duration)) { throw new BluetoothAdapterSnippetException("Failed to become discoverable."); + } } + } } @Rpc(description = "Cancel ongoing bluetooth discovery.") -- cgit v1.2.3