More AttributionSource plumbing.
To prepare for future work which will plumb AttributionSource values through all remaining AIDLs, we need profiles to interact directly with the specific BluetoothAdapter they were created from. This is how we'll ensure that the relevant AttributionSource can be chained down from the original Context they're obtained from. This change also marks getDefaultAdapter() as deprecated to clearly communicate that BluetoothManager.getAdapter() is the best-practice path to obtaining a correctly scoped BluetoothAdapter instance. Bug: 183626112 Test: atest BluetoothInstrumentationTests Change-Id: I1e15170d7679019bbb6e396279d6e633e3dad4d6
This commit is contained in:
@@ -8633,7 +8633,7 @@ package android.bluetooth {
|
||||
method public android.bluetooth.le.BluetoothLeAdvertiser getBluetoothLeAdvertiser();
|
||||
method public android.bluetooth.le.BluetoothLeScanner getBluetoothLeScanner();
|
||||
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public java.util.Set<android.bluetooth.BluetoothDevice> getBondedDevices();
|
||||
method public static android.bluetooth.BluetoothAdapter getDefaultAdapter();
|
||||
method @Deprecated public static android.bluetooth.BluetoothAdapter getDefaultAdapter();
|
||||
method public int getLeMaximumAdvertisingDataLength();
|
||||
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public String getName();
|
||||
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public int getProfileConnectionState(int);
|
||||
|
||||
@@ -23,13 +23,13 @@ import android.annotation.Nullable;
|
||||
import android.annotation.RequiresNoPermission;
|
||||
import android.annotation.RequiresPermission;
|
||||
import android.annotation.SdkConstant;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.annotation.SystemApi;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.annotation.SystemApi;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -265,7 +265,8 @@ public final class BluetoothA2dp implements BluetoothProfile {
|
||||
@SystemApi
|
||||
public static final int DYNAMIC_BUFFER_SUPPORT_A2DP_SOFTWARE_ENCODING = 2;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothA2dp> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.A2DP, "BluetoothA2dp",
|
||||
IBluetoothA2dp.class.getName()) {
|
||||
@@ -279,8 +280,10 @@ public final class BluetoothA2dp implements BluetoothProfile {
|
||||
* Create a BluetoothA2dp proxy object for interacting with the local
|
||||
* Bluetooth A2DP service.
|
||||
*/
|
||||
/*package*/ BluetoothA2dp(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothA2dp(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -386,7 +389,8 @@ public final class BluetoothA2dp implements BluetoothProfile {
|
||||
try {
|
||||
final IBluetoothA2dp service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
}
|
||||
if (service == null) Log.w(TAG, "Proxy not attached to service");
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -407,7 +411,8 @@ public final class BluetoothA2dp implements BluetoothProfile {
|
||||
try {
|
||||
final IBluetoothA2dp service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
}
|
||||
if (service == null) Log.w(TAG, "Proxy not attached to service");
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -27,6 +27,7 @@ import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -78,7 +79,8 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
|
||||
public static final String ACTION_CONNECTION_STATE_CHANGED =
|
||||
"android.bluetooth.a2dp-sink.profile.action.CONNECTION_STATE_CHANGED";
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothA2dpSink> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.A2DP_SINK,
|
||||
"BluetoothA2dpSink", IBluetoothA2dpSink.class.getName()) {
|
||||
@@ -92,8 +94,10 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
|
||||
* Create a BluetoothA2dp proxy object for interacting with the local
|
||||
* Bluetooth A2DP service.
|
||||
*/
|
||||
/*package*/ BluetoothA2dpSink(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothA2dpSink(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -198,7 +202,8 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
|
||||
final IBluetoothA2dpSink service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -221,7 +226,8 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
|
||||
final IBluetoothA2dpSink service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -752,18 +752,23 @@ public final class BluetoothAdapter {
|
||||
|
||||
/**
|
||||
* Get a handle to the default local Bluetooth adapter.
|
||||
* <p>Currently Android only supports one Bluetooth adapter, but the API
|
||||
* could be extended to support more. This will always return the default
|
||||
* adapter.
|
||||
* <p>
|
||||
* Currently Android only supports one Bluetooth adapter, but the API could
|
||||
* be extended to support more. This will always return the default adapter.
|
||||
* </p>
|
||||
*
|
||||
* @return the default local adapter, or null if Bluetooth is not supported on this hardware
|
||||
* platform
|
||||
* @return the default local adapter, or null if Bluetooth is not supported
|
||||
* on this hardware platform
|
||||
* @deprecated this method will continue to work, but developers are
|
||||
* strongly encouraged to migrate to using
|
||||
* {@link BluetoothManager#getAdapter()}, since that approach
|
||||
* enables support for {@link Context#createAttributionContext}.
|
||||
*/
|
||||
@Deprecated
|
||||
@RequiresNoPermission
|
||||
public static synchronized BluetoothAdapter getDefaultAdapter() {
|
||||
if (sAdapter == null) {
|
||||
sAdapter = createAdapter(ActivityThread.currentAttributionSource());
|
||||
sAdapter = createAdapter(BluetoothManager.resolveAttributionSource(null));
|
||||
}
|
||||
return sAdapter;
|
||||
}
|
||||
@@ -2966,50 +2971,51 @@ public final class BluetoothAdapter {
|
||||
}
|
||||
|
||||
if (profile == BluetoothProfile.HEADSET) {
|
||||
BluetoothHeadset headset = new BluetoothHeadset(context, listener);
|
||||
BluetoothHeadset headset = new BluetoothHeadset(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.A2DP) {
|
||||
BluetoothA2dp a2dp = new BluetoothA2dp(context, listener);
|
||||
BluetoothA2dp a2dp = new BluetoothA2dp(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.A2DP_SINK) {
|
||||
BluetoothA2dpSink a2dpSink = new BluetoothA2dpSink(context, listener);
|
||||
BluetoothA2dpSink a2dpSink = new BluetoothA2dpSink(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.AVRCP_CONTROLLER) {
|
||||
BluetoothAvrcpController avrcp = new BluetoothAvrcpController(context, listener);
|
||||
BluetoothAvrcpController avrcp = new BluetoothAvrcpController(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.HID_HOST) {
|
||||
BluetoothHidHost iDev = new BluetoothHidHost(context, listener);
|
||||
BluetoothHidHost iDev = new BluetoothHidHost(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.PAN) {
|
||||
BluetoothPan pan = new BluetoothPan(context, listener);
|
||||
BluetoothPan pan = new BluetoothPan(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.PBAP) {
|
||||
BluetoothPbap pbap = new BluetoothPbap(context, listener);
|
||||
BluetoothPbap pbap = new BluetoothPbap(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.HEALTH) {
|
||||
Log.e(TAG, "getProfileProxy(): BluetoothHealth is deprecated");
|
||||
return false;
|
||||
} else if (profile == BluetoothProfile.MAP) {
|
||||
BluetoothMap map = new BluetoothMap(context, listener);
|
||||
BluetoothMap map = new BluetoothMap(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.HEADSET_CLIENT) {
|
||||
BluetoothHeadsetClient headsetClient = new BluetoothHeadsetClient(context, listener);
|
||||
BluetoothHeadsetClient headsetClient =
|
||||
new BluetoothHeadsetClient(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.SAP) {
|
||||
BluetoothSap sap = new BluetoothSap(context, listener);
|
||||
BluetoothSap sap = new BluetoothSap(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.PBAP_CLIENT) {
|
||||
BluetoothPbapClient pbapClient = new BluetoothPbapClient(context, listener);
|
||||
BluetoothPbapClient pbapClient = new BluetoothPbapClient(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.MAP_CLIENT) {
|
||||
BluetoothMapClient mapClient = new BluetoothMapClient(context, listener);
|
||||
BluetoothMapClient mapClient = new BluetoothMapClient(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.HID_DEVICE) {
|
||||
BluetoothHidDevice hidDevice = new BluetoothHidDevice(context, listener);
|
||||
BluetoothHidDevice hidDevice = new BluetoothHidDevice(context, listener, this);
|
||||
return true;
|
||||
} else if (profile == BluetoothProfile.HEARING_AID) {
|
||||
if (isHearingAidProfileSupported()) {
|
||||
BluetoothHearingAid hearingAid = new BluetoothHearingAid(context, listener);
|
||||
BluetoothHearingAid hearingAid = new BluetoothHearingAid(context, listener, this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.annotation.SuppressLint;
|
||||
import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
@@ -86,7 +87,8 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
|
||||
public static final String EXTRA_PLAYER_SETTING =
|
||||
"android.bluetooth.avrcp-controller.profile.extra.PLAYER_SETTING";
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothAvrcpController> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.AVRCP_CONTROLLER,
|
||||
"BluetoothAvrcpController", IBluetoothAvrcpController.class.getName()) {
|
||||
@@ -101,8 +103,10 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
|
||||
* Create a BluetoothAvrcpController proxy object for interacting with the local
|
||||
* Bluetooth AVRCP service.
|
||||
*/
|
||||
/*package*/ BluetoothAvrcpController(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothAvrcpController(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -131,7 +135,8 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
|
||||
getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -153,7 +158,8 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
|
||||
getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -24,7 +24,6 @@ import android.annotation.SdkConstant;
|
||||
import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SystemApi;
|
||||
import android.app.ActivityThread;
|
||||
import android.app.PropertyInvalidatedCache;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresBluetoothLocationPermission;
|
||||
@@ -48,6 +47,7 @@ import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -1167,13 +1167,29 @@ public final class BluetoothDevice implements Parcelable {
|
||||
|
||||
mAddress = address;
|
||||
mAddressType = ADDRESS_TYPE_PUBLIC;
|
||||
mAttributionSource = ActivityThread.currentAttributionSource();
|
||||
mAttributionSource = BluetoothManager.resolveAttributionSource(null);
|
||||
}
|
||||
|
||||
void setAttributionSource(AttributionSource attributionSource) {
|
||||
mAttributionSource = attributionSource;
|
||||
}
|
||||
|
||||
static BluetoothDevice setAttributionSource(BluetoothDevice device,
|
||||
AttributionSource attributionSource) {
|
||||
device.setAttributionSource(attributionSource);
|
||||
return device;
|
||||
}
|
||||
|
||||
static List<BluetoothDevice> setAttributionSource(List<BluetoothDevice> devices,
|
||||
AttributionSource attributionSource) {
|
||||
if (devices != null) {
|
||||
for (BluetoothDevice device : devices) {
|
||||
device.setAttributionSource(attributionSource);
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object o) {
|
||||
if (o instanceof BluetoothDevice) {
|
||||
|
||||
@@ -46,11 +46,12 @@ public final class BluetoothGattServer implements BluetoothProfile {
|
||||
private static final boolean DBG = true;
|
||||
private static final boolean VDBG = false;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private IBluetoothGatt mService;
|
||||
private BluetoothGattServerCallback mCallback;
|
||||
private final IBluetoothGatt mService;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
|
||||
private BluetoothGattServerCallback mCallback;
|
||||
|
||||
private Object mServerIfLock = new Object();
|
||||
private int mServerIf;
|
||||
private int mTransport;
|
||||
@@ -384,15 +385,15 @@ public final class BluetoothGattServer implements BluetoothProfile {
|
||||
/**
|
||||
* Create a BluetoothGattServer proxy object.
|
||||
*/
|
||||
/*package*/ BluetoothGattServer(
|
||||
IBluetoothGatt iGatt, int transport, AttributionSource attributionSource) {
|
||||
/* package */ BluetoothGattServer(IBluetoothGatt iGatt, int transport,
|
||||
BluetoothAdapter adapter) {
|
||||
mService = iGatt;
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mCallback = null;
|
||||
mServerIf = 0;
|
||||
mTransport = transport;
|
||||
mServices = new ArrayList<BluetoothGattService>();
|
||||
mAttributionSource = attributionSource;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,6 +28,7 @@ import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.annotation.SystemApi;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
@@ -339,7 +340,8 @@ public final class BluetoothHeadset implements BluetoothProfile {
|
||||
private Context mContext;
|
||||
private ServiceListener mServiceListener;
|
||||
private volatile IBluetoothHeadset mService;
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
|
||||
@SuppressLint("AndroidFrameworkBluetoothPermission")
|
||||
private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
|
||||
@@ -357,10 +359,11 @@ public final class BluetoothHeadset implements BluetoothProfile {
|
||||
/**
|
||||
* Create a BluetoothHeadset proxy object.
|
||||
*/
|
||||
/*package*/ BluetoothHeadset(Context context, ServiceListener l) {
|
||||
/* package */ BluetoothHeadset(Context context, ServiceListener l, BluetoothAdapter adapter) {
|
||||
mContext = context;
|
||||
mServiceListener = l;
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
|
||||
IBluetoothManager mgr = mAdapter.getBluetoothManager();
|
||||
if (mgr != null) {
|
||||
@@ -519,7 +522,8 @@ public final class BluetoothHeadset implements BluetoothProfile {
|
||||
final IBluetoothHeadset service = mService;
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -540,7 +544,8 @@ public final class BluetoothHeadset implements BluetoothProfile {
|
||||
final IBluetoothHeadset service = mService;
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -424,7 +425,8 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
|
||||
public static final int CALL_ACCEPT_HOLD = 1;
|
||||
public static final int CALL_ACCEPT_TERMINATE = 2;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothHeadsetClient> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.HEADSET_CLIENT,
|
||||
"BluetoothHeadsetClient", IBluetoothHeadsetClient.class.getName()) {
|
||||
@@ -437,8 +439,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
|
||||
/**
|
||||
* Create a BluetoothHeadsetClient proxy object.
|
||||
*/
|
||||
/*package*/ BluetoothHeadsetClient(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothHeadsetClient(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -531,7 +535,8 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
|
||||
getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -557,7 +562,8 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
|
||||
getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -28,6 +28,7 @@ import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.annotation.SystemApi;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -130,7 +131,8 @@ public final class BluetoothHearingAid implements BluetoothProfile {
|
||||
*/
|
||||
public static final long HI_SYNC_ID_INVALID = IBluetoothHearingAid.HI_SYNC_ID_INVALID;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothHearingAid> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.HEARING_AID,
|
||||
"BluetoothHearingAid", IBluetoothHearingAid.class.getName()) {
|
||||
@@ -144,8 +146,10 @@ public final class BluetoothHearingAid implements BluetoothProfile {
|
||||
* Create a BluetoothHearingAid proxy object for interacting with the local
|
||||
* Bluetooth Hearing Aid service.
|
||||
*/
|
||||
/*package*/ BluetoothHearingAid(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothHearingAid(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -240,7 +244,8 @@ public final class BluetoothHearingAid implements BluetoothProfile {
|
||||
final IBluetoothHearingAid service = getService();
|
||||
try {
|
||||
if (service != null && isEnabled()) {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
}
|
||||
if (service == null) Log.w(TAG, "Proxy not attached to service");
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -262,7 +267,8 @@ public final class BluetoothHearingAid implements BluetoothProfile {
|
||||
final IBluetoothHearingAid service = getService();
|
||||
try {
|
||||
if (service != null && isEnabled()) {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
}
|
||||
if (service == null) Log.w(TAG, "Proxy not attached to service");
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.annotation.SystemApi;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
@@ -415,7 +416,8 @@ public final class BluetoothHidDevice implements BluetoothProfile {
|
||||
}
|
||||
}
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothHidDevice> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.HID_DEVICE,
|
||||
"BluetoothHidDevice", IBluetoothHidDevice.class.getName()) {
|
||||
@@ -425,8 +427,9 @@ public final class BluetoothHidDevice implements BluetoothProfile {
|
||||
}
|
||||
};
|
||||
|
||||
BluetoothHidDevice(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
BluetoothHidDevice(Context context, ServiceListener listener, BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -446,7 +449,8 @@ public final class BluetoothHidDevice implements BluetoothProfile {
|
||||
final IBluetoothHidDevice service = getService();
|
||||
if (service != null) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, e.toString());
|
||||
}
|
||||
@@ -465,7 +469,8 @@ public final class BluetoothHidDevice implements BluetoothProfile {
|
||||
final IBluetoothHidDevice service = getService();
|
||||
if (service != null) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, e.toString());
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SystemApi;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
@@ -236,7 +237,8 @@ public final class BluetoothHidHost implements BluetoothProfile {
|
||||
public static final String EXTRA_IDLE_TIME =
|
||||
"android.bluetooth.BluetoothHidHost.extra.IDLE_TIME";
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothHidHost> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.HID_HOST,
|
||||
"BluetoothHidHost", IBluetoothHidHost.class.getName()) {
|
||||
@@ -250,8 +252,10 @@ public final class BluetoothHidHost implements BluetoothProfile {
|
||||
* Create a BluetoothHidHost proxy object for interacting with the local
|
||||
* Bluetooth Service which handles the InputDevice profile
|
||||
*/
|
||||
/*package*/ BluetoothHidHost(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothHidHost(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -348,7 +352,8 @@ public final class BluetoothHidHost implements BluetoothProfile {
|
||||
final IBluetoothHidHost service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -371,7 +376,8 @@ public final class BluetoothHidHost implements BluetoothProfile {
|
||||
final IBluetoothHidHost service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
@@ -101,7 +102,8 @@ public final class BluetoothLeAudio implements BluetoothProfile, AutoCloseable {
|
||||
*/
|
||||
public static final int GROUP_ID_INVALID = IBluetoothLeAudio.LE_AUDIO_GROUP_ID_INVALID;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothLeAudio> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.LE_AUDIO, "BluetoothLeAudio",
|
||||
IBluetoothLeAudio.class.getName()) {
|
||||
@@ -115,8 +117,10 @@ public final class BluetoothLeAudio implements BluetoothProfile, AutoCloseable {
|
||||
* Create a BluetoothLeAudio proxy object for interacting with the local
|
||||
* Bluetooth LeAudio service.
|
||||
*/
|
||||
/*package*/ BluetoothLeAudio(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothLeAudio(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
mCloseGuard = new CloseGuard();
|
||||
mCloseGuard.open("close");
|
||||
@@ -223,7 +227,8 @@ public final class BluetoothLeAudio implements BluetoothProfile, AutoCloseable {
|
||||
try {
|
||||
final IBluetoothLeAudio service = getService();
|
||||
if (service != null && mAdapter.isEnabled()) {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
}
|
||||
if (service == null) Log.w(TAG, "Proxy not attached to service");
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -245,7 +250,8 @@ public final class BluetoothLeAudio implements BluetoothProfile, AutoCloseable {
|
||||
try {
|
||||
final IBluetoothLeAudio service = getService();
|
||||
if (service != null && mAdapter.isEnabled()) {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
}
|
||||
if (service == null) Log.w(TAG, "Proxy not attached to service");
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -21,6 +21,7 @@ import android.annotation.RequiresNoPermission;
|
||||
import android.annotation.RequiresPermission;
|
||||
import android.annotation.SystemService;
|
||||
import android.app.ActivityThread;
|
||||
import android.app.AppGlobals;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.content.AttributionSource;
|
||||
@@ -31,7 +32,6 @@ import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* High level manager used to obtain an instance of an {@link BluetoothAdapter}
|
||||
@@ -66,11 +66,36 @@ public final class BluetoothManager {
|
||||
* @hide
|
||||
*/
|
||||
public BluetoothManager(Context context) {
|
||||
mAttributionSource = (context != null) ? context.getAttributionSource()
|
||||
: ActivityThread.currentAttributionSource();
|
||||
mAttributionSource = resolveAttributionSource(context);
|
||||
mAdapter = BluetoothAdapter.createAdapter(mAttributionSource);
|
||||
}
|
||||
|
||||
/** {@hide} */
|
||||
public static AttributionSource resolveAttributionSource(Context context) {
|
||||
AttributionSource res = null;
|
||||
if (context != null) {
|
||||
res = context.getAttributionSource();
|
||||
}
|
||||
if (res == null) {
|
||||
res = ActivityThread.currentAttributionSource();
|
||||
}
|
||||
if (res == null) {
|
||||
int uid = android.os.Process.myUid();
|
||||
if (uid == android.os.Process.ROOT_UID) {
|
||||
uid = android.os.Process.SYSTEM_UID;
|
||||
}
|
||||
try {
|
||||
res = new AttributionSource(uid,
|
||||
AppGlobals.getPackageManager().getPackagesForUid(uid)[0], null);
|
||||
} catch (RemoteException ignored) {
|
||||
}
|
||||
}
|
||||
if (res == null) {
|
||||
throw new IllegalStateException("Failed to resolve AttributionSource");
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the BLUETOOTH Adapter for this device.
|
||||
*
|
||||
@@ -129,24 +154,9 @@ public final class BluetoothManager {
|
||||
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
|
||||
public List<BluetoothDevice> getConnectedDevices(int profile) {
|
||||
if (DBG) Log.d(TAG, "getConnectedDevices");
|
||||
if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) {
|
||||
throw new IllegalArgumentException("Profile not supported: " + profile);
|
||||
}
|
||||
|
||||
List<BluetoothDevice> connectedDevices = new ArrayList<BluetoothDevice>();
|
||||
|
||||
try {
|
||||
IBluetoothManager managerService = mAdapter.getBluetoothManager();
|
||||
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
|
||||
if (iGatt == null) return connectedDevices;
|
||||
|
||||
connectedDevices = iGatt.getDevicesMatchingConnectionStates(
|
||||
new int[]{BluetoothProfile.STATE_CONNECTED}, mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "", e);
|
||||
}
|
||||
|
||||
return connectedDevices;
|
||||
return getDevicesMatchingConnectionStates(profile, new int[] {
|
||||
BluetoothProfile.STATE_CONNECTED
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,8 +193,9 @@ public final class BluetoothManager {
|
||||
IBluetoothManager managerService = mAdapter.getBluetoothManager();
|
||||
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
|
||||
if (iGatt == null) return devices;
|
||||
devices = iGatt.getDevicesMatchingConnectionStates(
|
||||
states, mAttributionSource);
|
||||
devices = BluetoothDevice.setAttributionSource(
|
||||
iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource),
|
||||
mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "", e);
|
||||
}
|
||||
@@ -288,7 +299,7 @@ public final class BluetoothManager {
|
||||
return null;
|
||||
}
|
||||
BluetoothGattServer mGattServer =
|
||||
new BluetoothGattServer(iGatt, transport, mAttributionSource);
|
||||
new BluetoothGattServer(iGatt, transport, mAdapter);
|
||||
Boolean regStatus = mGattServer.registerCallback(callback, eatt_support);
|
||||
return regStatus ? mGattServer : null;
|
||||
} catch (RemoteException e) {
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.annotation.SystemApi;
|
||||
import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -79,7 +80,8 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
|
||||
*/
|
||||
public static final int RESULT_CANCELED = 2;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothMap> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.MAP,
|
||||
"BluetoothMap", IBluetoothMap.class.getName()) {
|
||||
@@ -92,9 +94,11 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
|
||||
/**
|
||||
* Create a BluetoothMap proxy object.
|
||||
*/
|
||||
/*package*/ BluetoothMap(Context context, ServiceListener listener) {
|
||||
/* package */ BluetoothMap(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
mCloseGuard = new CloseGuard();
|
||||
mCloseGuard.open("close");
|
||||
@@ -281,7 +285,8 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
|
||||
final IBluetoothMap service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -305,7 +310,8 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
|
||||
final IBluetoothMap service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -455,13 +461,10 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
|
||||
log("Bluetooth is Not enabled");
|
||||
return false;
|
||||
return mAdapter.isEnabled();
|
||||
}
|
||||
|
||||
private static boolean isValidDevice(BluetoothDevice device) {
|
||||
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.app.PendingIntent;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Binder;
|
||||
@@ -173,7 +174,8 @@ public final class BluetoothMapClient implements BluetoothProfile {
|
||||
/** @hide */
|
||||
public static final int DELETED = 3;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothMapClient> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.MAP_CLIENT,
|
||||
"BluetoothMapClient", IBluetoothMapClient.class.getName()) {
|
||||
@@ -186,9 +188,11 @@ public final class BluetoothMapClient implements BluetoothProfile {
|
||||
/**
|
||||
* Create a BluetoothMapClient proxy object.
|
||||
*/
|
||||
/*package*/ BluetoothMapClient(Context context, ServiceListener listener) {
|
||||
/* package */ BluetoothMapClient(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
if (DBG) Log.d(TAG, "Create BluetoothMapClient proxy object");
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -299,7 +303,8 @@ public final class BluetoothMapClient implements BluetoothProfile {
|
||||
final IBluetoothMapClient service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<>();
|
||||
@@ -323,7 +328,8 @@ public final class BluetoothMapClient implements BluetoothProfile {
|
||||
final IBluetoothMapClient service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<>();
|
||||
@@ -620,14 +626,10 @@ public final class BluetoothMapClient implements BluetoothProfile {
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
|
||||
if (DBG) Log.d(TAG, "Bluetooth is Not enabled");
|
||||
return false;
|
||||
return mAdapter.isEnabled();
|
||||
}
|
||||
|
||||
private static boolean isValidDevice(BluetoothDevice device) {
|
||||
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SystemApi;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -183,7 +184,8 @@ public final class BluetoothPan implements BluetoothProfile {
|
||||
|
||||
private final Context mContext;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothPan> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.PAN,
|
||||
"BluetoothPan", IBluetoothPan.class.getName()) {
|
||||
@@ -201,8 +203,10 @@ public final class BluetoothPan implements BluetoothProfile {
|
||||
* @hide
|
||||
*/
|
||||
@UnsupportedAppUsage
|
||||
/*package*/ BluetoothPan(Context context, ServiceListener listener) {
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
/* package */ BluetoothPan(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mContext = context;
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
@@ -355,7 +359,8 @@ public final class BluetoothPan implements BluetoothProfile {
|
||||
final IBluetoothPan service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -381,7 +386,8 @@ public final class BluetoothPan implements BluetoothProfile {
|
||||
final IBluetoothPan service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.annotation.SystemApi;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -98,7 +99,8 @@ public class BluetoothPbap implements BluetoothProfile {
|
||||
private volatile IBluetoothPbap mService;
|
||||
private final Context mContext;
|
||||
private ServiceListener mServiceListener;
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
|
||||
/** @hide */
|
||||
public static final int RESULT_FAILURE = 0;
|
||||
@@ -129,10 +131,11 @@ public class BluetoothPbap implements BluetoothProfile {
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public BluetoothPbap(Context context, ServiceListener l) {
|
||||
public BluetoothPbap(Context context, ServiceListener l, BluetoothAdapter adapter) {
|
||||
mContext = context;
|
||||
mServiceListener = l;
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
IBluetoothManager mgr = mAdapter.getBluetoothManager();
|
||||
if (mgr != null) {
|
||||
try {
|
||||
@@ -229,7 +232,8 @@ public class BluetoothPbap implements BluetoothProfile {
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
}
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, e.toString());
|
||||
}
|
||||
@@ -277,7 +281,8 @@ public class BluetoothPbap implements BluetoothProfile {
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
}
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, e.toString());
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.annotation.SdkConstant;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
@@ -57,7 +58,8 @@ public final class BluetoothPbapClient implements BluetoothProfile {
|
||||
/** Connection canceled before completion. */
|
||||
public static final int RESULT_CANCELED = 2;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothPbapClient> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.PBAP_CLIENT,
|
||||
"BluetoothPbapClient", IBluetoothPbapClient.class.getName()) {
|
||||
@@ -70,11 +72,12 @@ public final class BluetoothPbapClient implements BluetoothProfile {
|
||||
/**
|
||||
* Create a BluetoothPbapClient proxy object.
|
||||
*/
|
||||
BluetoothPbapClient(Context context, ServiceListener listener) {
|
||||
BluetoothPbapClient(Context context, ServiceListener listener, BluetoothAdapter adapter) {
|
||||
if (DBG) {
|
||||
Log.d(TAG, "Create BluetoothPbapClient proxy object");
|
||||
}
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -176,7 +179,8 @@ public final class BluetoothPbapClient implements BluetoothProfile {
|
||||
final IBluetoothPbapClient service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -203,7 +207,8 @@ public final class BluetoothPbapClient implements BluetoothProfile {
|
||||
final IBluetoothPbapClient service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -247,12 +252,7 @@ public final class BluetoothPbapClient implements BluetoothProfile {
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) {
|
||||
return true;
|
||||
}
|
||||
log("Bluetooth is Not enabled");
|
||||
return false;
|
||||
return mAdapter.isEnabled();
|
||||
}
|
||||
|
||||
private static boolean isValidDevice(BluetoothDevice device) {
|
||||
|
||||
@@ -20,11 +20,11 @@ import android.Manifest;
|
||||
import android.annotation.RequiresNoPermission;
|
||||
import android.annotation.RequiresPermission;
|
||||
import android.annotation.SdkConstant;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SdkConstant.SdkConstantType;
|
||||
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
|
||||
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.AttributionSource;
|
||||
import android.content.Context;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -97,7 +97,8 @@ public final class BluetoothSap implements BluetoothProfile {
|
||||
*/
|
||||
public static final int RESULT_CANCELED = 2;
|
||||
|
||||
private BluetoothAdapter mAdapter;
|
||||
private final BluetoothAdapter mAdapter;
|
||||
private final AttributionSource mAttributionSource;
|
||||
private final BluetoothProfileConnector<IBluetoothSap> mProfileConnector =
|
||||
new BluetoothProfileConnector(this, BluetoothProfile.SAP,
|
||||
"BluetoothSap", IBluetoothSap.class.getName()) {
|
||||
@@ -110,9 +111,11 @@ public final class BluetoothSap implements BluetoothProfile {
|
||||
/**
|
||||
* Create a BluetoothSap proxy object.
|
||||
*/
|
||||
/*package*/ BluetoothSap(Context context, ServiceListener listener) {
|
||||
/* package */ BluetoothSap(Context context, ServiceListener listener,
|
||||
BluetoothAdapter adapter) {
|
||||
if (DBG) Log.d(TAG, "Create BluetoothSap proxy object");
|
||||
mAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mAdapter = adapter;
|
||||
mAttributionSource = adapter.getAttributionSource();
|
||||
mProfileConnector.connect(context, listener);
|
||||
}
|
||||
|
||||
@@ -265,7 +268,8 @@ public final class BluetoothSap implements BluetoothProfile {
|
||||
final IBluetoothSap service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getConnectedDevices();
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getConnectedDevices(), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -288,7 +292,8 @@ public final class BluetoothSap implements BluetoothProfile {
|
||||
final IBluetoothSap service = getService();
|
||||
if (service != null && isEnabled()) {
|
||||
try {
|
||||
return service.getDevicesMatchingConnectionStates(states);
|
||||
return BluetoothDevice.setAttributionSource(
|
||||
service.getDevicesMatchingConnectionStates(states), mAttributionSource);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(new Throwable()));
|
||||
return new ArrayList<BluetoothDevice>();
|
||||
@@ -435,17 +440,10 @@ public final class BluetoothSap implements BluetoothProfile {
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
|
||||
if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) {
|
||||
return true;
|
||||
}
|
||||
log("Bluetooth is Not enabled");
|
||||
return false;
|
||||
return mAdapter.isEnabled();
|
||||
}
|
||||
|
||||
private static boolean isValidDevice(BluetoothDevice device) {
|
||||
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user