b/2126036 Improve remote device capability identification by switching to use UUIDs instead of class bits.
Change-Id: Ie60d1c579e40027c2174215c1989887a3250c9bc
This commit is contained in:
@@ -37,16 +37,13 @@ import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
|
|||||||
*/
|
*/
|
||||||
public class BluetoothEventRedirector {
|
public class BluetoothEventRedirector {
|
||||||
private static final String TAG = "BluetoothEventRedirector";
|
private static final String TAG = "BluetoothEventRedirector";
|
||||||
private static final boolean V = LocalBluetoothManager.V;
|
|
||||||
|
|
||||||
private LocalBluetoothManager mManager;
|
private LocalBluetoothManager mManager;
|
||||||
|
|
||||||
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
if (V) {
|
Log.v(TAG, "Received " + intent.getAction());
|
||||||
Log.v(TAG, "Received " + intent.getAction());
|
|
||||||
}
|
|
||||||
|
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||||
@@ -65,6 +62,8 @@ public class BluetoothEventRedirector {
|
|||||||
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
|
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
|
||||||
BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
|
BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
|
||||||
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
|
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
|
||||||
|
// TODO Pick up UUID. They should be available for 2.1 devices.
|
||||||
|
// Skip for now, there's a bluez problem and we are not getting uuids even for 2.1.
|
||||||
mManager.getCachedDeviceManager().onDeviceAppeared(device, rssi, btClass, name);
|
mManager.getCachedDeviceManager().onDeviceAppeared(device, rssi, btClass, name);
|
||||||
|
|
||||||
} else if (action.equals(BluetoothDevice.ACTION_DISAPPEARED)) {
|
} else if (action.equals(BluetoothDevice.ACTION_DISAPPEARED)) {
|
||||||
@@ -107,6 +106,9 @@ public class BluetoothEventRedirector {
|
|||||||
} else if (action.equals(BluetoothDevice.ACTION_CLASS_CHANGED)) {
|
} else if (action.equals(BluetoothDevice.ACTION_CLASS_CHANGED)) {
|
||||||
mManager.getCachedDeviceManager().onBtClassChanged(device);
|
mManager.getCachedDeviceManager().onBtClassChanged(device);
|
||||||
|
|
||||||
|
} else if (action.equals(BluetoothDevice.ACTION_UUID)) {
|
||||||
|
mManager.getCachedDeviceManager().onUuidChanged(device);
|
||||||
|
|
||||||
} else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) {
|
} else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) {
|
||||||
int errorMsg = R.string.bluetooth_pairing_error_message;
|
int errorMsg = R.string.bluetooth_pairing_error_message;
|
||||||
mManager.showError(device, R.string.bluetooth_error_title, errorMsg);
|
mManager.showError(device, R.string.bluetooth_error_title, errorMsg);
|
||||||
@@ -139,6 +141,7 @@ public class BluetoothEventRedirector {
|
|||||||
filter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
|
filter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
|
||||||
filter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED);
|
filter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED);
|
||||||
filter.addAction(BluetoothDevice.ACTION_CLASS_CHANGED);
|
filter.addAction(BluetoothDevice.ACTION_CLASS_CHANGED);
|
||||||
|
filter.addAction(BluetoothDevice.ACTION_UUID);
|
||||||
|
|
||||||
mManager.getContext().registerReceiver(mBroadcastReceiver, filter);
|
mManager.getContext().registerReceiver(mBroadcastReceiver, filter);
|
||||||
}
|
}
|
||||||
|
@@ -17,13 +17,15 @@
|
|||||||
package com.android.settings.bluetooth;
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.bluetooth.BluetoothClass;
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.bluetooth.BluetoothClass;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.bluetooth.ParcelUuid;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.os.SystemClock;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
@@ -50,6 +52,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
private static final String TAG = "CachedBluetoothDevice";
|
private static final String TAG = "CachedBluetoothDevice";
|
||||||
private static final boolean D = LocalBluetoothManager.D;
|
private static final boolean D = LocalBluetoothManager.D;
|
||||||
private static final boolean V = LocalBluetoothManager.V;
|
private static final boolean V = LocalBluetoothManager.V;
|
||||||
|
private static final boolean DEBUG = true; // STOPSHIP - disable before final rom
|
||||||
|
|
||||||
private static final int CONTEXT_ITEM_CONNECT = Menu.FIRST + 1;
|
private static final int CONTEXT_ITEM_CONNECT = Menu.FIRST + 1;
|
||||||
private static final int CONTEXT_ITEM_DISCONNECT = Menu.FIRST + 2;
|
private static final int CONTEXT_ITEM_DISCONNECT = Menu.FIRST + 2;
|
||||||
@@ -75,6 +78,17 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
*/
|
*/
|
||||||
private boolean mIsConnectingErrorPossible;
|
private boolean mIsConnectingErrorPossible;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Last time a bt profile auto-connect was attempted without any profiles or
|
||||||
|
* UUIDs. If an ACTION_UUID intent comes in within
|
||||||
|
* MAX_UUID_DELAY_FOR_AUTO_CONNECT milliseconds, we will try auto-connect
|
||||||
|
* again with the new UUIDs
|
||||||
|
*/
|
||||||
|
private long mConnectAttemptedWithoutUuid;
|
||||||
|
|
||||||
|
// See mConnectAttemptedWithoutUuid
|
||||||
|
private static final long MAX_UUID_DELAY_FOR_AUTO_CONNECT = 5000;
|
||||||
|
|
||||||
// Max time to hold the work queue if we don't get or missed a response
|
// Max time to hold the work queue if we don't get or missed a response
|
||||||
// from the bt framework.
|
// from the bt framework.
|
||||||
private static final long MAX_WAIT_TIME_FOR_FRAMEWORK = 25 * 1000;
|
private static final long MAX_WAIT_TIME_FOR_FRAMEWORK = 25 * 1000;
|
||||||
@@ -361,6 +375,16 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
public void connect() {
|
public void connect() {
|
||||||
if (!ensurePaired()) return;
|
if (!ensurePaired()) return;
|
||||||
|
|
||||||
|
// Try to initialize the profiles if there were not.
|
||||||
|
if (mProfiles.size() == 0) {
|
||||||
|
if (!updateProfiles()) {
|
||||||
|
// If UUIDs are not available yet, connect will be happen
|
||||||
|
// upon arrival of the ACTION_UUID intent.
|
||||||
|
mConnectAttemptedWithoutUuid = SystemClock.elapsedRealtime();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Reset the only-show-one-error-dialog tracking variable
|
// Reset the only-show-one-error-dialog tracking variable
|
||||||
mIsConnectingErrorPossible = true;
|
mIsConnectingErrorPossible = true;
|
||||||
|
|
||||||
@@ -479,6 +503,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
private void fillData() {
|
private void fillData() {
|
||||||
fetchName();
|
fetchName();
|
||||||
fetchBtClass();
|
fetchBtClass();
|
||||||
|
updateProfiles();
|
||||||
|
|
||||||
mVisible = false;
|
mVisible = false;
|
||||||
|
|
||||||
@@ -599,9 +624,47 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
*/
|
*/
|
||||||
private void fetchBtClass() {
|
private void fetchBtClass() {
|
||||||
mBtClass = mDevice.getBluetoothClass();
|
mBtClass = mDevice.getBluetoothClass();
|
||||||
if (mBtClass != null) {
|
}
|
||||||
LocalBluetoothProfileManager.fill(mBtClass, mProfiles);
|
|
||||||
|
private boolean updateProfiles() {
|
||||||
|
ParcelUuid[] uuids = mDevice.getUuids();
|
||||||
|
if (uuids == null) return false;
|
||||||
|
|
||||||
|
LocalBluetoothProfileManager.updateProfiles(uuids, mProfiles);
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
Log.e(TAG, "updating profiles for " + mDevice.getName());
|
||||||
|
|
||||||
|
boolean printUuids = true;
|
||||||
|
BluetoothClass bluetoothClass = mDevice.getBluetoothClass();
|
||||||
|
|
||||||
|
if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET) !=
|
||||||
|
mProfiles.contains(Profile.HEADSET)) {
|
||||||
|
Log.v(TAG, "headset classbits != uuid");
|
||||||
|
printUuids = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_A2DP) !=
|
||||||
|
mProfiles.contains(Profile.A2DP)) {
|
||||||
|
Log.v(TAG, "a2dp classbits != uuid");
|
||||||
|
printUuids = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_OPP) !=
|
||||||
|
mProfiles.contains(Profile.OPP)) {
|
||||||
|
Log.v(TAG, "opp classbits != uuid");
|
||||||
|
printUuids = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printUuids) {
|
||||||
|
Log.v(TAG, "Class: " + bluetoothClass.toString());
|
||||||
|
Log.v(TAG, "UUID:");
|
||||||
|
for (int i = 0; i < uuids.length; i++) {
|
||||||
|
Log.v(TAG, " " + uuids[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -613,10 +676,30 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
dispatchAttributesChanged();
|
dispatchAttributesChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refreshes the UI when framework alerts us of a UUID change.
|
||||||
|
*/
|
||||||
|
public void onUuidChanged() {
|
||||||
|
updateProfiles();
|
||||||
|
|
||||||
|
if (DEBUG) Log.e(TAG, "onUuidChanged: Time since last connect w/ no uuid "
|
||||||
|
+ (SystemClock.elapsedRealtime() - mConnectAttemptedWithoutUuid));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If a connect was attempted earlier without any UUID, we will do the
|
||||||
|
* connect now.
|
||||||
|
*/
|
||||||
|
if (mProfiles.size() > 0
|
||||||
|
&& (mConnectAttemptedWithoutUuid + MAX_UUID_DELAY_FOR_AUTO_CONNECT) > SystemClock
|
||||||
|
.elapsedRealtime()) {
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
dispatchAttributesChanged();
|
||||||
|
}
|
||||||
|
|
||||||
public void setBtClass(BluetoothClass btClass) {
|
public void setBtClass(BluetoothClass btClass) {
|
||||||
if (btClass != null && mBtClass != btClass) {
|
if (btClass != null && mBtClass != btClass) {
|
||||||
mBtClass = btClass;
|
mBtClass = btClass;
|
||||||
LocalBluetoothProfileManager.fill(mBtClass, mProfiles);
|
|
||||||
dispatchAttributesChanged();
|
dispatchAttributesChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -257,4 +257,11 @@ public class CachedBluetoothDeviceManager {
|
|||||||
cachedDevice.refreshBtClass();
|
cachedDevice.refreshBtClass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void onUuidChanged(BluetoothDevice device) {
|
||||||
|
CachedBluetoothDevice cachedDevice = findDevice(device);
|
||||||
|
if (cachedDevice != null) {
|
||||||
|
cachedDevice.onUuidChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,10 +18,10 @@ package com.android.settings.bluetooth;
|
|||||||
|
|
||||||
import android.bluetooth.BluetoothA2dp;
|
import android.bluetooth.BluetoothA2dp;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothClass;
|
|
||||||
import android.bluetooth.BluetoothHeadset;
|
import android.bluetooth.BluetoothHeadset;
|
||||||
|
import android.bluetooth.BluetoothUuid;
|
||||||
|
import android.bluetooth.ParcelUuid;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.text.TextUtils;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
|
||||||
@@ -35,6 +35,21 @@ import java.util.Set;
|
|||||||
* functionality related to a profile.
|
* functionality related to a profile.
|
||||||
*/
|
*/
|
||||||
public abstract class LocalBluetoothProfileManager {
|
public abstract class LocalBluetoothProfileManager {
|
||||||
|
private static final String TAG = "LocalBluetoothProfileManager";
|
||||||
|
|
||||||
|
private static final ParcelUuid[] HEADSET_PROFILE_UUIDS = new ParcelUuid[] {
|
||||||
|
BluetoothUuid.HSP,
|
||||||
|
BluetoothUuid.Handsfree,
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final ParcelUuid[] A2DP_PROFILE_UUIDS = new ParcelUuid[] {
|
||||||
|
BluetoothUuid.AudioSink,
|
||||||
|
BluetoothUuid.AdvAudioDist,
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final ParcelUuid[] OPP_PROFILE_UUIDS = new ParcelUuid[] {
|
||||||
|
BluetoothUuid.ObexObjectPush
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: close profiles when we're shutting down
|
// TODO: close profiles when we're shutting down
|
||||||
private static Map<Profile, LocalBluetoothProfileManager> sProfileMap =
|
private static Map<Profile, LocalBluetoothProfileManager> sProfileMap =
|
||||||
@@ -77,22 +92,25 @@ public abstract class LocalBluetoothProfileManager {
|
|||||||
*
|
*
|
||||||
* NOTE: This list happens to define the connection order. We should put this logic in a more
|
* NOTE: This list happens to define the connection order. We should put this logic in a more
|
||||||
* well known place when this method is no longer temporary.
|
* well known place when this method is no longer temporary.
|
||||||
*
|
* @param uuids of the remote device
|
||||||
* @param btClass The class
|
|
||||||
* @param profiles The list of profiles to fill
|
* @param profiles The list of profiles to fill
|
||||||
*/
|
*/
|
||||||
public static void fill(BluetoothClass btClass, List<Profile> profiles) {
|
public static void updateProfiles(ParcelUuid[] uuids, List<Profile> profiles) {
|
||||||
profiles.clear();
|
profiles.clear();
|
||||||
|
|
||||||
if (btClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) {
|
if (uuids == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BluetoothUuid.containsAnyUuid(uuids, HEADSET_PROFILE_UUIDS)) {
|
||||||
profiles.add(Profile.HEADSET);
|
profiles.add(Profile.HEADSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (btClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) {
|
if (BluetoothUuid.containsAnyUuid(uuids, A2DP_PROFILE_UUIDS)) {
|
||||||
profiles.add(Profile.A2DP);
|
profiles.add(Profile.A2DP);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (btClass.doesClassMatch(BluetoothClass.PROFILE_OPP)) {
|
if (BluetoothUuid.containsAnyUuid(uuids, OPP_PROFILE_UUIDS)) {
|
||||||
profiles.add(Profile.OPP);
|
profiles.add(Profile.OPP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user