1# Copyright (C) 2025 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 time 17from mobly import base_test 18from mobly import test_runner 19from mobly.controllers import android_device 20from utilities.main_utils import common_main 21 22class BluetoothValidationTest(base_test.BaseTestClass): 23 24 def setup_class(self): 25 self.android_devices = self.register_controller(android_device) 26 27 def bluetooth_toggle(self, enable): 28 """Enables or disables the Bluetooth on the device. 29 30 Args: 31 enable (bool): True to enable Bluetooth, False to disable. 32 """ 33 state = 'enable' if enable else 'disable' 34 cmd = f'cmd bluetooth_manager {state}' 35 output = self.android_devices[0].adb.shell(cmd) 36 logging.info(f'Bluetooth {state} command output: {output}') 37 38 def check_bluetooth_status(self, expected_status): 39 """Checks the current Bluetooth status against the expected status. 40 41 Args: 42 expected_status (int): The expected Bluetooth status, 1 for 'enabled' or 0 for 'disabled'. 43 44 Returns: 45 bool: True if the actual and expected statuses match, False otherwise. 46 """ 47 status_output_bytes = self.android_devices[0].adb.shell('settings list global | grep ^bluetooth_on') 48 # The output will be a string like 'bluetooth_on=1' or 'bluetooth_on=0'. 49 status_output = status_output_bytes.decode('utf-8').strip() 50 current_status = status_output.split('=')[-1] 51 logging.info(f'Current Bluetooth status: {status_output}') 52 return str(expected_status) == current_status 53 54 def test_bluetooth_validation(self): 55 # Ensure at least one device is available 56 if not self.android_devices: 57 raise AssertionError('No Android devices are registered.') 58 59 # Check the current status of Bluetooth 60 logging.info("check bluetooth") 61 bluetooth_enabled = self.check_bluetooth_status(1) 62 63 # If Bluetooth is originally enabled, disable it 64 if bluetooth_enabled: 65 logging.info("turn off bluetooth") 66 self.bluetooth_toggle(enable=False) 67 assert not self.check_bluetooth_status(1), 'Failed to disable Bluetooth' 68 69 # Enable Bluetooth and check status 70 logging.info("enable bluetooth") 71 self.bluetooth_toggle(enable=True) 72 time.sleep(2) 73 assert self.check_bluetooth_status(1), 'Failed to enable Bluetooth' 74 75 76if __name__ == '__main__': 77 common_main() 78