1 /* 2 * Copyright (C) 2021 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.android.car.kitchensink.bluetooth; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.os.ParcelUuid; 25 import android.util.Log; 26 27 import java.util.concurrent.CompletableFuture; 28 import java.util.concurrent.TimeUnit; 29 30 public class BluetoothDeviceTypeChecker { 31 private static final ParcelUuid IAP_UUID = 32 ParcelUuid.fromString("00000000-deca-fade-deca-deafdecacafe"); 33 private static final int MAX_SECONDS_TO_BLOCK = 10; 34 private static final String TAG = "BluetoothDeviceTypeChecker"; 35 36 private final Context mContext; 37 private final boolean mAllowBlocking; 38 private final Object mLock = new Object(); 39 40 CompletableFuture<ParcelUuid[]> mDeviceUUidsFuture; 41 BluetoothDevice mBluetoothDevice; 42 43 /** 44 * BluetoothDeviceTypeChecker 45 * Class designed to fetch and check UUID records for matches based on either cached or live 46 * records. Live records are fetched if allowBlocking is enabled and there is nothing in the 47 * cache. Paired devices should always have records in the cache if the BluetoothAdapter is on. 48 * 49 * @param context The context on which to receive updates if live records are necessary 50 * @param allowBlocking If cached SDP records are not available allow methods to block in a 51 * best effort of acquiring them. 52 */ BluetoothDeviceTypeChecker(Context context, boolean allowBlocking)53 public BluetoothDeviceTypeChecker(Context context, boolean allowBlocking) { 54 mContext = context; 55 if (mContext != null) { 56 mAllowBlocking = allowBlocking; 57 } else { 58 mAllowBlocking = false; 59 } 60 } 61 62 /** 63 * isIapDevice 64 * Check if device is indicating support for iAP 65 * @param device 66 * @return 67 */ isIapDevice(BluetoothDevice device)68 public boolean isIapDevice(BluetoothDevice device) { 69 return deviceContainsUuid(device, IAP_UUID); 70 } 71 72 /** 73 * deviceContainsUuid 74 * Check if device contains a specific UUID record 75 * @param device to perform a lookup on 76 * @param uuid to check in the records 77 * @return 78 */ deviceContainsUuid(BluetoothDevice device, ParcelUuid uuid)79 public boolean deviceContainsUuid(BluetoothDevice device, ParcelUuid uuid) { 80 if (device == null) return false; 81 if (uuid == null) return false; 82 83 synchronized (mLock) { 84 mBluetoothDevice = device; 85 ParcelUuid[] uuidsArray = device.getUuids(); 86 if (mAllowBlocking && (uuidsArray == null || uuidsArray.length == 0)) { 87 uuidsArray = blockingFetchUuids(device); 88 } 89 if (uuidsArray == null || uuidsArray.length == 0) { 90 return false; 91 } 92 for (int i = 0; i < uuidsArray.length; i++) { 93 if (uuid.equals(uuidsArray[i])) { 94 return true; 95 } 96 } 97 return false; 98 } 99 } 100 101 /* 102 Perform a blocking fetch of the UUIDs on specified BluetoothDevice 103 */ blockingFetchUuids(BluetoothDevice device)104 private ParcelUuid[] blockingFetchUuids(BluetoothDevice device) { 105 IntentFilter filter = new IntentFilter(); 106 filter.addAction(BluetoothDevice.ACTION_UUID); 107 mContext.registerReceiver(mUuidReceiver, filter); 108 mDeviceUUidsFuture = new CompletableFuture<>(); 109 if (!device.fetchUuidsWithSdp()) { 110 Log.w(TAG, "fetching UUIDs failed."); 111 mContext.unregisterReceiver(mUuidReceiver); 112 return new ParcelUuid[0]; 113 } 114 try { 115 return mDeviceUUidsFuture.get(MAX_SECONDS_TO_BLOCK, TimeUnit.SECONDS); 116 } catch (Exception e) { 117 mContext.unregisterReceiver(mUuidReceiver); 118 return new ParcelUuid[0]; 119 } 120 } 121 122 /* 123 Broadcast receiver on which to receive updates to Bluetooth UUID records. 124 */ 125 private BroadcastReceiver mUuidReceiver = new BroadcastReceiver() { 126 @Override 127 public void onReceive(Context context, Intent intent) { 128 BluetoothDevice device = 129 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 130 if (mBluetoothDevice.equals(device) 131 && BluetoothDevice.ACTION_UUID.equals(intent.getAction())) { 132 mDeviceUUidsFuture.complete(device.getUuids()); 133 mContext.unregisterReceiver(this); 134 } 135 } 136 }; 137 } 138