1#!/usr/bin/env python3 2# 3# Copyright 2017 Google, Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of 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, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import time 18 19from acts.test_utils.wifi import wifi_power_test_utils as wputils 20from acts.test_utils.bt.bt_test_utils import enable_bluetooth 21from acts.test_utils.bt.bt_test_utils import disable_bluetooth 22 23BT_BASE_UUID = '00000000-0000-1000-8000-00805F9B34FB' 24BT_CLASSICAL_DATA = [1, 2, 3] 25BLE_LOCATION_SCAN_ENABLE = 'settings put global ble_scan_always_enabled 1' 26BLE_LOCATION_SCAN_DISABLE = 'settings put global ble_scan_always_enabled 0' 27START_PMC_CMD = 'am start -n com.android.pmc/com.android.pmc.PMCMainActivity' 28PMC_VERBOSE_CMD = 'setprop log.tag.PMC VERBOSE' 29PMC_BASE_SCAN = 'am broadcast -a com.android.pmc.BLESCAN --es ScanMode ' 30 31 32def phone_setup_for_BT(dut, bt_on, ble_on, screen_status): 33 """Sets the phone and Bluetooth in the desired state 34 35 Args: 36 dut: object of the android device under test 37 bt_on: Enable/Disable BT 38 ble_on: Enable/Disable BLE 39 screen_status: screen ON or OFF 40 """ 41 # Initialize the dut to rock-bottom state 42 wputils.dut_rockbottom(dut) 43 time.sleep(2) 44 45 # Check if we are enabling a background scan 46 # TODO: Turn OFF cellular wihtout having to turn ON airplane mode 47 if bt_on == 'OFF' and ble_on == 'ON': 48 dut.adb.shell(BLE_LOCATION_SCAN_ENABLE) 49 dut.droid.connectivityToggleAirplaneMode(False) 50 time.sleep(2) 51 52 # Turn ON/OFF BT 53 if bt_on == 'ON': 54 enable_bluetooth(dut.droid, dut.ed) 55 dut.log.info('BT is ON') 56 else: 57 disable_bluetooth(dut.droid) 58 dut.droid.bluetoothDisableBLE() 59 dut.log.info('BT is OFF') 60 time.sleep(2) 61 62 # Turn ON/OFF BLE 63 if ble_on == 'ON': 64 dut.droid.bluetoothEnableBLE() 65 dut.log.info('BLE is ON') 66 else: 67 dut.droid.bluetoothDisableBLE() 68 dut.log.info('BLE is OFF') 69 time.sleep(2) 70 71 # Set the desired screen status 72 if screen_status == 'OFF': 73 dut.droid.goToSleepNow() 74 dut.log.info('Screen is OFF') 75 time.sleep(2) 76 77 78def start_pmc_ble_scan(dut, 79 scan_mode, 80 offset_start, 81 scan_time, 82 idle_time=None, 83 num_reps=1): 84 """Starts a generic BLE scan via the PMC app 85 86 Args: 87 dut: object of the android device under test 88 scan mode: desired BLE scan type 89 offset_start: Time delay in seconds before scan starts 90 scan_time: active scan time 91 idle_time: iddle time (i.e., no scans occuring) 92 num_reps: Number of repetions of the ative+idle scan sequence 93 """ 94 scan_dur = scan_time 95 if not idle_time: 96 idle_time = 0.2 * scan_time 97 scan_dur = 0.8 * scan_time 98 99 first_part_msg = '%s%s --es StartTime %d --es ScanTime %d' % ( 100 PMC_BASE_SCAN, scan_mode, offset_start, scan_dur) 101 102 msg = '%s --es NoScanTime %d --es Repetitions %d' % (first_part_msg, 103 idle_time, num_reps) 104 105 dut.log.info('Sent BLE scan broadcast message: %s', msg) 106 dut.adb.shell(msg) 107