From b1301fa2849bafd6daa422281dc5200863bc761e Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Sun, 9 Oct 2016 12:54:42 -0700 Subject: [PATCH] Add a way to query for supported Bluetooth profiles. Currently there is no way to get the profiles supported by the Bluetooth adapter. Asking for a profile proxy of an unsupported profile does not fail and can lead to code indefinitely waiting for the proxy response. This new code will allow for checking the supported profiles before asking for the proxies. Bug: 26451648 Change-Id: I4b48e7151f5ca53851aa3b967c143fae140ecd34 --- .../android/bluetooth/BluetoothAdapter.java | 30 +++++++++++++++++++ .../android/bluetooth/BluetoothProfile.java | 7 +++++ core/java/android/bluetooth/IBluetooth.aidl | 1 + 3 files changed, 38 insertions(+) diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 246a752b3a5b0..b93a5578f2b8b 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -1497,6 +1497,36 @@ public final class BluetoothAdapter { return null; } + /** + * Gets the currently supported profiles by the adapter. + * + *

This can be used to check whether a profile is supported before attempting + * to connect to its respective proxy. + * + * @return a list of integers indicating the ids of supported profiles as defined in + * {@link BluetoothProfile}. + * @hide + */ + public List getSupportedProfiles() { + final ArrayList supportedProfiles = new ArrayList(); + + try { + synchronized (mManagerCallback) { + if (mService != null) { + final long supportedProfilesBitMask = mService.getSupportedProfiles(); + + for (int i = 0; i <= BluetoothProfile.MAX_PROFILE_ID; i++) { + if ((supportedProfilesBitMask & (1 << i)) != 0) { + supportedProfiles.add(i); + } + } + } + } + } catch (RemoteException e) {Log.e(TAG, "getSupportedProfiles:", e);} + + return supportedProfiles; + } + /** * Get the current connection state of the local Bluetooth adapter. * This can be used to check whether the local Bluetooth adapter is connected diff --git a/core/java/android/bluetooth/BluetoothProfile.java b/core/java/android/bluetooth/BluetoothProfile.java index eee66d193fe43..20d95ccc38356 100644 --- a/core/java/android/bluetooth/BluetoothProfile.java +++ b/core/java/android/bluetooth/BluetoothProfile.java @@ -136,6 +136,13 @@ public interface BluetoothProfile { */ public static final int PBAP_CLIENT = 17; + /** + * Max profile ID. This value should be updated whenever a new profile is added to match + * the largest value assigned to a profile. + * @hide + */ + public static final int MAX_PROFILE_ID = 17; + /** * Default priority for devices that we try to auto-connect to and * and allow incoming connections for the profile diff --git a/core/java/android/bluetooth/IBluetooth.aidl b/core/java/android/bluetooth/IBluetooth.aidl index a420539636780..f28ab275e4c77 100644 --- a/core/java/android/bluetooth/IBluetooth.aidl +++ b/core/java/android/bluetooth/IBluetooth.aidl @@ -62,6 +62,7 @@ interface IBluetooth boolean cancelBondProcess(in BluetoothDevice device); boolean removeBond(in BluetoothDevice device); int getBondState(in BluetoothDevice device); + long getSupportedProfiles(); int getConnectionState(in BluetoothDevice device); String getRemoteName(in BluetoothDevice device);