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 test script exercises different opportunistic scan scenarios.
18It is expected that the second AndroidDevice is able to advertise.
19
20This test script was designed with this setup in mind:
21Shield box one: Android Device, Android Device
22"""
23
24from queue import Empty
25
26from acts import utils
27from acts.test_decorators import test_tracker_info
28from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
29from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
30from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
31from acts.test_utils.bt.bt_test_utils import batch_scan_result
32from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers
33from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
34from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
35from acts.test_utils.bt.bt_test_utils import reset_bluetooth
36from acts.test_utils.bt.bt_constants import scan_result
37
38
39class BleOpportunisticScanTest(BluetoothBaseTest):
40    default_timeout = 10
41    max_scan_instances = 27
42    report_delay = 2000
43    scan_callbacks = []
44    adv_callbacks = []
45    active_scan_callback_list = []
46    active_adv_callback_list = []
47
48    def __init__(self, controllers):
49        BluetoothBaseTest.__init__(self, controllers)
50        self.scn_ad = self.android_devices[0]
51        self.adv_ad = self.android_devices[1]
52
53    def setup_class(self):
54        super(BluetoothBaseTest, self).setup_class()
55        utils.set_location_service(self.scn_ad, True)
56        utils.set_location_service(self.adv_ad, True)
57        return True
58
59    def teardown_test(self):
60        cleanup_scanners_and_advertisers(
61            self.scn_ad, self.active_scan_callback_list, self.adv_ad,
62            self.active_adv_callback_list)
63        self.active_adv_callback_list = []
64        self.active_scan_callback_list = []
65
66    def on_exception(self, test_name, begin_time):
67        reset_bluetooth(self.android_devices)
68
69    def _setup_generic_advertisement(self):
70        adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
71            self.adv_ad.droid)
72        self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data,
73                                                 adv_settings)
74        self.active_adv_callback_list.append(adv_callback)
75
76    def _verify_no_events_found(self, event_name):
77        try:
78            event = self.scn_ad.ed.pop_event(event_name, self.default_timeout)
79            self.log.error("Found an event when none was expected: {}".format(
80                event))
81            return False
82        except Empty:
83            self.log.info("No scan result found as expected.")
84            return True
85
86    @BluetoothBaseTest.bt_test_wrap
87    @test_tracker_info(uuid='6bccfbea-3734-4504-8ea9-3511ad17a3e0')
88    def test_scan_result_no_advertisement(self):
89        """Test opportunistic scan with no advertisement.
90
91        Tests opportunistic scan where there are no advertisements. This should
92        not find any onScanResults.
93
94        Steps:
95        1. Initialize scanner with scan mode set to opportunistic mode.
96        2. Start scanning on dut 0
97        3. Pop onScanResults event on the scanner
98
99        Expected Result:
100        Find no advertisements with the opportunistic scan instance.
101
102        Returns:
103          Pass if True
104          Fail if False
105
106        TAGS: LE, Advertising, Scanning, Opportunistic Scan
107        Priority: 1
108        """
109        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
110            'opportunistic'])
111        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
112            self.scn_ad.droid)
113        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
114                                          scan_callback)
115        self.active_scan_callback_list.append(scan_callback)
116        if not self._verify_no_events_found(scan_result.format(scan_callback)):
117            return False
118        self.scn_ad.droid.bleStopBleScan(scan_callback)
119        return True
120
121    @BluetoothBaseTest.bt_test_wrap
122    @test_tracker_info(uuid='8430bc57-925c-4b70-a62e-cd34df264ca1')
123    def test_batch_scan_result_no_advertisement(self):
124        """Test batch opportunistic scan without an advertisement.
125
126        Tests opportunistic scan where there are no advertisements. This should
127        not find any onBatchScanResult.
128
129        Steps:
130        1. Initialize scanner with scan mode set to opportunistic mode.
131        2. Set report delay seconds such that onBatchScanResult events are
132        expected
133        2. Start scanning on dut 0
134        3. Pop onBatchScanResult event on the scanner
135
136        Expected Result:
137        Find no advertisements with the opportunistic scan instance.
138
139        Returns:
140          Pass if True
141          Fail if False
142
143        TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
144        Priority: 1
145        """
146        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
147            'opportunistic'])
148        self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
149            self.report_delay)
150        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
151            self.scn_ad.droid)
152        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
153                                          scan_callback)
154        self.active_scan_callback_list.append(scan_callback)
155        if not self._verify_no_events_found(
156                batch_scan_result.format(scan_callback)):
157            return False
158        self.scn_ad.droid.bleStopBleScan(scan_callback)
159        return True
160
161    @BluetoothBaseTest.bt_test_wrap
162    @test_tracker_info(uuid='4613cb67-0f54-494e-8a56-2e8ce56fad41')
163    def test_scan_result(self):
164        """Test opportunistic scan with an advertisement.
165
166        Tests opportunistic scan where it will only report scan results when
167        other registered scanners find results.
168
169        Steps:
170        1. Initialize advertiser and start advertisement on dut1
171        2. Initialize scanner with scan mode set to opportunistic mode on dut0
172        and start scanning
173        3. Try to find an event, expect none.
174        4. Start a second scanner on dut0, with any other mode set
175        5. Pop onScanResults event on the second scanner
176        6. Pop onScanResults event on the first scanner
177
178        Expected Result:
179        Scan result is found on the opportunistic scan instance.
180
181        Returns:
182          Pass if True
183          Fail if False
184
185        TAGS: LE, Advertising, Scanning, Opportunistic Scan
186        Priority: 1
187        """
188        self._setup_generic_advertisement()
189        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
190            'opportunistic'])
191        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
192            self.scn_ad.droid)
193
194        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
195                                          scan_callback)
196        self.active_scan_callback_list.append(scan_callback)
197        if not self._verify_no_events_found(scan_result.format(scan_callback)):
198            return False
199        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
200            'low_latency'])
201        filter_list2, scan_settings2, scan_callback2 = (
202            generate_ble_scan_objects(self.scn_ad.droid))
203        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
204                                          scan_callback2)
205        self.active_scan_callback_list.append(scan_callback2)
206        try:
207            self.scn_ad.ed.pop_event(
208                scan_result.format(scan_callback2), self.default_timeout)
209        except Empty:
210            self.log.error("Non-Opportunistic scan found no scan results.")
211            return False
212        try:
213            self.scn_ad.ed.pop_event(
214                scan_result.format(scan_callback), self.default_timeout)
215        except Empty:
216            self.log.error("Opportunistic scan found no scan results.")
217            return False
218        return True
219
220    @BluetoothBaseTest.bt_test_wrap
221    @test_tracker_info(uuid='5b46fefc-70ef-48a0-acf4-35077cd72202')
222    def test_batch_scan_result(self):
223        """Test batch opportunistic scan with advertisement.
224
225        Tests opportunistic scan where it will only report scan results when
226        other registered scanners find results. Set the report delay millis such
227        that an onBatchScanResult is expected.
228
229        Steps:
230        1. Initialize advertiser and start advertisement on dut1
231        2. Initialize scanner with scan mode set to opportunistic mode and
232        set scan settings report delay seconds such that a batch scan is
233        expected
234        3. Start scanning on dut 0
235        4. Try to find an event, expect none.
236        5. Start a second scanner on dut0, with any other mode set and set scan
237        settings report delay millis such that an onBatchScanResult is expected
238        6. Pop onBatchScanResult event on the second scanner
239        7. Pop onBatchScanResult event on the first scanner
240
241        Expected Result:
242        Find a batch scan result on both opportunistic scan instances.
243
244        Returns:
245          Pass if True
246          Fail if False
247
248        TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
249        Priority: 1
250        """
251        self._setup_generic_advertisement()
252        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
253            'opportunistic'])
254        self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
255            self.report_delay)
256        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
257            self.scn_ad.droid)
258        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
259                                          scan_callback)
260        self.active_scan_callback_list.append(scan_callback)
261        if not self._verify_no_events_found(
262                batch_scan_result.format(scan_callback)):
263            return False
264        self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
265            self.report_delay)
266        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
267            'low_latency'])
268        filter_list2, scan_settings2, scan_callback2 = generate_ble_scan_objects(
269            self.scn_ad.droid)
270        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
271                                          scan_callback2)
272        self.active_scan_callback_list.append(scan_callback2)
273        try:
274            self.scn_ad.ed.pop_event(
275                batch_scan_result.format(scan_callback2), self.default_timeout)
276        except Empty:
277            self.log.error("Non-Opportunistic scan found no scan results.")
278            return False
279        try:
280            self.scn_ad.ed.pop_event(
281                batch_scan_result.format(scan_callback), self.default_timeout)
282        except Empty:
283            self.log.error("Opportunistic scan found no scan results.")
284            return False
285        return True
286
287    @BluetoothBaseTest.bt_test_wrap
288    @test_tracker_info(uuid='fd85d95e-dc8c-48c1-8d8a-83c3475755ff')
289    def test_batch_scan_result_not_expected(self):
290        """Test opportunistic batch scan without expecting an event.
291
292        Tests opportunistic scan where it will only report scan results when
293        other registered scanners find results. Set the report delay millis such
294        that a batch scan is not expected.
295
296        Steps:
297        1. Initialize advertiser and start advertisement on dut1
298        2. Initialize scanner with scan mode set to opportunistic mode and
299        set scan settings report delay seconds such that a batch scan is
300        expected.
301        3. Start scanning on dut 0
302        4. Try to find an event, expect none.
303        5. Start a second scanner on dut0, with any other mode set and set scan
304        settings report delay millis to 0 such that an onBatchScanResult is not
305        expected.
306        6. Pop onScanResults event on the second scanner
307        7. Pop onBatchScanResult event on the first scanner
308
309        Expected Result:
310        Batch scan result is not expected on opportunistic scan instance.
311
312        Returns:
313          Pass if True
314          Fail if False
315
316        TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
317        Priority: 1
318        """
319        self._setup_generic_advertisement()
320        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
321            'opportunistic'])
322        self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
323            self.report_delay)
324        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
325            self.scn_ad.droid)
326        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
327                                          scan_callback)
328        self.active_scan_callback_list.append(scan_callback)
329        if not self._verify_no_events_found(
330                batch_scan_result.format(scan_callback)):
331
332            return False
333        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
334            'low_latency'])
335        filter_list2, scan_settings2, scan_callback2 = (
336            generate_ble_scan_objects(self.scn_ad.droid))
337        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
338                                          scan_callback2)
339        self.active_scan_callback_list.append(scan_callback2)
340        try:
341            self.scn_ad.ed.pop_event(
342                scan_result.format(scan_callback2), self.default_timeout)
343        except Empty:
344            self.log.error("Non-Opportunistic scan found no scan results.")
345            return False
346        return self._verify_no_events_found(
347            batch_scan_result.format(scan_callback))
348
349    @BluetoothBaseTest.bt_test_wrap
350    @test_tracker_info(uuid='6138592e-8fd5-444f-9a7c-25cd9695644a')
351    def test_scan_result_not_expected(self):
352        """Test opportunistic scan without expecting an event.
353
354        Tests opportunistic scan where it will only report batch scan results
355        when other registered scanners find results.
356
357        Steps:
358        1. Initialize advertiser and start advertisement on dut1
359        2. Initialize scanner with scan mode set to opportunistic mode.
360        3. Start scanning on dut 0
361        4. Try to find an event, expect none.
362        5. Start a second scanner on dut0, with any other mode set and set scan
363        settings
364        report delay millis such that an onBatchScanResult is expected
365        6. Pop onBatchScanResult event on the second scanner
366        7. Pop onScanResults event on the first scanner
367
368        Expected Result:
369        Scan result is not expected on opportunistic scan instance.
370
371        Returns:
372          Pass if True
373          Fail if False
374
375        TAGS: LE, Advertising, Scanning, Opportunistic Scan
376        Priority: 1
377        """
378        self._setup_generic_advertisement()
379        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
380            'opportunistic'])
381        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
382            self.scn_ad.droid)
383        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
384                                          scan_callback)
385        self.active_scan_callback_list.append(scan_callback)
386        if not self._verify_no_events_found(scan_result.format(scan_callback)):
387            return False
388        self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
389            self.report_delay)
390        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
391            'low_latency'])
392        filter_list2, scan_settings2, scan_callback2 = (
393            generate_ble_scan_objects(self.scn_ad.droid))
394        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
395                                          scan_callback2)
396        self.active_scan_callback_list.append(scan_callback2)
397        try:
398            self.scn_ad.ed.pop_event(
399                batch_scan_result.format(scan_callback2), self.default_timeout)
400        except Empty:
401            self.log.error("Non-Opportunistic scan found no scan results.")
402            return False
403        return self._verify_no_events_found(scan_result.format(scan_callback))
404
405    @BluetoothBaseTest.bt_test_wrap
406    @test_tracker_info(uuid='f7aba3d9-d3f7-4b2f-976e-441772705613')
407    def test_max_opportunistic_scan_instances(self):
408        """Test max number of opportunistic scan instances.
409
410        Tests max instances of opportunistic scans. Each instances should
411        find an onScanResults event.
412
413        Steps:
414        1. Initialize advertiser and start advertisement on dut1
415        2. Set scan settings to opportunistic scan on dut0 scan instance
416        3. Start scan scan from step 2
417        4. Repeat step two and three until there are max_scan_instances-1 scan
418        instances
419        5. Start a regular ble scan on dut0 with the last available scan
420        instance
421        6. Pop onScanResults event on all scan instances
422
423        Expected Result:
424        Each opportunistic scan instance finds a advertisement.
425
426        Returns:
427          Pass if True
428          Fail if False
429
430        TAGS: LE, Advertising, Scanning, Opportunistic Scan
431        Priority: 1
432        """
433        self._setup_generic_advertisement()
434        for _ in range(self.max_scan_instances - 1):
435            self.scn_ad.droid.bleSetScanSettingsScanMode(
436                ble_scan_settings_modes['opportunistic'])
437            filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
438                self.scn_ad.droid)
439            self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
440                                              scan_callback)
441            self.active_scan_callback_list.append(scan_callback)
442
443        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
444            'low_latency'])
445        filter_list2, scan_settings2, scan_callback2 = (
446            generate_ble_scan_objects(self.scn_ad.droid))
447        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
448                                          scan_callback2)
449        self.active_scan_callback_list.append(scan_callback2)
450
451        for callback in self.active_scan_callback_list:
452            try:
453                self.scn_ad.ed.pop_event(
454                    scan_result.format(callback), self.default_timeout)
455            except Empty:
456                self.log.error("No scan results found for callback {}".format(
457                    callback))
458                return False
459        return True
460
461    @BluetoothBaseTest.bt_test_wrap
462    @test_tracker_info(uuid='cf971f08-4d92-4046-bba6-b86a75aa773c')
463    def test_max_opportunistic_batch_scan_instances(self):
464        """Test max opportunistic batch scan instances.
465
466        Tests max instances of opportunistic batch scans. Each instances should
467        find an onBatchScanResult event.
468
469        Steps:
470        1. Initialize advertiser and start advertisement on dut1
471        2. Set scan settings to opportunistic scan on dut0 scan instance and
472        set report delay seconds such that an onBatchScanResult is expected
473        3. Start scan scan from step 2
474        4. Repeat step two and three until there are max_scan_instances-1 scan
475        instances
476        5. Start a regular ble scan on dut0 with the last available scan
477        instance
478        6. Pop onBatchScanResult event on all scan instances
479
480        Expected Result:
481        Each opportunistic scan instance finds an advertisement.
482
483        Returns:
484          Pass if True
485          Fail if False
486
487        TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
488        Priority: 1
489        """
490        self._setup_generic_advertisement()
491        for _ in range(self.max_scan_instances - 1):
492            self.scn_ad.droid.bleSetScanSettingsScanMode(
493                ble_scan_settings_modes['opportunistic'])
494            self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
495                self.report_delay)
496            filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
497                self.scn_ad.droid)
498            self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
499                                              scan_callback)
500            self.active_scan_callback_list.append(scan_callback)
501
502        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
503            'low_latency'])
504        self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
505            self.report_delay)
506        filter_list2, scan_settings2, scan_callback2 = (
507            generate_ble_scan_objects(self.scn_ad.droid))
508        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
509                                          scan_callback2)
510        self.active_scan_callback_list.append(scan_callback2)
511
512        for callback in self.active_scan_callback_list:
513            try:
514                self.scn_ad.ed.pop_event(
515                    batch_scan_result.format(callback), self.default_timeout)
516            except Empty:
517                self.log.error("No scan results found for callback {}".format(
518                    callback))
519        return True
520
521    @BluetoothBaseTest.bt_test_wrap
522    @test_tracker_info(uuid='965d84ef-11a7-418a-97e9-2a441c6de776')
523    def test_discover_opportunistic_scan_result_off_secondary_scan_filter(
524            self):
525        """Test opportunistic scan result from secondary scan filter.
526
527        Tests opportunistic scan where the secondary scan instance does not find
528        an advertisement but the scan instance with scan mode set to
529        opportunistic scan will find an advertisement.
530
531        Steps:
532        1. Initialize advertiser and start advertisement on dut1 (make sure the
533        advertisement is not advertising the device name)
534        2. Set scan settings to opportunistic scan on dut0 scan instance
535        3. Start scan scan from step 2
536        4. Try to find an event, expect none
537        5. Start a second scanner on dut0, with any other mode set and set the
538        scan filter device name to "opp_test"
539        6. Pop onScanResults from the second scanner
540        7. Expect no events
541        8. Pop onScanResults from the first scanner
542
543        Expected Result:
544        Opportunistic scan instance finds an advertisement.
545
546        Returns:
547          Pass if True
548          Fail if False
549
550        TAGS: LE, Advertising, Scanning, Opportunistic Scan
551        Priority: 1
552        """
553        self._setup_generic_advertisement()
554        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
555            'opportunistic'])
556        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
557            self.scn_ad.droid)
558
559        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
560                                          scan_callback)
561        self.active_scan_callback_list.append(scan_callback)
562        if not self._verify_no_events_found(scan_result.format(scan_callback)):
563            return False
564        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
565            'low_latency'])
566        self.scn_ad.droid.bleSetScanFilterDeviceName("opp_test")
567        filter_list2, scan_settings2, scan_callback2 = (
568            generate_ble_scan_objects(self.scn_ad.droid))
569        self.scn_ad.droid.bleBuildScanFilter(filter_list2)
570        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
571                                          scan_callback2)
572        self.active_scan_callback_list.append(scan_callback2)
573        if not self._verify_no_events_found(
574                scan_result.format(scan_callback2)):
575            return False
576        try:
577            self.scn_ad.ed.pop_event(
578                scan_result.format(scan_callback), self.default_timeout)
579        except Empty:
580            self.log.error("Opportunistic scan found no scan results.")
581            return False
582        return True
583
584    @BluetoothBaseTest.bt_test_wrap
585    @test_tracker_info(uuid='13b0a83f-e96e-4d64-84ef-66351ec5054c')
586    def test_negative_opportunistic_scan_filter_result_off_secondary_scan_result(
587            self):
588        """Test opportunistic scan not found scenario.
589
590        Tests opportunistic scan where the secondary scan instance does find an
591        advertisement but the scan instance with scan mode set to opportunistic
592        scan does not find an advertisement due to mismatched scan filters.
593
594        Steps:
595        1. Initialize advertiser and start advertisement on dut1 (make sure the
596        advertisement is not advertising the device name)
597        2. Set scan settings to opportunistic scan on dut0 scan instance and set
598        the scan filter device name to "opp_test"
599        3. Start scan scan from step 2
600        4. Try to find an event, expect none
601        5. Start a second scanner on dut0, with any other mode set
602        6. Pop onScanResults from the second scanner
603        7. Pop onScanResults from the first scanner
604        8. Expect no events
605
606        Expected Result:
607        Opportunistic scan instance doesn't find any advertisements.
608
609        Returns:
610          Pass if True
611          Fail if False
612
613        TAGS: LE, Advertising, Scanning, Opportunistic Scan
614        Priority: 1
615        """
616        self._setup_generic_advertisement()
617        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
618            'opportunistic'])
619        self.scn_ad.droid.bleSetScanFilterDeviceName("opp_test")
620        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
621            self.scn_ad.droid)
622        self.scn_ad.droid.bleBuildScanFilter(filter_list)
623        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
624                                          scan_callback)
625        self.active_scan_callback_list.append(scan_callback)
626        if not self._verify_no_events_found(scan_result.format(scan_callback)):
627            return False
628        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
629            'low_latency'])
630        filter_list2, scan_settings2, scan_callback2 = (
631            generate_ble_scan_objects(self.scn_ad.droid))
632        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
633                                          scan_callback2)
634        self.active_scan_callback_list.append(scan_callback2)
635        try:
636            self.scn_ad.ed.pop_event(
637                scan_result.format(scan_callback2), self.default_timeout)
638        except Empty:
639            self.log.error("Non-Opportunistic scan found no scan results.")
640            return False
641        return self._verify_no_events_found(scan_result.format(scan_callback))
642
643    @BluetoothBaseTest.bt_test_wrap
644    @test_tracker_info(uuid='087f60b2-f6a1-4919-b4c5-cdf3debcfeff')
645    def test_opportunistic_scan_filter_result_off_secondary_scan_result(self):
646        """Test opportunistic scan from a secondary scan result.
647
648        Tests opportunistic scan where the scan filters are the same between the
649        first scan instance with opportunistic scan set and the second instance
650        with any other mode set.
651
652        Steps:
653        1. Initialize advertiser and start advertisement on dut1
654        2. On dut0, set the scan settings mode to opportunistic scan and set
655        the scan filter device name to the advertiser's device name
656        3. Start scan scan from step 2
657        4. Try to find an event, expect none
658        5. Start a second scanner on dut0, with any other mode set and set the
659        scan filter device name to the advertiser's device name
660        6. Pop onScanResults from the second scanner
661        7. Pop onScanResults from the first scanner
662
663        Expected Result:
664        Opportunistic scan instance finds a advertisement.
665
666        Returns:
667          Pass if True
668          Fail if False
669
670        TAGS: LE, Advertising, Scanning, Opportunistic Scan
671        Priority: 1
672        """
673        self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True)
674        self._setup_generic_advertisement()
675        adv_device_name = self.adv_ad.droid.bluetoothGetLocalName()
676        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
677            'opportunistic'])
678        self.scn_ad.droid.bleSetScanFilterDeviceName(adv_device_name)
679        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
680            self.scn_ad.droid)
681        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
682                                          scan_callback)
683        self.active_scan_callback_list.append(scan_callback)
684        if not self._verify_no_events_found(scan_result.format(scan_callback)):
685            return False
686        self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
687            'low_latency'])
688        filter_list2, scan_settings2, scan_callback2 = (
689            generate_ble_scan_objects(self.scn_ad.droid))
690        self.scn_ad.droid.bleSetScanFilterDeviceName(adv_device_name)
691        self.scn_ad.droid.bleBuildScanFilter(filter_list2)
692        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
693                                          scan_callback2)
694        self.active_scan_callback_list.append(scan_callback2)
695        try:
696            self.scn_ad.ed.pop_event(
697                scan_result.format(scan_callback2), self.default_timeout)
698        except Empty:
699            self.log.error("Opportunistic scan found no scan results.")
700            return False
701        try:
702            self.scn_ad.ed.pop_event(
703                scan_result.format(scan_callback), self.default_timeout)
704        except Empty:
705            self.log.error("Non-Opportunistic scan found no scan results.")
706            return False
707        return True
708