1# Lint as: python3 2""" 3Bluetooth multi devices tests. 4""" 5import sys 6import os.path 7import time 8import re 9 10import logging 11logging.basicConfig(filename="/tmp/bluetooth_multi_devices_test_log.txt", level=logging.INFO) 12 13from mobly import asserts 14from mobly import base_test 15from mobly import test_runner 16from mobly import utils 17from mobly.controllers import android_device 18 19BLUETOOTH_MULTI_DEVICES_SNIPPET_PACKAGE = 'com.google.snippet.bluetooth' 20 21SERVICE_UUID_1 = "0000fffb-0000-1000-8000-00805f9b34fc" 22SERVICE_UUID_2 = "0000fffb-0000-1000-8000-00805f9b34fd" 23 24class BluetoothMultiDevicesTest(base_test.BaseTestClass): 25 26 def setup_class(self): 27 # Declare that two Android devices are needed. 28 self.client, self.server = self.register_controller( 29 android_device, min_number=2) 30 31 def setup_device(device): 32 device.load_snippet('bluetooth_multi_devices_snippet', BLUETOOTH_MULTI_DEVICES_SNIPPET_PACKAGE) 33 34 # Set up devices in parallel to save time. 35 utils.concurrent_exec( 36 setup_device, ((self.client,), (self.server,)), 37 max_workers=2, 38 raise_on_exception=True) 39 40 def setup_test(self): 41 self.server.bluetooth_multi_devices_snippet.disableBluetooth() 42 self.client.bluetooth_multi_devices_snippet.disableBluetooth() 43 44 # TODO(b/266635827) implement callback 45 time.sleep(3) 46 47 asserts.assert_false(self.server.bluetooth_multi_devices_snippet.isBluetoothOn(), 'Server Bluetooth did not stop') 48 asserts.assert_false(self.client.bluetooth_multi_devices_snippet.isBluetoothOn(), 'Client Bluetooth did not stop') 49 50 self.server.bluetooth_multi_devices_snippet.enableBluetooth() 51 self.client.bluetooth_multi_devices_snippet.enableBluetooth() 52 53 # TODO(b/266635827) implement callback 54 time.sleep(3) 55 56 asserts.assert_true(self.server.bluetooth_multi_devices_snippet.isBluetoothOn(), 'Server Bluetooth did not start') 57 asserts.assert_true(self.client.bluetooth_multi_devices_snippet.isBluetoothOn(), 'Client Bluetooth did not start') 58 59 self.server.bluetooth_multi_devices_snippet.reset() 60 self.client.bluetooth_multi_devices_snippet.reset() 61 62 def test_normal_gatt_server(self): 63 """ 64 Tests the android.bluetooth.le.BluetoothLeAdvertiser#startAdvertisingSet API. 65 """ 66 # Start and advertise two regular servers 67 self.server.bluetooth_multi_devices_snippet.createAndAdvertiseServer(SERVICE_UUID_1) 68 self.server.bluetooth_multi_devices_snippet.createAndAdvertiseServer(SERVICE_UUID_2) 69 70 # Connect to the first advertisement 71 asserts.assert_true(self.client.bluetooth_multi_devices_snippet.connectGatt(SERVICE_UUID_1), "Server not discovered") 72 73 # Check the target UUID is present 74 asserts.assert_true(self.client.bluetooth_multi_devices_snippet.containsService(SERVICE_UUID_1), "Service not found") 75 # Check that the UUID from the other server is *also* present 76 asserts.assert_true(self.client.bluetooth_multi_devices_snippet.containsService(SERVICE_UUID_2), "Service not found") 77 78 def test_isolated_gatt_server(self): 79 """ 80 Tests the android.bluetooth.le.BluetoothLeAdvertiser#startAdvertisingSet API. 81 """ 82 # Start a server tied to its advertisement 83 self.server.bluetooth_multi_devices_snippet.createAndAdvertiseIsolatedServer(SERVICE_UUID_1) 84 # Start a second regular server 85 self.server.bluetooth_multi_devices_snippet.createAndAdvertiseServer(SERVICE_UUID_2) 86 # Connect to the first server 87 asserts.assert_true(self.client.bluetooth_multi_devices_snippet.connectGatt(SERVICE_UUID_1), "Server not discovered") 88 89 # Check the target UUID is present 90 asserts.assert_true(self.client.bluetooth_multi_devices_snippet.containsService(SERVICE_UUID_1), "Service not found") 91 # Check that the UUID from the other server is NOT present 92 asserts.assert_false(self.client.bluetooth_multi_devices_snippet.containsService(SERVICE_UUID_2), "Service unexpectedly found") 93 94 95if __name__ == '__main__': 96 # Take test args 97 if '--' in sys.argv: 98 index = sys.argv.index('--') 99 sys.argv = sys.argv[:1] + sys.argv[index + 1:] 100 test_runner.main() 101