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