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