1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.snippet.bluetooth;
18 
19 import android.bluetooth.BluetoothManager;
20 import android.content.Context;
21 
22 import androidx.test.platform.app.InstrumentationRegistry;
23 
24 import com.google.android.mobly.snippet.Snippet;
25 import com.google.android.mobly.snippet.rpc.Rpc;
26 
27 public class BluetoothGattMultiDevicesSnippet implements Snippet {
28     private static final String TAG = "BluetoothGattMultiDevicesSnippet";
29 
30     private BluetoothGattMultiDevicesServer mGattServer;
31     private BluetoothGattMultiDevicesClient mGattClient;
32 
33     private Context mContext;
34     private BluetoothManager mBluetoothManager;
35 
BluetoothGattMultiDevicesSnippet()36     public BluetoothGattMultiDevicesSnippet() {
37         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
38         mBluetoothManager = mContext.getSystemService(BluetoothManager.class);
39         var uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
40         uiAutomation.adoptShellPermissionIdentity();
41     }
42 
43     @Rpc(description = "Reset the state of client + server")
reset()44     public void reset() {
45         mGattServer = new BluetoothGattMultiDevicesServer(mContext, mBluetoothManager);
46         mGattClient = new BluetoothGattMultiDevicesClient(mContext, mBluetoothManager);
47     }
48 
49     @Rpc(description = "Creates Bluetooth GATT server with a given UUID and advertises it.")
createAndAdvertiseServer(String uuid)50     public void createAndAdvertiseServer(String uuid) {
51         mGattServer.createAndAdvertiseServer(uuid);
52     }
53 
54     @Rpc(
55             description =
56                     "Creates Bluetooth GATT server with a given UUID and ties it to an"
57                             + " advertisement.")
createAndAdvertiseIsolatedServer(String uuid)58     public void createAndAdvertiseIsolatedServer(String uuid) {
59         mGattServer.createAndAdvertiseIsolatedServer(uuid);
60     }
61 
62     @Rpc(description = "Connect to the peer device advertising the specified UUID")
connectGatt(String uuid)63     public boolean connectGatt(String uuid) {
64         return mGattClient.connect(uuid);
65     }
66 
67     @Rpc(description = "Enables Bluetooth")
enableBluetooth()68     public void enableBluetooth() {
69         mBluetoothManager.getAdapter().enable();
70     }
71 
72     @Rpc(description = "Disable Bluetooth")
disableBluetooth()73     public void disableBluetooth() {
74         mBluetoothManager.getAdapter().disable();
75     }
76 
77     @Rpc(description = "Checks Bluetooth state")
isBluetoothOn()78     public boolean isBluetoothOn() {
79         return mBluetoothManager.getAdapter().isEnabled();
80     }
81 
82     @Rpc(description = "Whether the connected peer has a service of the given UUID")
containsService(String uuid)83     public boolean containsService(String uuid) {
84         return mGattClient.containsService(uuid);
85     }
86 }
87