diff options
| author | Oli Lan <olilan@google.com> | 2021-04-22 19:05:17 +0100 |
|---|---|---|
| committer | Jeff Sharkey <jsharkey@android.com> | 2021-04-22 14:47:39 -0600 |
| commit | 4dabcb764f1948823dcd74eefb3440afcab07db2 (patch) | |
| tree | 32b0d4ad5c2e3f8d37a08b619d7d78bcd4e2a295 /framework/java | |
| parent | 4e4c9c4796c2c16d32e87417e084a1f724e9f258 (diff) | |
Pass attribution source to BT APIs.
This adds attribution source to BT method calls. This is now
required to allow the app ops for the new BT permissions
(BLUETOOTH_CONNECT, BLUETOOTH_ADVERTISE, and BLUETOOTH_SCAN)
to be noted.
Bug: 183626112
Test: atest BluetoothInstrumentationTests
Change-Id: I81598553b762e491d6364064a2e1ef41dec89bf9
Diffstat (limited to 'framework/java')
9 files changed, 199 insertions, 168 deletions
diff --git a/framework/java/android/bluetooth/BluetoothAdapter.java b/framework/java/android/bluetooth/BluetoothAdapter.java index 2426ead810..4ef37379a5 100644 --- a/framework/java/android/bluetooth/BluetoothAdapter.java +++ b/framework/java/android/bluetooth/BluetoothAdapter.java @@ -706,11 +706,13 @@ public final class BluetoothAdapter { */ private static BluetoothAdapter sAdapter; - private static BluetoothLeScanner sBluetoothLeScanner; - private static BluetoothLeAdvertiser sBluetoothLeAdvertiser; - private static PeriodicAdvertisingManager sPeriodicAdvertisingManager; + private BluetoothLeScanner mBluetoothLeScanner; + private BluetoothLeAdvertiser mBluetoothLeAdvertiser; + private PeriodicAdvertisingManager mPeriodicAdvertisingManager; private final IBluetoothManager mManagerService; + private final AttributionSource mAttributionSource; + @UnsupportedAppUsage private IBluetooth mService; private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock(); @@ -722,8 +724,6 @@ public final class BluetoothAdapter { private final Map<BluetoothConnectionCallback, Executor> mBluetoothConnectionCallbackExecutorMap = new HashMap<>(); - private AttributionSource mAttributionSource; - /** * Bluetooth metadata listener. Overrides the default BluetoothMetadataListener * implementation. @@ -763,16 +763,17 @@ public final class BluetoothAdapter { @RequiresNoPermission public static synchronized BluetoothAdapter getDefaultAdapter() { if (sAdapter == null) { - sAdapter = createAdapter(); + sAdapter = createAdapter(ActivityThread.currentAttributionSource()); } return sAdapter; } /** {@hide} */ - public static BluetoothAdapter createAdapter() { + public static BluetoothAdapter createAdapter(AttributionSource attributionSource) { IBinder binder = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE); if (binder != null) { - return new BluetoothAdapter(IBluetoothManager.Stub.asInterface(binder)); + return new BluetoothAdapter(IBluetoothManager.Stub.asInterface(binder), + attributionSource); } else { Log.e(TAG, "Bluetooth binder is null"); return null; @@ -782,7 +783,7 @@ public final class BluetoothAdapter { /** * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance. */ - BluetoothAdapter(IBluetoothManager managerService) { + BluetoothAdapter(IBluetoothManager managerService, AttributionSource attributionSource) { if (managerService == null) { throw new IllegalArgumentException("bluetooth manager service is null"); } @@ -794,20 +795,12 @@ public final class BluetoothAdapter { } finally { mServiceLock.writeLock().unlock(); } - mManagerService = managerService; + mManagerService = Objects.requireNonNull(managerService); + mAttributionSource = Objects.requireNonNull(attributionSource); mLeScanClients = new HashMap<LeScanCallback, ScanCallback>(); mToken = new Binder(); } - void setAttributionSource(AttributionSource attributionSource) { - mAttributionSource = attributionSource; - } - - private AttributionSource resolveAttributionSource() { - return (mAttributionSource != null) ? mAttributionSource - : ActivityThread.currentAttributionSource(); - } - /** * Get a {@link BluetoothDevice} object for the given Bluetooth hardware * address. @@ -864,11 +857,11 @@ public final class BluetoothAdapter { return null; } synchronized (mLock) { - if (sBluetoothLeAdvertiser == null) { - sBluetoothLeAdvertiser = new BluetoothLeAdvertiser(mManagerService); + if (mBluetoothLeAdvertiser == null) { + mBluetoothLeAdvertiser = new BluetoothLeAdvertiser(this); } + return mBluetoothLeAdvertiser; } - return sBluetoothLeAdvertiser; } /** @@ -892,11 +885,11 @@ public final class BluetoothAdapter { } synchronized (mLock) { - if (sPeriodicAdvertisingManager == null) { - sPeriodicAdvertisingManager = new PeriodicAdvertisingManager(mManagerService); + if (mPeriodicAdvertisingManager == null) { + mPeriodicAdvertisingManager = new PeriodicAdvertisingManager(this); } + return mPeriodicAdvertisingManager; } - return sPeriodicAdvertisingManager; } /** @@ -908,12 +901,11 @@ public final class BluetoothAdapter { return null; } synchronized (mLock) { - if (sBluetoothLeScanner == null) { - sBluetoothLeScanner = - new BluetoothLeScanner(mManagerService, resolveAttributionSource()); + if (mBluetoothLeScanner == null) { + mBluetoothLeScanner = new BluetoothLeScanner(this); } + return mBluetoothLeScanner; } - return sBluetoothLeScanner; } /** @@ -1349,7 +1341,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.getUuids(resolveAttributionSource()); + return mService.getUuids(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1383,7 +1375,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.setName(name, resolveAttributionSource()); + return mService.setName(name, mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1411,7 +1403,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.getBluetoothClass(resolveAttributionSource()); + return mService.getBluetoothClass(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1467,7 +1459,7 @@ public final class BluetoothAdapter { if (getState() != STATE_ON) return BluetoothAdapter.IO_CAPABILITY_UNKNOWN; try { mServiceLock.readLock().lock(); - if (mService != null) return mService.getIoCapability(resolveAttributionSource()); + if (mService != null) return mService.getIoCapability(mAttributionSource); } catch (RemoteException e) { Log.e(TAG, e.getMessage(), e); } finally { @@ -1520,7 +1512,7 @@ public final class BluetoothAdapter { if (getState() != STATE_ON) return BluetoothAdapter.IO_CAPABILITY_UNKNOWN; try { mServiceLock.readLock().lock(); - if (mService != null) return mService.getLeIoCapability(resolveAttributionSource()); + if (mService != null) return mService.getLeIoCapability(mAttributionSource); } catch (RemoteException e) { Log.e(TAG, e.getMessage(), e); } finally { @@ -1582,7 +1574,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.getScanMode(resolveAttributionSource()); + return mService.getScanMode(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1631,7 +1623,7 @@ public final class BluetoothAdapter { mServiceLock.readLock().lock(); if (mService != null) { int durationSeconds = Math.toIntExact(durationMillis / 1000); - return mService.setScanMode(mode, durationSeconds, resolveAttributionSource()); + return mService.setScanMode(mode, durationSeconds, mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1681,7 +1673,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.setScanMode(mode, getDiscoverableTimeout(), resolveAttributionSource()); + return mService.setScanMode(mode, getDiscoverableTimeout(), mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1702,7 +1694,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.getDiscoverableTimeout(resolveAttributionSource()); + return mService.getDiscoverableTimeout(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1723,7 +1715,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - mService.setDiscoverableTimeout(timeout, resolveAttributionSource()); + mService.setDiscoverableTimeout(timeout, mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1796,7 +1788,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.startDiscovery(resolveAttributionSource()); + return mService.startDiscovery(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1832,7 +1824,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.cancelDiscovery(resolveAttributionSource()); + return mService.cancelDiscovery(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1870,7 +1862,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.isDiscovering(resolveAttributionSource()); + return mService.isDiscovering(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -2307,7 +2299,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.getMaxConnectedAudioDevices(resolveAttributionSource()); + return mService.getMaxConnectedAudioDevices(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "failed to get getMaxConnectedAudioDevices, error: ", e); @@ -2335,7 +2327,7 @@ public final class BluetoothAdapter { // BLE is not supported return false; } - return (iGatt.numHwTrackFiltersAvailable() != 0); + return (iGatt.numHwTrackFiltersAvailable(mAttributionSource) != 0); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -2419,7 +2411,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return mService.getMostRecentlyConnectedDevices(resolveAttributionSource()); + return mService.getMostRecentlyConnectedDevices(mAttributionSource); } } catch (RemoteException e) { Log.e(TAG, "", e); @@ -2449,7 +2441,7 @@ public final class BluetoothAdapter { try { mServiceLock.readLock().lock(); if (mService != null) { - return toDeviceSet(mService.getBondedDevices(resolveAttributionSource())); + return toDeviceSet(mService.getBondedDevices(mAttributionSource)); } return toDeviceSet(new BluetoothDevice[0]); } catch (RemoteException e) { @@ -3171,11 +3163,11 @@ public final class BluetoothAdapter { if (mLeScanClients != null) { mLeScanClients.clear(); } - if (sBluetoothLeAdvertiser != null) { - sBluetoothLeAdvertiser.cleanup(); + if (mBluetoothLeAdvertiser != null) { + mBluetoothLeAdvertiser.cleanup(); } - if (sBluetoothLeScanner != null) { - sBluetoothLeScanner.cleanup(); + if (mBluetoothLeScanner != null) { + mBluetoothLeScanner.cleanup(); } } finally { mServiceLock.writeLock().unlock(); @@ -3514,11 +3506,17 @@ public final class BluetoothAdapter { && (Integer.parseInt(address.split(":")[5], 16) & 0b11) == 0b11; } + /** {@hide} */ @UnsupportedAppUsage - /*package*/ IBluetoothManager getBluetoothManager() { + public IBluetoothManager getBluetoothManager() { return mManagerService; } + /** {@hide} */ + public AttributionSource getAttributionSource() { + return mAttributionSource; + } + private final ArrayList<IBluetoothManagerCallback> mProxyServiceStateCallbacks = new ArrayList<IBluetoothManagerCallback>(); diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index cc2ba9b8ab..5eca947931 100644 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/java/android/bluetooth/BluetoothDevice.java @@ -1167,17 +1167,13 @@ public final class BluetoothDevice implements Parcelable { mAddress = address; mAddressType = ADDRESS_TYPE_PUBLIC; + mAttributionSource = ActivityThread.currentAttributionSource(); } void setAttributionSource(AttributionSource attributionSource) { mAttributionSource = attributionSource; } - private AttributionSource resolveAttributionSource() { - return (mAttributionSource != null) ? mAttributionSource - : ActivityThread.currentAttributionSource(); - } - @Override public boolean equals(@Nullable Object o) { if (o instanceof BluetoothDevice) { @@ -1268,7 +1264,7 @@ public final class BluetoothDevice implements Parcelable { return null; } try { - String name = service.getRemoteName(this, resolveAttributionSource()); + String name = service.getRemoteName(this, mAttributionSource); if (name != null) { // remove whitespace characters from the name return name @@ -1299,7 +1295,7 @@ public final class BluetoothDevice implements Parcelable { return DEVICE_TYPE_UNKNOWN; } try { - return service.getRemoteType(this, resolveAttributionSource()); + return service.getRemoteType(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1323,7 +1319,7 @@ public final class BluetoothDevice implements Parcelable { return null; } try { - String alias = service.getRemoteAliasWithAttribution(this, resolveAttributionSource()); + String alias = service.getRemoteAliasWithAttribution(this, mAttributionSource); if (alias == null) { return getName(); } @@ -1365,8 +1361,8 @@ public final class BluetoothDevice implements Parcelable { } try { return service.setRemoteAlias(this, alias, - resolveAttributionSource().getPackageName(), - resolveAttributionSource()); + mAttributionSource.getPackageName(), + mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1392,7 +1388,7 @@ public final class BluetoothDevice implements Parcelable { return BATTERY_LEVEL_BLUETOOTH_OFF; } try { - return service.getBatteryLevel(this, resolveAttributionSource()); + return service.getBatteryLevel(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1483,7 +1479,7 @@ public final class BluetoothDevice implements Parcelable { } try { return service.createBond( - this, transport, remoteP192Data, remoteP256Data, resolveAttributionSource()); + this, transport, remoteP192Data, remoteP256Data, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1508,7 +1504,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.isBondingInitiatedLocally(this, resolveAttributionSource()); + return service.isBondingInitiatedLocally(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1533,7 +1529,7 @@ public final class BluetoothDevice implements Parcelable { Log.i(TAG, "cancelBondProcess() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); - return service.cancelBondProcess(this, resolveAttributionSource()); + return service.cancelBondProcess(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1561,7 +1557,7 @@ public final class BluetoothDevice implements Parcelable { Log.i(TAG, "removeBond() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); - return service.removeBond(this, resolveAttributionSource()); + return service.removeBond(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1577,7 +1573,7 @@ public final class BluetoothDevice implements Parcelable { @SuppressLint("AndroidFrameworkRequiresPermission") protected Integer recompute(BluetoothDevice query) { try { - return sService.getBondState(query, resolveAttributionSource()); + return sService.getBondState(query, mAttributionSource); } catch (RemoteException e) { throw e.rethrowAsRuntimeException(); } @@ -1667,7 +1663,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.getConnectionStateWithAttribution(this, resolveAttributionSource()) + return service.getConnectionStateWithAttribution(this, mAttributionSource) != CONNECTION_STATE_DISCONNECTED; } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1693,7 +1689,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.getConnectionStateWithAttribution(this, resolveAttributionSource()) + return service.getConnectionStateWithAttribution(this, mAttributionSource) > CONNECTION_STATE_CONNECTED; } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1716,7 +1712,7 @@ public final class BluetoothDevice implements Parcelable { return null; } try { - int classInt = service.getRemoteClass(this, resolveAttributionSource()); + int classInt = service.getRemoteClass(this, mAttributionSource); if (classInt == BluetoothClass.ERROR) return null; return new BluetoothClass(classInt); } catch (RemoteException e) { @@ -1745,7 +1741,7 @@ public final class BluetoothDevice implements Parcelable { return null; } try { - return service.getRemoteUuids(this, resolveAttributionSource()); + return service.getRemoteUuids(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1775,7 +1771,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.fetchRemoteUuidsWithAttribution(this, resolveAttributionSource()); + return service.fetchRemoteUuidsWithAttribution(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1812,7 +1808,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.sdpSearch(this, uuid, resolveAttributionSource()); + return service.sdpSearch(this, uuid, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1834,7 +1830,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.setPin(this, true, pin.length, pin, resolveAttributionSource()); + return service.setPin(this, true, pin.length, pin, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1897,7 +1893,7 @@ public final class BluetoothDevice implements Parcelable { return false; } try { - return service.cancelBondProcess(this, resolveAttributionSource()); + return service.cancelBondProcess(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1930,7 +1926,7 @@ public final class BluetoothDevice implements Parcelable { return ACCESS_UNKNOWN; } try { - return service.getPhonebookAccessPermission(this, resolveAttributionSource()); + return service.getPhonebookAccessPermission(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -2036,7 +2032,7 @@ public final class BluetoothDevice implements Parcelable { return ACCESS_UNKNOWN; } try { - return service.getMessageAccessPermission(this, resolveAttributionSource()); + return service.getMessageAccessPermission(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -2087,7 +2083,7 @@ public final class BluetoothDevice implements Parcelable { return ACCESS_UNKNOWN; } try { - return service.getSimAccessPermission(this, resolveAttributionSource()); + return service.getSimAccessPermission(this, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -2516,7 +2512,8 @@ public final class BluetoothDevice implements Parcelable { // BLE is not supported return null; } - BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, opportunistic, phy); + BluetoothGatt gatt = new BluetoothGatt( + iGatt, this, transport, opportunistic, phy, mAttributionSource); gatt.connect(autoConnect, callback, handler); return gatt; } catch (RemoteException e) { diff --git a/framework/java/android/bluetooth/BluetoothGatt.java b/framework/java/android/bluetooth/BluetoothGatt.java index 9d3eed8f6f..aea82102ca 100644 --- a/framework/java/android/bluetooth/BluetoothGatt.java +++ b/framework/java/android/bluetooth/BluetoothGatt.java @@ -22,6 +22,7 @@ import android.annotation.SuppressLint; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; import android.compat.annotation.UnsupportedAppUsage; +import android.content.AttributionSource; import android.os.Build; import android.os.Handler; import android.os.ParcelUuid; @@ -69,6 +70,7 @@ public final class BluetoothGatt implements BluetoothProfile { private int mTransport; private int mPhy; private boolean mOpportunistic; + private final AttributionSource mAttributionSource; private static final int AUTH_RETRY_STATE_IDLE = 0; private static final int AUTH_RETRY_STATE_NO_MITM = 1; @@ -198,7 +200,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.clientConnect(mClientIf, mDevice.getAddress(), !mAutoConnect, mTransport, mOpportunistic, - mPhy); // autoConnect is inverse of "isDirect" + mPhy, mAttributionSource); // autoConnect is inverse of "isDirect" } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -376,7 +378,8 @@ public final class BluetoothGatt implements BluetoothProfile { try { final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.readCharacteristic(mClientIf, address, handle, authReq); + mService.readCharacteristic( + mClientIf, address, handle, authReq, mAttributionSource); mAuthRetryState++; return; } catch (RemoteException e) { @@ -439,7 +442,7 @@ public final class BluetoothGatt implements BluetoothProfile { ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; mService.writeCharacteristic(mClientIf, address, handle, characteristic.getWriteType(), authReq, - characteristic.getValue()); + characteristic.getValue(), mAttributionSource); mAuthRetryState++; return; } catch (RemoteException e) { @@ -521,7 +524,8 @@ public final class BluetoothGatt implements BluetoothProfile { try { final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.readDescriptor(mClientIf, address, handle, authReq); + mService.readDescriptor( + mClientIf, address, handle, authReq, mAttributionSource); mAuthRetryState++; return; } catch (RemoteException e) { @@ -573,7 +577,7 @@ public final class BluetoothGatt implements BluetoothProfile { final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; mService.writeDescriptor(mClientIf, address, handle, - authReq, descriptor.getValue()); + authReq, descriptor.getValue(), mAttributionSource); mAuthRetryState++; return; } catch (RemoteException e) { @@ -726,13 +730,14 @@ public final class BluetoothGatt implements BluetoothProfile { } }; - /*package*/ BluetoothGatt(IBluetoothGatt iGatt, BluetoothDevice device, - int transport, boolean opportunistic, int phy) { + /* package */ BluetoothGatt(IBluetoothGatt iGatt, BluetoothDevice device, int transport, + boolean opportunistic, int phy, AttributionSource attributionSource) { mService = iGatt; mDevice = device; mTransport = transport; mPhy = phy; mOpportunistic = opportunistic; + mAttributionSource = attributionSource; mServices = new ArrayList<BluetoothGattService>(); mConnState = CONN_STATE_IDLE; @@ -867,7 +872,8 @@ public final class BluetoothGatt implements BluetoothProfile { if (DBG) Log.d(TAG, "registerApp() - UUID=" + uuid); try { - mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback, eatt_support); + mService.registerClient( + new ParcelUuid(uuid), mBluetoothGattCallback, eatt_support, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -888,7 +894,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mCallback = null; - mService.unregisterClient(mClientIf); + mService.unregisterClient(mClientIf, mAttributionSource); mClientIf = 0; } catch (RemoteException e) { Log.e(TAG, "", e); @@ -958,7 +964,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return; try { - mService.clientDisconnect(mClientIf, mDevice.getAddress()); + mService.clientDisconnect(mClientIf, mDevice.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -977,8 +983,9 @@ public final class BluetoothGatt implements BluetoothProfile { @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public boolean connect() { try { + // autoConnect is inverse of "isDirect" mService.clientConnect(mClientIf, mDevice.getAddress(), false, mTransport, - mOpportunistic, mPhy); // autoConnect is inverse of "isDirect" + mOpportunistic, mPhy, mAttributionSource); return true; } catch (RemoteException e) { Log.e(TAG, "", e); @@ -1009,7 +1016,7 @@ public final class BluetoothGatt implements BluetoothProfile { public void setPreferredPhy(int txPhy, int rxPhy, int phyOptions) { try { mService.clientSetPreferredPhy(mClientIf, mDevice.getAddress(), txPhy, rxPhy, - phyOptions); + phyOptions, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1023,7 +1030,7 @@ public final class BluetoothGatt implements BluetoothProfile { @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public void readPhy() { try { - mService.clientReadPhy(mClientIf, mDevice.getAddress()); + mService.clientReadPhy(mClientIf, mDevice.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1060,7 +1067,7 @@ public final class BluetoothGatt implements BluetoothProfile { mServices.clear(); try { - mService.discoverServices(mClientIf, mDevice.getAddress()); + mService.discoverServices(mClientIf, mDevice.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1087,7 +1094,8 @@ public final class BluetoothGatt implements BluetoothProfile { mServices.clear(); try { - mService.discoverServiceByUuid(mClientIf, mDevice.getAddress(), new ParcelUuid(uuid)); + mService.discoverServiceByUuid( + mClientIf, mDevice.getAddress(), new ParcelUuid(uuid), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1179,7 +1187,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.readCharacteristic(mClientIf, device.getAddress(), - characteristic.getInstanceId(), AUTHENTICATION_NONE); + characteristic.getInstanceId(), AUTHENTICATION_NONE, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mDeviceBusy = false; @@ -1214,7 +1222,8 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.readUsingCharacteristicUuid(mClientIf, mDevice.getAddress(), - new ParcelUuid(uuid), startHandle, endHandle, AUTHENTICATION_NONE); + new ParcelUuid(uuid), startHandle, endHandle, AUTHENTICATION_NONE, + mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mDeviceBusy = false; @@ -1262,7 +1271,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.writeCharacteristic(mClientIf, device.getAddress(), characteristic.getInstanceId(), characteristic.getWriteType(), - AUTHENTICATION_NONE, characteristic.getValue()); + AUTHENTICATION_NONE, characteristic.getValue(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mDeviceBusy = false; @@ -1305,7 +1314,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.readDescriptor(mClientIf, device.getAddress(), - descriptor.getInstanceId(), AUTHENTICATION_NONE); + descriptor.getInstanceId(), AUTHENTICATION_NONE, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mDeviceBusy = false; @@ -1347,7 +1356,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.writeDescriptor(mClientIf, device.getAddress(), descriptor.getInstanceId(), - AUTHENTICATION_NONE, descriptor.getValue()); + AUTHENTICATION_NONE, descriptor.getValue(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mDeviceBusy = false; @@ -1383,7 +1392,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return false; try { - mService.beginReliableWrite(mClientIf, mDevice.getAddress()); + mService.beginReliableWrite(mClientIf, mDevice.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1416,7 +1425,7 @@ public final class BluetoothGatt implements BluetoothProfile { } try { - mService.endReliableWrite(mClientIf, mDevice.getAddress(), true); + mService.endReliableWrite(mClientIf, mDevice.getAddress(), true, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mDeviceBusy = false; @@ -1440,7 +1449,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return; try { - mService.endReliableWrite(mClientIf, mDevice.getAddress(), false); + mService.endReliableWrite(mClientIf, mDevice.getAddress(), false, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1487,7 +1496,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.registerForNotification(mClientIf, device.getAddress(), - characteristic.getInstanceId(), enable); + characteristic.getInstanceId(), enable, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1510,7 +1519,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return false; try { - mService.refreshDevice(mClientIf, mDevice.getAddress()); + mService.refreshDevice(mClientIf, mDevice.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1535,7 +1544,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return false; try { - mService.readRemoteRssi(mClientIf, mDevice.getAddress()); + mService.readRemoteRssi(mClientIf, mDevice.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1567,7 +1576,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return false; try { - mService.configureMTU(mClientIf, mDevice.getAddress(), mtu); + mService.configureMTU(mClientIf, mDevice.getAddress(), mtu, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1599,7 +1608,8 @@ public final class BluetoothGatt implements BluetoothProfile { if (mService == null || mClientIf == 0) return false; try { - mService.connectionParameterUpdate(mClientIf, mDevice.getAddress(), connectionPriority); + mService.connectionParameterUpdate( + mClientIf, mDevice.getAddress(), connectionPriority, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1633,9 +1643,10 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.leConnectionUpdate(mClientIf, mDevice.getAddress(), - minConnectionInterval, maxConnectionInterval, - slaveLatency, supervisionTimeout, - minConnectionEventLen, maxConnectionEventLen); + minConnectionInterval, maxConnectionInterval, + slaveLatency, supervisionTimeout, + minConnectionEventLen, maxConnectionEventLen, + mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; diff --git a/framework/java/android/bluetooth/BluetoothGattServer.java b/framework/java/android/bluetooth/BluetoothGattServer.java index 865f476e78..e28006aac3 100644 --- a/framework/java/android/bluetooth/BluetoothGattServer.java +++ b/framework/java/android/bluetooth/BluetoothGattServer.java @@ -21,6 +21,7 @@ import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; +import android.content.AttributionSource; import android.os.ParcelUuid; import android.os.RemoteException; import android.util.Log; @@ -48,6 +49,7 @@ public final class BluetoothGattServer implements BluetoothProfile { private BluetoothAdapter mAdapter; private IBluetoothGatt mService; private BluetoothGattServerCallback mCallback; + private final AttributionSource mAttributionSource; private Object mServerIfLock = new Object(); private int mServerIf; @@ -382,13 +384,15 @@ public final class BluetoothGattServer implements BluetoothProfile { /** * Create a BluetoothGattServer proxy object. */ - /*package*/ BluetoothGattServer(IBluetoothGatt iGatt, int transport) { + /*package*/ BluetoothGattServer( + IBluetoothGatt iGatt, int transport, AttributionSource attributionSource) { mService = iGatt; mAdapter = BluetoothAdapter.getDefaultAdapter(); mCallback = null; mServerIf = 0; mTransport = transport; mServices = new ArrayList<BluetoothGattService>(); + mAttributionSource = attributionSource; } /** @@ -488,7 +492,8 @@ public final class BluetoothGattServer implements BluetoothProfile { mCallback = callback; try { - mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback, eatt_support); + mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback, + eatt_support, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); mCallback = null; @@ -522,7 +527,7 @@ public final class BluetoothGattServer implements BluetoothProfile { try { mCallback = null; - mService.unregisterServer(mServerIf); + mService.unregisterServer(mServerIf, mAttributionSource); mServerIf = 0; } catch (RemoteException e) { Log.e(TAG, "", e); @@ -576,7 +581,8 @@ public final class BluetoothGattServer implements BluetoothProfile { try { // autoConnect is inverse of "isDirect" - mService.serverConnect(mServerIf, device.getAddress(), !autoConnect, mTransport); + mService.serverConnect( + mServerIf, device.getAddress(), !autoConnect, mTransport, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -599,7 +605,7 @@ public final class BluetoothGattServer implements BluetoothProfile { if (mService == null || mServerIf == 0) return; try { - mService.serverDisconnect(mServerIf, device.getAddress()); + mService.serverDisconnect(mServerIf, device.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -628,7 +634,7 @@ public final class BluetoothGattServer implements BluetoothProfile { public void setPreferredPhy(BluetoothDevice device, int txPhy, int rxPhy, int phyOptions) { try { mService.serverSetPreferredPhy(mServerIf, device.getAddress(), txPhy, rxPhy, - phyOptions); + phyOptions, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -644,7 +650,7 @@ public final class BluetoothGattServer implements BluetoothProfile { @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public void readPhy(BluetoothDevice device) { try { - mService.serverReadPhy(mServerIf, device.getAddress()); + mService.serverReadPhy(mServerIf, device.getAddress(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -679,7 +685,7 @@ public final class BluetoothGattServer implements BluetoothProfile { try { mService.sendResponse(mServerIf, device.getAddress(), requestId, - status, offset, value); + status, offset, value, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -722,7 +728,7 @@ public final class BluetoothGattServer implements BluetoothProfile { try { mService.sendNotification(mServerIf, device.getAddress(), characteristic.getInstanceId(), confirm, - characteristic.getValue()); + characteristic.getValue(), mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -757,7 +763,7 @@ public final class BluetoothGattServer implements BluetoothProfile { mPendingService = service; try { - mService.addService(mServerIf, service); + mService.addService(mServerIf, service, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -784,7 +790,7 @@ public final class BluetoothGattServer implements BluetoothProfile { if (intService == null) return false; try { - mService.removeService(mServerIf, service.getInstanceId()); + mService.removeService(mServerIf, service.getInstanceId(), mAttributionSource); mServices.remove(intService); } catch (RemoteException e) { Log.e(TAG, "", e); @@ -805,7 +811,7 @@ public final class BluetoothGattServer implements BluetoothProfile { if (mService == null || mServerIf == 0) return; try { - mService.clearServices(mServerIf); + mService.clearServices(mServerIf, mAttributionSource); mServices.clear(); } catch (RemoteException e) { Log.e(TAG, "", e); diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 69f9a79c73..07af8dbd23 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -20,8 +20,10 @@ import android.annotation.RequiresFeature; import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; import android.annotation.SystemService; +import android.app.ActivityThread; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; +import android.content.AttributionSource; import android.content.Context; import android.content.pm.PackageManager; import android.os.RemoteException; @@ -29,6 +31,7 @@ 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} @@ -56,16 +59,16 @@ public final class BluetoothManager { private static final String TAG = "BluetoothManager"; private static final boolean DBG = false; + private final AttributionSource mAttributionSource; private final BluetoothAdapter mAdapter; /** * @hide */ public BluetoothManager(Context context) { - mAdapter = BluetoothAdapter.createAdapter(); - if (context != null) { - mAdapter.setAttributionSource(context.getAttributionSource()); - } + mAttributionSource = (context != null) ? context.getAttributionSource() + : ActivityThread.currentAttributionSource(); + mAdapter = BluetoothAdapter.createAdapter(mAttributionSource); } /** @@ -138,7 +141,7 @@ public final class BluetoothManager { if (iGatt == null) return connectedDevices; connectedDevices = iGatt.getDevicesMatchingConnectionStates( - new int[]{BluetoothProfile.STATE_CONNECTED}); + new int[]{BluetoothProfile.STATE_CONNECTED}, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -180,7 +183,8 @@ public final class BluetoothManager { IBluetoothManager managerService = mAdapter.getBluetoothManager(); IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) return devices; - devices = iGatt.getDevicesMatchingConnectionStates(states); + devices = iGatt.getDevicesMatchingConnectionStates( + states, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -283,7 +287,8 @@ public final class BluetoothManager { Log.e(TAG, "Fail to get GATT Server connection"); return null; } - BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport); + BluetoothGattServer mGattServer = + new BluetoothGattServer(iGatt, transport, mAttributionSource); Boolean regStatus = mGattServer.registerCallback(callback, eatt_support); return regStatus ? mGattServer : null; } catch (RemoteException e) { diff --git a/framework/java/android/bluetooth/le/AdvertisingSet.java b/framework/java/android/bluetooth/le/AdvertisingSet.java index d7e48ca543..caa91fb239 100644 --- a/framework/java/android/bluetooth/le/AdvertisingSet.java +++ b/framework/java/android/bluetooth/le/AdvertisingSet.java @@ -18,12 +18,12 @@ package android.bluetooth.le; import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; -import android.annotation.SuppressLint; import android.bluetooth.BluetoothAdapter; import android.bluetooth.IBluetoothGatt; import android.bluetooth.IBluetoothManager; import android.bluetooth.annotations.RequiresBluetoothAdvertisePermission; import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission; +import android.content.AttributionSource; import android.os.RemoteException; import android.util.Log; @@ -40,11 +40,12 @@ public final class AdvertisingSet { private final IBluetoothGatt mGatt; private int mAdvertiserId; + private AttributionSource mAttributionSource; - /* package */ AdvertisingSet(int advertiserId, - IBluetoothManager bluetoothManager) { + /* package */ AdvertisingSet(int advertiserId, IBluetoothManager bluetoothManager, + AttributionSource attributionSource) { mAdvertiserId = advertiserId; - + mAttributionSource = attributionSource; try { mGatt = bluetoothManager.getBluetoothGatt(); } catch (RemoteException e) { @@ -75,7 +76,7 @@ public final class AdvertisingSet { int maxExtendedAdvertisingEvents) { try { mGatt.enableAdvertisingSet(mAdvertiserId, enable, duration, - maxExtendedAdvertisingEvents); + maxExtendedAdvertisingEvents, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } @@ -98,7 +99,7 @@ public final class AdvertisingSet { @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE) public void setAdvertisingData(AdvertiseData advertiseData) { try { - mGatt.setAdvertisingData(mAdvertiserId, advertiseData); + mGatt.setAdvertisingData(mAdvertiserId, advertiseData, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } @@ -118,7 +119,7 @@ public final class AdvertisingSet { @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE) public void setScanResponseData(AdvertiseData scanResponse) { try { - mGatt.setScanResponseData(mAdvertiserId, scanResponse); + mGatt.setScanResponseData(mAdvertiserId, scanResponse, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } @@ -136,7 +137,7 @@ public final class AdvertisingSet { @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE) public void setAdvertisingParameters(AdvertisingSetParameters parameters) { try { - mGatt.setAdvertisingParameters(mAdvertiserId, parameters); + mGatt.setAdvertisingParameters(mAdvertiserId, parameters, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } @@ -152,7 +153,7 @@ public final class AdvertisingSet { @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE) public void setPeriodicAdvertisingParameters(PeriodicAdvertisingParameters parameters) { try { - mGatt.setPeriodicAdvertisingParameters(mAdvertiserId, parameters); + mGatt.setPeriodicAdvertisingParameters(mAdvertiserId, parameters, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } @@ -173,7 +174,7 @@ public final class AdvertisingSet { @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE) public void setPeriodicAdvertisingData(AdvertiseData periodicData) { try { - mGatt.setPeriodicAdvertisingData(mAdvertiserId, periodicData); + mGatt.setPeriodicAdvertisingData(mAdvertiserId, periodicData, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } @@ -191,7 +192,7 @@ public final class AdvertisingSet { @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE) public void setPeriodicAdvertisingEnabled(boolean enable) { try { - mGatt.setPeriodicAdvertisingEnable(mAdvertiserId, enable); + mGatt.setPeriodicAdvertisingEnable(mAdvertiserId, enable, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "remote exception - ", e); } diff --git a/framework/java/android/bluetooth/le/BluetoothLeAdvertiser.java b/framework/java/android/bluetooth/le/BluetoothLeAdvertiser.java index ff279d8598..58029745ab 100644 --- a/framework/java/android/bluetooth/le/BluetoothLeAdvertiser.java +++ b/framework/java/android/bluetooth/le/BluetoothLeAdvertiser.java @@ -26,6 +26,7 @@ import android.bluetooth.IBluetoothGatt; import android.bluetooth.IBluetoothManager; import android.bluetooth.annotations.RequiresBluetoothAdvertisePermission; import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission; +import android.content.AttributionSource; import android.os.Handler; import android.os.Looper; import android.os.ParcelUuid; @@ -35,6 +36,7 @@ import android.util.Log; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * This class provides a way to perform Bluetooth LE advertise operations, such as starting and @@ -58,9 +60,11 @@ public final class BluetoothLeAdvertiser { private static final int FLAGS_FIELD_BYTES = 3; private static final int MANUFACTURER_SPECIFIC_DATA_LENGTH = 2; + private final BluetoothAdapter mBluetoothAdapter; private final IBluetoothManager mBluetoothManager; + private final AttributionSource mAttributionSource; + private final Handler mHandler; - private BluetoothAdapter mBluetoothAdapter; private final Map<AdvertiseCallback, AdvertisingSetCallback> mLegacyAdvertisers = new HashMap<>(); private final Map<AdvertisingSetCallback, IAdvertisingSetCallback> @@ -74,9 +78,10 @@ public final class BluetoothLeAdvertiser { * @param bluetoothManager BluetoothManager that conducts overall Bluetooth Management * @hide */ - public BluetoothLeAdvertiser(IBluetoothManager bluetoothManager) { - mBluetoothManager = bluetoothManager; - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + public BluetoothLeAdvertiser(BluetoothAdapter bluetoothAdapter) { + mBluetoothAdapter = Objects.requireNonNull(bluetoothAdapter); + mBluetoothManager = mBluetoothAdapter.getBluetoothManager(); + mAttributionSource = mBluetoothAdapter.getAttributionSource(); mHandler = new Handler(Looper.getMainLooper()); } @@ -453,7 +458,8 @@ public final class BluetoothLeAdvertiser { try { gatt.startAdvertisingSet(parameters, advertiseData, scanResponse, periodicParameters, - periodicData, duration, maxExtendedAdvertisingEvents, wrapped); + periodicData, duration, maxExtendedAdvertisingEvents, wrapped, + mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "Failed to start advertising set - ", e); postStartSetFailure(handler, callback, @@ -482,7 +488,7 @@ public final class BluetoothLeAdvertiser { IBluetoothGatt gatt; try { gatt = mBluetoothManager.getBluetoothGatt(); - gatt.stopAdvertisingSet(wrapped); + gatt.stopAdvertisingSet(wrapped, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "Failed to stop advertising - ", e); } @@ -600,8 +606,8 @@ public final class BluetoothLeAdvertiser { return; } - AdvertisingSet advertisingSet = - new AdvertisingSet(advertiserId, mBluetoothManager); + AdvertisingSet advertisingSet = new AdvertisingSet( + advertiserId, mBluetoothManager, mAttributionSource); mAdvertisingSets.put(advertiserId, advertisingSet); callback.onAdvertisingSetStarted(advertisingSet, txPower, status); } diff --git a/framework/java/android/bluetooth/le/BluetoothLeScanner.java b/framework/java/android/bluetooth/le/BluetoothLeScanner.java index f27f22b9af..60d4e2d6d2 100644 --- a/framework/java/android/bluetooth/le/BluetoothLeScanner.java +++ b/framework/java/android/bluetooth/le/BluetoothLeScanner.java @@ -41,6 +41,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** * This class provides methods to perform scan related operations for Bluetooth LE devices. An @@ -80,11 +81,12 @@ public final class BluetoothLeScanner { */ public static final String EXTRA_CALLBACK_TYPE = "android.bluetooth.le.extra.CALLBACK_TYPE"; + private final BluetoothAdapter mBluetoothAdapter; private final IBluetoothManager mBluetoothManager; + private final AttributionSource mAttributionSource; + private final Handler mHandler; - private BluetoothAdapter mBluetoothAdapter; private final Map<ScanCallback, BleScanCallbackWrapper> mLeScanClients; - private final AttributionSource mAttributionSource; /** * Use {@link BluetoothAdapter#getBluetoothLeScanner()} instead. @@ -94,13 +96,12 @@ public final class BluetoothLeScanner { * @param featureId The featureId of the context this object was created from * @hide */ - public BluetoothLeScanner(IBluetoothManager bluetoothManager, - @NonNull AttributionSource attributionSource) { - mBluetoothManager = bluetoothManager; - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + public BluetoothLeScanner(BluetoothAdapter bluetoothAdapter) { + mBluetoothAdapter = Objects.requireNonNull(bluetoothAdapter); + mBluetoothManager = mBluetoothAdapter.getBluetoothManager(); + mAttributionSource = mBluetoothAdapter.getAttributionSource(); mHandler = new Handler(Looper.getMainLooper()); mLeScanClients = new HashMap<ScanCallback, BleScanCallbackWrapper>(); - mAttributionSource = attributionSource; } /** @@ -276,7 +277,8 @@ public final class BluetoothLeScanner { wrapper.startRegistration(); } else { try { - gatt.startScanForIntent(callbackIntent, settings, filters, mAttributionSource); + gatt.startScanForIntent(callbackIntent, settings, filters, + mAttributionSource); } catch (RemoteException e) { return ScanCallback.SCAN_FAILED_INTERNAL_ERROR; } @@ -321,7 +323,7 @@ public final class BluetoothLeScanner { IBluetoothGatt gatt; try { gatt = mBluetoothManager.getBluetoothGatt(); - gatt.stopScanForIntent(callbackIntent); + gatt.stopScanForIntent(callbackIntent, mAttributionSource); } catch (RemoteException e) { } } @@ -420,7 +422,7 @@ public final class BluetoothLeScanner { // Scan stopped. if (mScannerId == -1 || mScannerId == -2) return; try { - mBluetoothGatt.registerScanner(this, mWorkSource); + mBluetoothGatt.registerScanner(this, mWorkSource, mAttributionSource); wait(REGISTRATION_CALLBACK_TIMEOUT_MILLIS); } catch (InterruptedException | RemoteException e) { Log.e(TAG, "application registeration exception", e); @@ -450,8 +452,8 @@ public final class BluetoothLeScanner { return; } try { - mBluetoothGatt.stopScan(mScannerId); - mBluetoothGatt.unregisterScanner(mScannerId); + mBluetoothGatt.stopScan(mScannerId, mAttributionSource); + mBluetoothGatt.unregisterScanner(mScannerId, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "Failed to stop scan and unregister", e); } @@ -467,7 +469,7 @@ public final class BluetoothLeScanner { return; } try { - mBluetoothGatt.flushPendingBatchResults(mScannerId); + mBluetoothGatt.flushPendingBatchResults(mScannerId, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "Failed to get pending scan results", e); } @@ -486,7 +488,7 @@ public final class BluetoothLeScanner { try { if (mScannerId == -1) { // Registration succeeds after timeout, unregister scanner. - mBluetoothGatt.unregisterScanner(scannerId); + mBluetoothGatt.unregisterScanner(scannerId, mAttributionSource); } else { mScannerId = scannerId; mBluetoothGatt.startScan(mScannerId, mSettings, mFilters, diff --git a/framework/java/android/bluetooth/le/PeriodicAdvertisingManager.java b/framework/java/android/bluetooth/le/PeriodicAdvertisingManager.java index 26978e3980..47f47bb8e3 100644 --- a/framework/java/android/bluetooth/le/PeriodicAdvertisingManager.java +++ b/framework/java/android/bluetooth/le/PeriodicAdvertisingManager.java @@ -25,6 +25,7 @@ import android.bluetooth.IBluetoothManager; import android.bluetooth.annotations.RequiresBluetoothLocationPermission; import android.bluetooth.annotations.RequiresBluetoothScanPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission; +import android.content.AttributionSource; import android.os.Handler; import android.os.Looper; import android.os.RemoteException; @@ -32,6 +33,7 @@ import android.util.Log; import java.util.IdentityHashMap; import java.util.Map; +import java.util.Objects; /** * This class provides methods to perform periodic advertising related @@ -54,8 +56,9 @@ public final class PeriodicAdvertisingManager { private static final int SYNC_STARTING = -1; + private final BluetoothAdapter mBluetoothAdapter; private final IBluetoothManager mBluetoothManager; - private BluetoothAdapter mBluetoothAdapter; + private final AttributionSource mAttributionSource; /* maps callback, to callback wrapper and sync handle */ Map<PeriodicAdvertisingCallback, @@ -67,9 +70,10 @@ public final class PeriodicAdvertisingManager { * @param bluetoothManager BluetoothManager that conducts overall Bluetooth Management. * @hide */ - public PeriodicAdvertisingManager(IBluetoothManager bluetoothManager) { - mBluetoothManager = bluetoothManager; - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + public PeriodicAdvertisingManager(BluetoothAdapter bluetoothAdapter) { + mBluetoothAdapter = Objects.requireNonNull(bluetoothAdapter); + mBluetoothManager = mBluetoothAdapter.getBluetoothManager(); + mAttributionSource = mBluetoothAdapter.getAttributionSource(); mCallbackWrappers = new IdentityHashMap<>(); } @@ -166,7 +170,8 @@ public final class PeriodicAdvertisingManager { mCallbackWrappers.put(callback, wrapped); try { - gatt.registerSync(scanResult, skip, timeout, wrapped); + gatt.registerSync( + scanResult, skip, timeout, wrapped, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "Failed to register sync - ", e); return; @@ -202,7 +207,7 @@ public final class PeriodicAdvertisingManager { } try { - gatt.unregisterSync(wrapper); + gatt.unregisterSync(wrapper, mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "Failed to cancel sync creation - ", e); return; |
