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 17 """ 18 Test script to test connect and disconnect sequence between two devices which can run 19 SL4A. The script does the following: 20 Setup: 21 Clear up the bonded devices on both bluetooth adapters and bond the DUTs to each other. 22 Test (NUM_TEST_RUNS times): 23 1. Connect A2dpSink and HeadsetClient 24 1.1. Check that devices are connected. 25 2. Disconnect A2dpSink and HeadsetClient 26 2.1 Check that devices are disconnected. 27 """ 28 29 import time 30 31 from acts.base_test import BaseTestClass 32 from acts.test_utils.bt import bt_test_utils 33 from acts import asserts 34 35 class BtCarPairedConnectDisconnectTest(BaseTestClass): 36 def setup_class(self): 37 self.droid_ad = self.android_devices[0] 38 self.droid1_ad = self.android_devices[1] 39 40 def setup_test(self): 41 # Reset the devices in a clean state. 42 bt_test_utils.setup_multiple_devices_for_bt_test(self.android_devices) 43 bt_test_utils.reset_bluetooth(self.android_devices) 44 for a in self.android_devices: 45 a.ed.clear_all_events() 46 47 # Pair the devices. 48 # This call may block until some specified timeout in bt_test_utils.py. 49 result = bt_test_utils.pair_pri_to_sec(self.droid_ad.droid, self.droid1_ad.droid) 50 51 asserts.assert_true(result, "pair_pri_to_sec returned false."); 52 53 # Check for successful setup of test. 54 devices = self.droid_ad.droid.bluetoothGetBondedDevices() 55 asserts.assert_equal(len(devices), 1, "pair_pri_to_sec succeeded but no bonded devices.") 56 57 def on_fail(self, test_name, begin_time): 58 bt_test_utils.take_btsnoop_logs(self.android_devices, self, test_name) 59 60 def test_connect_disconnect_paired(self): 61 NUM_TEST_RUNS = 2 62 failure = 0 63 for i in range(NUM_TEST_RUNS): 64 self.log.info("Running test [" + str(i) + "/" + str(NUM_TEST_RUNS) + "]") 65 # Connect the device. 66 devices = self.droid_ad.droid.bluetoothGetBondedDevices() 67 if (len(devices) == 0): 68 self.log.info("No bonded devices.") 69 failure = failure + 1 70 continue 71 72 self.log.info("Attempting to connect.") 73 self.droid_ad.droid.bluetoothConnectBonded(devices[0]['address']) 74 end_time = time.time() + 20 75 expected_address = self.droid1_ad.droid.bluetoothGetLocalAddress() 76 connected = False 77 a2dp_sink_connected = False 78 pbap_client_connected = False 79 hfp_client_connected = False 80 81 # Busy loop to check if we found a matching device. 82 while time.time() < end_time: 83 connected_devices = self.droid_ad.droid.bluetoothGetConnectedDevices() 84 for d in connected_devices: 85 if d['address'] == expected_address: 86 connected = True 87 break 88 a2dp_sink_connected_devices = (self.droid_ad.droid 89 .bluetoothGetConnectedDevicesOnProfile(11)) 90 for d in a2dp_sink_connected_devices: 91 if d['address'] == expected_address: 92 a2dp_sink_connected = True 93 break 94 95 hfp_client_connected_devices = (self.droid_ad.droid. 96 bluetoothGetConnectedDevicesOnProfile(16)) 97 for d in hfp_client_connected_devices: 98 if d['address'] == expected_address: 99 hfp_client_connected = True 100 break 101 102 pbap_client_connected_devices = (self.droid_ad.droid. 103 bluetoothGetConnectedDevicesOnProfile(17)) 104 for d in hfp_client_connected_devices: 105 if d['address'] == expected_address: 106 pbap_client_connected = True 107 break 108 time.sleep(5) 109 110 self.log.info("Connected " + str(connected)) 111 self.log.info("A2DP Sink Connected " + str(a2dp_sink_connected)) 112 self.log.info("HFP client connected " + str(hfp_client_connected)) 113 self.log.info("PBAP Client connected " + str(pbap_client_connected)) 114 115 if (all([connected, a2dp_sink_connected, 116 hfp_client_connected, pbap_client_connected])): 117 break 118 119 # Try again to overcome occasional throw away by bluetooth 120 self.droid_ad.droid.bluetoothConnectBonded(devices[0]['address']) 121 122 # Check if we got connected. 123 if (not all([connected, a2dp_sink_connected, pbap_client_connected, 124 hfp_client_connected])): 125 self.log.info("Not all profiles connected.") 126 failure = failure + 1 127 continue 128 129 # Disconnect the devices. 130 self.log.info("Attempt to disconnect.") 131 self.droid_ad.droid.bluetoothDisconnectConnected(expected_address) 132 133 end_time = time.time() + 10 134 disconnected = False 135 # Busy loop to check if we have successfully disconnected from the 136 # device 137 while time.time() < end_time: 138 connectedDevices = self.droid_ad.droid.bluetoothGetConnectedDevices() 139 exists = False 140 connected_devices = self.droid_ad.droid.bluetoothGetConnectedDevices() 141 for d in connected_devices: 142 if d['address'] == expected_address: 143 exists = True 144 break 145 if exists is False: 146 disconnected = True 147 break 148 time.sleep(1) 149 150 if disconnected is False: 151 self.log.info("Still connected devices.") 152 failure = failure + 1 153 continue 154 self.log.info("Failure {} total tests {}".format(failure, NUM_TEST_RUNS)) 155 asserts.assert_equal(failure, 0, "") 156