aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKolin Lu <kolinlu@google.com>2023-07-12 10:42:14 -0700
committerKolin Lu <kolinlu@google.com>2023-07-12 10:42:14 -0700
commitc6656ea26538769ab98ab1c2be9ce56822c66aca (patch)
treea8ea9364a01d442de26df85f35c4da3d72b10c73
parentce2fd0fc57ccc2f21aae2cf0841688009340efb6 (diff)
downloadmobly-bundled-snippets-c6656ea26538769ab98ab1c2be9ce56822c66aca.tar.gz
Support for clicking the "OK" button on the watch in btBecomeDiscoverable API
-rw-r--r--src/main/java/com/google/android/mobly/snippet/bundled/bluetooth/BluetoothAdapterSnippet.java21
1 files changed, 17 insertions, 4 deletions
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 c16a2b0..2fd836b 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.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -62,6 +63,7 @@ public class BluetoothAdapterSnippet implements Snippet {
// Default timeout in seconds.
private static final int TIMEOUT_TOGGLE_STATE_SEC = 30;
private final Context mContext;
+ private final PackageManager mPackageManager;
private static final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private final JsonSerializer mJsonSerializer = new JsonSerializer();
private static final ConcurrentHashMap<String, BluetoothDevice> mDiscoveryResults =
@@ -70,6 +72,7 @@ public class BluetoothAdapterSnippet implements Snippet {
public BluetoothAdapterSnippet() {
mContext = InstrumentationRegistry.getInstrumentation().getContext();
+ mPackageManager = mContext.getPackageManager();
}
/**
@@ -240,10 +243,18 @@ public class BluetoothAdapterSnippet implements Snippet {
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
// Triggers the system UI popup to ask for explicit permission.
mContext.startActivity(discoverableIntent);
- // Clicks the "ALLOW" button.
- BySelector allowButtonSelector = By.text(TEXT_PATTERN_ALLOW).clickable(true);
- uiDevice.wait(Until.findObject(allowButtonSelector), 10);
- uiDevice.findObject(allowButtonSelector).click();
+
+ if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)) {
+ // Clicks the "OK" button.
+ BySelector okButtonSelector = By.desc(TEXT_PATTERN_OK).clickable(true);
+ uiDevice.wait(Until.findObject(okButtonSelector), 10);
+ uiDevice.findObject(okButtonSelector).click();
+ } else {
+ // Clicks the "ALLOW" button.
+ BySelector allowButtonSelector = By.text(TEXT_PATTERN_ALLOW).clickable(true);
+ uiDevice.wait(Until.findObject(allowButtonSelector), 10);
+ uiDevice.findObject(allowButtonSelector).click();
+ }
} else if (Build.VERSION.SDK_INT >= 30) {
if (!(boolean)
Utils.invokeByReflection(
@@ -267,6 +278,8 @@ public class BluetoothAdapterSnippet implements Snippet {
private static final Pattern TEXT_PATTERN_ALLOW =
Pattern.compile("allow", Pattern.CASE_INSENSITIVE);
+ private static final Pattern TEXT_PATTERN_OK =
+ Pattern.compile("ok", Pattern.CASE_INSENSITIVE);
@Rpc(description = "Cancel ongoing bluetooth discovery.")
public void btCancelDiscovery() throws BluetoothAdapterSnippetException {