1#/usr/bin/env python3.4 2# 3# Copyright (C) 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may not 6# use this file except in compliance with the License. You may obtain a copy of 7# the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations under 15# the License. 16 17import time 18 19from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 20from acts.test_utils.bt.bt_test_utils import * 21 22 23class BtReconnectTest(BluetoothBaseTest): 24 tests = None 25 default_timeout = 10 26 27 def __init__(self, controllers): 28 BluetoothBaseTest.__init__(self, controllers) 29 self.tests = ("test_tool_reconnect", ) 30 31 def setup_class(self): 32 return setup_multiple_devices_for_bt_test(self.android_devices) 33 34 def setup_test(self): 35 return reset_bluetooth(self.android_devices) 36 37 def setup_test(self): 38 setup_result = reset_bluetooth(self.android_devices) 39 self.log.debug(log_energy_info(self.android_devices, "Start")) 40 return setup_result 41 42 def teardown_test(self): 43 self.log.debug(log_energy_info(self.android_devices, "End")) 44 return True 45 46 @BluetoothBaseTest.bt_test_wrap 47 def test_tool_reconnect(self): 48 droid, ed = self.android_devices[0].droid, self.android_devices[0].ed 49 n = 0 50 test_result = True 51 test_result_list = [] 52 sleep_time = input( 53 "Assumption: Android Device is already paired.\nEnter sleep time before toggling bluetooth back on in milliseconds:") 54 sleep_time_ms = int(sleep_time) / 1000 55 iteration_count = input("Enter number of iterations:") 56 while n < int(iteration_count): 57 self.log.info("Test iteration {}.".format(n)) 58 test_result = True 59 self.log.info("Toggling BT state off...") 60 droid.bluetoothToggleState(False) 61 self.log.info("Sleeping {} milliseconds".format(sleep_time)) 62 time.sleep(sleep_time_ms) 63 self.log.info("Toggling BT state on...") 64 droid.bluetoothToggleState(True) 65 start_time = time.time() 66 connected_devices = droid.bluetoothGetConnectedDevices() 67 self.log.info( 68 "Waiting up to 10 seconds for device to reconnect...") 69 while time.time() < start_time + 10 and len( 70 connected_devices) != 1: 71 connected_devices = droid.bluetoothGetConnectedDevices() 72 if len(connected_devices) > 0: 73 break 74 if len(connected_devices) != 1: 75 print( 76 "Failed to reconnect at iteration {}... continuing".format( 77 n)) 78 test_result_list.append(test_result) 79 n += 1 80 if False in test_result_list: 81 return False 82 return test_result 83