1#!/usr/bin/env python3 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""" 17This script shows simple examples of how to get started with bluetooth low energy testing in acts. 18""" 19 20import pprint 21 22from acts.controllers import android_device 23from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 24from acts_contrib.test_utils.bt.bt_constants import adv_succ 25from acts_contrib.test_utils.bt.bt_constants import scan_result 26from acts_contrib.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers 27from acts_contrib.test_utils.bt.bt_test_utils import reset_bluetooth 28 29 30class BleExamplesTest(BluetoothBaseTest): 31 default_timeout = 10 32 active_scan_callback_list = [] 33 active_adv_callback_list = [] 34 scn_droid = None 35 adv_droid = None 36 37 def setup_class(self): 38 super().setup_class() 39 self.scn_droid, self.scn_ed = (self.android_devices[0].droid, 40 self.android_devices[0].ed) 41 self.adv_droid, self.adv_ed = (self.android_devices[1].droid, 42 self.android_devices[1].ed) 43 44 def teardown_test(self): 45 cleanup_scanners_and_advertisers( 46 self.android_devices[0], self.active_adv_callback_list, 47 self.android_devices[1], self.active_adv_callback_list) 48 self.active_adv_callback_list = [] 49 self.active_scan_callback_list = [] 50 51 # An optional function. This overrides the default 52 # on_exception in base_test. If the test throws an 53 # unexpected exception, you can customise it. 54 def on_exception(self, test_name, begin_time): 55 self.log.debug("Test {} failed. Gathering bugreport and btsnoop logs". 56 format(test_name)) 57 android_devices.take_bug_reports(self.android_devices, test_name, 58 begin_time) 59 60 @BluetoothBaseTest.bt_test_wrap 61 def test_bt_toggle(self): 62 """ 63 Test that simply toggle bluetooth 64 :return: 65 """ 66 return reset_bluetooth([self.android_devices[0]]) 67 68 ''' 69 Start: Examples of BLE Scanning 70 ''' 71 72 @BluetoothBaseTest.bt_test_wrap 73 def test_start_ble_scan(self): 74 """Test to demonstrate how to start an LE scan 75 76 Test that shows the steps to start a new ble scan. 77 78 Steps: 79 1. Create a scan filter object. 80 2. Create a scan setting object. 81 3. Create a scan callback object. 82 4. Start an LE scan using the objects created in steps 1-3. 83 5. Find an advertisement with the scanner's event dispatcher. 84 85 Expected Result: 86 A generic advertisement is found. 87 88 Returns: 89 Pass if True 90 Fail if False 91 92 TAGS: LE, Scanning 93 Priority: 4 94 """ 95 filter_list = self.scn_droid.bleGenFilterList() 96 scan_settings = self.scn_droid.bleBuildScanSetting() 97 scan_callback = self.scn_droid.bleGenScanCallback() 98 self.scn_droid.bleStartBleScan(filter_list, scan_settings, 99 scan_callback) 100 self.active_scan_callback_list.append(scan_callback) 101 event_name = scan_result.format(scan_callback) 102 try: 103 event = self.scn_ed.pop_event(event_name, self.default_timeout) 104 self.log.info("Found scan result: {}".format( 105 pprint.pformat(event))) 106 except Exception: 107 self.log.info("Didn't find any scan results.") 108 return True 109 110 ''' 111 End: Examples of BLE Scanning 112 ''' 113 114 @BluetoothBaseTest.bt_test_wrap 115 def test_start_ble_advertise(self): 116 """Test to demonstrate how to start an LE advertisement 117 118 Test that shows the steps to start a new ble scan. 119 120 Steps: 121 1. Create a advertise data object 122 2. Create a advertise settings object. 123 3. Create a advertise callback object. 124 4. Start an LE advertising using the objects created in steps 1-3. 125 5. Find the onSuccess advertisement event. 126 127 Expected Result: 128 Advertisement is successfully advertising. 129 130 Returns: 131 Pass if True 132 Fail if False 133 134 TAGS: LE, Advertising 135 Priority: 4 136 """ 137 advertise_data = self.adv_droid.bleBuildAdvertiseData() 138 advertise_settings = self.adv_droid.bleBuildAdvertiseSettings() 139 advertise_callback = self.adv_droid.bleGenBleAdvertiseCallback() 140 self.adv_droid.bleStartBleAdvertising( 141 advertise_callback, advertise_data, advertise_settings) 142 self.adv_ed.pop_event(adv_succ.format(advertise_callback)) 143 return True 144