1 /* 2 * Copyright (C) 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 android.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.ParcelUuid; 26 27 import androidx.test.core.app.ApplicationProvider; 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 30 import com.android.compatibility.common.util.AdoptShellPermissionsRule; 31 32 import com.google.common.util.concurrent.SettableFuture; 33 import com.google.protobuf.ByteString; 34 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 import pandora.HostProto.ConnectRequest; 40 41 /** Test cases for {@link ServiceDiscoveryManager}. */ 42 @RunWith(AndroidJUnit4.class) 43 public class SdpClientTest { 44 private static final String TAG = "SdpClientTest"; 45 46 private final Context mContext = ApplicationProvider.getApplicationContext(); 47 private final BluetoothManager mManager = mContext.getSystemService(BluetoothManager.class); 48 private final BluetoothAdapter mAdapter = mManager.getAdapter(); 49 50 private SettableFuture<ParcelUuid[]> mFutureIntent; 51 52 @Rule public final AdoptShellPermissionsRule mPermissionRule = new AdoptShellPermissionsRule(); 53 54 @Rule public final PandoraDevice mBumble = new PandoraDevice(); 55 56 private BroadcastReceiver mConnectionStateReceiver = 57 new BroadcastReceiver() { 58 @Override 59 public void onReceive(Context context, Intent intent) { 60 if (BluetoothDevice.ACTION_UUID.equals(intent.getAction())) { 61 ParcelUuid[] parcelUuids = 62 intent.getParcelableArrayExtra( 63 BluetoothDevice.EXTRA_UUID, ParcelUuid.class); 64 mFutureIntent.set(parcelUuids); 65 } 66 } 67 }; 68 69 @Test remoteConnectServiceDiscoveryTest()70 public void remoteConnectServiceDiscoveryTest() throws Exception { 71 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_UUID); 72 mContext.registerReceiver(mConnectionStateReceiver, filter); 73 74 mFutureIntent = SettableFuture.create(); 75 76 String local_addr = mAdapter.getAddress(); 77 byte[] local_bytes_addr = Utils.addressBytesFromString(local_addr); 78 79 // Initiate connect from remote 80 mBumble.hostBlocking() 81 .connect( 82 ConnectRequest.newBuilder() 83 .setAddress(ByteString.copyFrom(local_bytes_addr)) 84 .build()); 85 86 // Get the remote device 87 BluetoothDevice device = mBumble.getRemoteDevice(); 88 89 // Execute service discovery procedure 90 assertThat(device.fetchUuidsWithSdp()).isTrue(); 91 92 ParcelUuid[] arr = mFutureIntent.get(); 93 assertThat(arr).asList().contains(BluetoothUuid.HFP); 94 95 mContext.unregisterReceiver(mConnectionStateReceiver); 96 } 97 } 98