Funhaus fixes

Add more logging during setup.
Try to connect to the peripheral if
peripheral did not autoconnect.
Run adb shell command to skip setup wizard.

Bug: 31750423
Change-Id: I7485537b30a8ed6f90635461562adfba5f25f995
(cherry picked from commit bd2cedf3d4d0999cde9f03114d428831fb6b5db1)
diff --git a/acts/framework/acts/utils.py b/acts/framework/acts/utils.py
index 1228971..af66d98 100755
--- a/acts/framework/acts/utils.py
+++ b/acts/framework/acts/utils.py
@@ -300,10 +300,8 @@
         OSError is raised if an error occurred during the command execution.
     """
     cmd = ' '.join(cmds)
-    proc = subprocess.Popen(cmd,
-                            stdout=subprocess.PIPE,
-                            stderr=subprocess.PIPE,
-                            shell=True)
+    proc = subprocess.Popen(
+        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
     (out, err) = proc.communicate()
     if not err:
         return out
@@ -653,3 +651,25 @@
     """
     ad.adb.shell("settings put global mobile_data_always_on {}".format(
         1 if new_state else 0))
+
+
+def bypass_setup_wizard(ad):
+    """Bypass the setup wizard on an input Android device
+
+    Args:
+        ad: android device object.
+
+    Returns:
+        True if Andorid device successfully bypassed the setup wizard.
+        False if failed.
+    """
+    ad.adb.shell(
+        "am start -n \"com.google.android.setupwizard/.SetupWizardExitActivity\"")
+    # magical sleep to wait for the gservices override broadcast to complete
+    time.sleep(3)
+    provisioned_state = int(
+        ad.adb.shell("settings get global device_provisioned"))
+    if (provisioned_state != 1):
+        logging.error("Failed to bypass setup wizard.")
+        return False
+    return True
diff --git a/acts/tests/google/bt/audio_lab/BtFunhausTest.py b/acts/tests/google/bt/audio_lab/BtFunhausTest.py
index 61be777..31de4db 100644
--- a/acts/tests/google/bt/audio_lab/BtFunhausTest.py
+++ b/acts/tests/google/bt/audio_lab/BtFunhausTest.py
@@ -19,6 +19,7 @@
 from acts.keys import Config
 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
 from acts.test_utils.bt.bt_test_utils import bluetooth_enabled_check
+from acts.utils import bypass_setup_wizard
 from acts.utils import create_dir
 from acts.utils import exe_cmd
 from acts.utils import sync_device_time
@@ -44,6 +45,9 @@
             sync_device_time(ad)
             # Disable Bluetooth HCI Snoop Logs for audio tests
             ad.droid.bluetoothConfigHciSnoopLog(False)
+            if not bypass_setup_wizard(ad):
+                self.log.debug(
+                    "Failed to bypass setup wizard, continuing test.")
         if not "bt_config" in self.user_params.keys():
             self.log.error("Missing mandatory user config \"bt_config\"!")
             return False
@@ -67,16 +71,25 @@
         except FileNotFoundError:
             self.log.error("File not found: {}.".format(bt_config_map_file))
             return False
+        # Connected devices return all upper case mac addresses.
+        # Make the peripheral_info address attribute upper case
+        # in order to ensure the BT mac addresses match.
+        for serial in bt_config_map.keys():
+            mac_address = bt_config_map[serial]["peripheral_info"][
+                "address"].upper()
+            bt_config_map[serial]["peripheral_info"]["address"] = mac_address
         for ad in self.android_devices:
             serial = ad.serial
 
             # Verify serial number in bt_config_map
+            self.log.info("Verify serial number of Android device in config.")
             if serial not in bt_config_map.keys():
                 self.log.error(
                     "Missing android device serial {} in bt_config.".format(
                         serial))
                 return False
             # Push the bt_config.conf file to Android device
+            self.log.info("Pushing bt_config.conf file to Android device.")
             config_path = bt_config_map[serial]["config_path"]
             if not os.path.isfile(config_path):
                 config_path = os.path.join(
@@ -88,6 +101,7 @@
             ad.adb.push("{} {}".format(config_path, bt_conf_path))
 
             # Add music to the Android device
+            self.log.info("Pushing music to the Android device.")
             music_path_str = "music_path"
             android_music_path = "/sdcard/Music/"
             if music_path_str not in self.user_params:
@@ -107,6 +121,7 @@
             ad.reboot()
 
             # Verify Bluetooth is enabled
+            self.log.info("Verifying Bluetooth is enabled on Android Device.")
             if not bluetooth_enabled_check(ad):
                 self.log.error("Failed to toggle on Bluetooth on device {}".
                                format(serial))
@@ -128,7 +143,18 @@
                     }:
                         result = True
                         break
+                else:
+                    try:
+                        ad.droid.bluetoothConnectBonded(bt_config_map[serial][
+                            "peripheral_info"]["address"])
+                    except Exception as err:
+                        self.log.error(
+                            "Failed to connect bonded. Err: {}".format(err))
             if not result:
+                self.log.info("Connected Devices: {}".format(
+                    connected_devices))
+                self.log.info("Bonded Devices: {}".format(
+                    ad.droid.bluetoothGetBondedDevices()))
                 self.log.error(
                     "Failed to connect to peripheral name: {}, address: {}".
                     format(bt_config_map[serial]["peripheral_info"]["name"],
@@ -197,7 +223,6 @@
 
         sleep_interval = 120
         twelve_hours_in_seconds = 43200
-        twelve_hours_in_seconds = 500
         end_time = time.time() + twelve_hours_in_seconds
         test_result = True
         bluetooth_off_list = []
@@ -261,4 +286,4 @@
             self.log.error("Devices failed to reconnect:\n{}".format(
                 self.device_fails_to_connect_list))
             return False
-        return True
\ No newline at end of file
+        return True