1"""BLE GATT connection test."""
2
3import ble_utils
4from mobly import base_test
5from mobly import test_runner
6from mobly.controllers import android_device
7
8
9class BleBasicTest(base_test.BaseTestClass):
10  """Tests the basic E2E connection flow for BLE."""
11
12  def setup_class(self):
13    self.ads = self.register_controller(android_device, min_number=2)
14    for device in self.ads:
15      device.load_snippet('mbs', android_device.MBS_PACKAGE)
16      device.mbs.btEnable()
17    self.initiator, self.receiver = self.ads[:2]
18    # The initiator device scans BLE devices and behaves as a GATT client.
19    self.initiator.debug_tag = 'initiator'
20    # The receiver device advertises and behaves as a GATT server.
21    self.receiver.debug_tag = 'receiver'
22
23  def test_ble_gatt_read_write(self):
24    """Test for making a GATT connection and reading and writing messages.
25
26    Steps:
27      1. Starts BLE scan and advertising, and complete the discovery process.
28      2. Initiator connects to receiver.
29      3. Initiator discovers the BLE service receiver provided.
30      4. Initiator reads a message from receiver.
31      5. Initiator sends a message to receiver.
32      6. Initiator disconnects from receiver.
33      7. BLE scan and advertising stopped.
34
35    Verifies:
36      In each step, initiator and receiver get corresponding callbacks.
37    """
38    ble_utils.Discover(self.initiator, self.receiver)
39    ble_utils.Connect(self.initiator, self.receiver)
40    ble_utils.DiscoverServices(self.initiator)
41    ble_utils.ReadCharacteristic(self.initiator)
42    ble_utils.WriteCharacteristic(self.initiator, self.receiver)
43    ble_utils.Disconnect(self.initiator, self.receiver)
44    ble_utils.StopDiscover(self.initiator, self.receiver)
45
46
47if __name__ == '__main__':
48  test_runner.main()
49