1# Copyright (C) 2023 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import logging 16import pprint 17import time 18 19from mbs_utils import constants 20from mobly import asserts 21from mobly.controllers import android_device 22 23# Number of seconds for the target to stay discoverable on Bluetooth. 24DISCOVERABLE_TIME = 60 25TIME_FOR_PROMPT_TO_LOAD = 2 26class BTUtils: 27 """A utility that provides access to Bluetooth connectivity controls.""" 28 29 def __init__(self, discoverer, target): 30 self.discoverer = discoverer 31 self.target = target 32 33 def get_info_from_devices(self, discovered_devices): 34 discovered_names = [device['Name'] for device in discovered_devices] 35 discovered_addresses = [device['Address'] for device in discovered_devices] 36 return discovered_names, discovered_addresses 37 38 def discover_secondary_from_primary(self): 39 target_name = self.target.mbs.btGetName() 40 self.target.log.info('Become discoverable with name "%s" for %ds.', 41 target_name, DISCOVERABLE_TIME) 42 self.target.mbs.btBecomeDiscoverable(DISCOVERABLE_TIME) 43 self.discoverer.log.info('Looking for Bluetooth devices.') 44 discovered_devices = self.discoverer.mbs.btDiscoverAndGetResults() 45 self.discoverer.log.debug('Found Bluetooth devices: %s', 46 pprint.pformat(discovered_devices, indent=2)) 47 discovered_names, _ = self.get_info_from_devices(discovered_devices) 48 logging.info('Verifying the target is discovered by the discoverer.') 49 asserts.assert_true( 50 target_name in discovered_names, 51 'Failed to discover the target device %s over Bluetooth.' % 52 target_name) 53 54 def pair_primary_to_secondary(self): 55 """Enable discovery on the target so the discoverer can find it.""" 56 # Turn bluetooth on in both machines 57 logging.info('Enabling Bluetooth on both devices') 58 self.discoverer.mbs.btEnable() 59 self.target.mbs.btEnable() 60 logging.info('Setting devices to be discoverable') 61 self.target.mbs.btBecomeDiscoverable(DISCOVERABLE_TIME) 62 self.target.mbs.btStartAutoAcceptIncomingPairRequest() 63 target_address = self.target.mbs.btGetAddress() 64 logging.info('Scanning for discoverable devices') 65 # Discovery of target device is tried 5 times. 66 discovered_devices = self.discoverer.mbs.btDiscoverAndGetResults() 67 self.discoverer.mbs.btPairDevice(target_address) 68 logging.info('Allowing time for contacts to sync.') 69 time.sleep(constants.SYNC_WAIT_TIME) 70 paired_devices = self.discoverer.mbs.btGetPairedDevices() 71 _, paired_addresses = self.get_info_from_devices(paired_devices) 72 asserts.assert_true( 73 target_address in paired_addresses, 74 'Failed to pair the target device %s over Bluetooth.' % 75 target_address) 76 77 def press_allow_on_device(self): 78 """ Repeatedly presses "Allow" on prompts until no more prompts appear""" 79 logging.info('Attempting to press ALLOW') 80 while (self.target.mbs.btPressAllow()): 81 logging.info('ALLOW pressed!') 82 time.sleep(TIME_FOR_PROMPT_TO_LOAD) 83 84