From 9afa274491ca272dc30f6f6211ef2719b7a2226e Mon Sep 17 00:00:00 2001 From: Matthew Xie Date: Fri, 1 Mar 2013 18:41:02 -0800 Subject: Unhide Bluetooth Low Energy public APIs Updated API headers. Add BluetoothManager to be retrieved by context.getSystemService(Context.BLUETOOTH_SERVICE). LE scan functions are placed in BluetoothAdapter The GATT API are device driven instead of a profile-driver. bug 8450158 Change-Id: I424a4cedaac3ef8120a05996500008dd210d2553 --- .../java/android/bluetooth/BluetoothManager.java | 219 +++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 framework/java/android/bluetooth/BluetoothManager.java (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java new file mode 100644 index 0000000000..19083b55da --- /dev/null +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.bluetooth; + +import android.content.Context; +import android.os.RemoteException; +import android.util.Log; + +import java.util.ArrayList; +import java.util.List; + +/** + * High level manager used to obtain an instance of an {@link BluetoothAdapter} + * and to conduct overall Bluetooth Management. + *

+ * Use {@link android.content.Context#getSystemService(java.lang.String)} + * with {@link Context#BLUETOOTH_SERVICE} to create an {@link BluetoothManager}, + * then call {@link #getAdapter} to obtain the {@link BluetoothAdapter}. + *

+ * Alternately, you can just call the static helper + * {@link BluetoothAdapter#getDefaultAdapter()}. + * + *

+ *

Developer Guides

+ *

For more information about using BLUETOOTH, read the + * Bluetooth developer guide.

+ *
+ * + * @see Context#getSystemService + * @see BluetoothAdapter#getDefaultAdapter() + */ +public final class BluetoothManager { + private static final String TAG = "BluetoothManager"; + private static final boolean DBG = true; + private static final boolean VDBG = true; + + private final BluetoothAdapter mAdapter; + + /** + * @hide + */ + public BluetoothManager(Context context) { + context = context.getApplicationContext(); + if (context == null) { + throw new IllegalArgumentException( + "context not associated with any application (using a mock context?)"); + } + // Legacy api - getDefaultAdapter does not take in the context + mAdapter = BluetoothAdapter.getDefaultAdapter(); + } + + /** + * Get the default BLUETOOTH Adapter for this device. + * + * @return the default BLUETOOTH Adapter + */ + public BluetoothAdapter getAdapter() { + return mAdapter; + } + + /** + * Get the current connection state of the profile to the remote device. + * + *

This is not specific to any application configuration but represents + * the connection state of the local Bluetooth adapter for certain profile. + * This can be used by applications like status bar which would just like + * to know the state of Bluetooth. + * + *

Requires {@link android.Manifest.permission#BLUETOOTH} permission. + * + * @param device Remote bluetooth device. + * @param profile GATT or GATT_SERVER + * @return State of the profile connection. One of + * {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, + * {@link BluetoothProfile#STATE_DISCONNECTED}, + * {@link BluetoothProfile#STATE_DISCONNECTING} + */ + public int getConnectionState(BluetoothDevice device, int profile) { + if (DBG) Log.d(TAG,"getConnectionState()"); + + List connectedDevices = getConnectedDevices(profile); + for(BluetoothDevice connectedDevice : connectedDevices) { + if (device.equals(connectedDevice)) { + return BluetoothProfile.STATE_CONNECTED; + } + } + + return BluetoothProfile.STATE_DISCONNECTED; + } + + /** + * Get connected devices for the specified profile. + * + *

Return the set of devices which are in state {@link BluetoothProfile#STATE_CONNECTED} + * + *

This is not specific to any application configuration but represents + * the connection state of Bluetooth for this profile. + * This can be used by applications like status bar which would just like + * to know the state of Bluetooth. + * + *

Requires {@link android.Manifest.permission#BLUETOOTH} permission. + * + * @param profile GATT or GATT_SERVER + * @return List of devices. The list will be empty on error. + */ + public List 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 connectedDevices = new ArrayList(); + + try { + IBluetoothManager managerService = mAdapter.getBluetoothManager(); + IBluetoothGatt iGatt = (IBluetoothGatt) managerService.getBluetoothGatt(); + if (iGatt == null) return connectedDevices; + + connectedDevices = iGatt.getDevicesMatchingConnectionStates( + new int[] { BluetoothProfile.STATE_CONNECTED }); + } catch (RemoteException e) { + Log.e(TAG,"",e); + } + + return connectedDevices; + } + + /** + * + * Get a list of devices that match any of the given connection + * states. + * + *

If none of the devices match any of the given states, + * an empty list will be returned. + * + *

This is not specific to any application configuration but represents + * the connection state of the local Bluetooth adapter for this profile. + * This can be used by applications like status bar which would just like + * to know the state of the local adapter. + * + *

Requires {@link android.Manifest.permission#BLUETOOTH} permission. + * + * @param profile GATT or GATT_SERVER + * @param states Array of states. States can be one of + * {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, + * {@link BluetoothProfile#STATE_DISCONNECTED}, + * {@link BluetoothProfile#STATE_DISCONNECTING}, + * @return List of devices. The list will be empty on error. + */ + public List getDevicesMatchingConnectionStates(int profile, int[] states) { + if (DBG) Log.d(TAG,"getDevicesMatchingConnectionStates"); + + if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { + throw new IllegalArgumentException("Profile not supported: " + profile); + } + + List devices = new ArrayList(); + + try { + IBluetoothManager managerService = mAdapter.getBluetoothManager(); + IBluetoothGatt iGatt = (IBluetoothGatt) managerService.getBluetoothGatt(); + if (iGatt == null) return devices; + devices = iGatt.getDevicesMatchingConnectionStates(states); + } catch (RemoteException e) { + Log.e(TAG,"",e); + } + + return devices; + } + + /** + * Open a GATT Server + * The callback is used to deliver results to Caller, such as connection status as well + * as the results of any other GATT server operations. + * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer + * to conduct GATT server operations. + * @param context App context + * @param callback GATT server callback handler that will receive asynchronous callbacks. + * @return BluetoothGattServer instance + */ + public BluetoothGattServer openGattServer(Context context, + BluetoothGattServerCallback callback) { + if (context == null || callback == null) { + throw new IllegalArgumentException("null parameter: " + context + " " + callback); + } + + // TODO(Bluetooth) check whether platform support BLE + // Do the check here or in GattServer? + + try { + IBluetoothManager managerService = mAdapter.getBluetoothManager(); + IBluetoothGatt iGatt = (IBluetoothGatt) managerService.getBluetoothGatt(); + if (iGatt == null) { + Log.e(TAG, "Fail to get GATT Server connection"); + return null; + } + BluetoothGattServer mGattServer = new BluetoothGattServer(context, iGatt); + Boolean regStatus = mGattServer.registerCallback(callback); + return regStatus? mGattServer : null; + } catch (RemoteException e) { + Log.e(TAG,"",e); + return null; + } + } +} -- cgit v1.2.3 From 01e8e2ac8e6994921b811f741b2d4dbe99b50b19 Mon Sep 17 00:00:00 2001 From: Matthew Xie Date: Thu, 11 Apr 2013 16:36:26 -0700 Subject: Remove BluetoothAdapterCallback. Simplify leScan Api App does not need to explicitly register/unregister callback bug 8599881 Change-Id: I18cfef14d7ddb344722945e657dcb959823b412b --- framework/java/android/bluetooth/BluetoothManager.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 19083b55da..172f3bcdef 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -127,7 +127,7 @@ public final class BluetoothManager { try { IBluetoothManager managerService = mAdapter.getBluetoothManager(); - IBluetoothGatt iGatt = (IBluetoothGatt) managerService.getBluetoothGatt(); + IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) return connectedDevices; connectedDevices = iGatt.getDevicesMatchingConnectionStates( @@ -172,7 +172,7 @@ public final class BluetoothManager { try { IBluetoothManager managerService = mAdapter.getBluetoothManager(); - IBluetoothGatt iGatt = (IBluetoothGatt) managerService.getBluetoothGatt(); + IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) return devices; devices = iGatt.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { @@ -203,7 +203,7 @@ public final class BluetoothManager { try { IBluetoothManager managerService = mAdapter.getBluetoothManager(); - IBluetoothGatt iGatt = (IBluetoothGatt) managerService.getBluetoothGatt(); + IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) { Log.e(TAG, "Fail to get GATT Server connection"); return null; -- cgit v1.2.3 From ec661918b6b6ac36905fceb26043633f3e8a6a21 Mon Sep 17 00:00:00 2001 From: Ganesh Ganapathi Batta Date: Fri, 18 Apr 2014 10:00:40 -0700 Subject: Add transport param to Connect APIs Support for passing preferred transport for GATT connections as part of Connect APIs Change-Id: I93938dce519b8fa12de41d7e8690dc9355ce2dc5 --- .../java/android/bluetooth/BluetoothManager.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 172f3bcdef..b1618cf328 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -194,6 +194,26 @@ public final class BluetoothManager { */ public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback) { + + return (openGattServer (context, callback, BluetoothDevice.TRANSPORT_AUTO)); + } + + /** + * Open a GATT Server + * The callback is used to deliver results to Caller, such as connection status as well + * as the results of any other GATT server operations. + * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer + * to conduct GATT server operations. + * @param context App context + * @param callback GATT server callback handler that will receive asynchronous callbacks. + * @param transport preferred transport for GATT connections to remote dual-mode devices + * {@link BluetoothDevice#TRANSPORT_AUTO} or + * {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE} + * @return BluetoothGattServer instance + * @hide + */ + public BluetoothGattServer openGattServer(Context context, + BluetoothGattServerCallback callback,int transport) { if (context == null || callback == null) { throw new IllegalArgumentException("null parameter: " + context + " " + callback); } @@ -208,7 +228,7 @@ public final class BluetoothManager { Log.e(TAG, "Fail to get GATT Server connection"); return null; } - BluetoothGattServer mGattServer = new BluetoothGattServer(context, iGatt); + BluetoothGattServer mGattServer = new BluetoothGattServer(context, iGatt,transport); Boolean regStatus = mGattServer.registerCallback(callback); return regStatus? mGattServer : null; } catch (RemoteException e) { -- cgit v1.2.3 From 27ce6cfbe105e5c36e19b1f76bc9b8f29710b331 Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Thu, 23 Apr 2015 17:10:21 -0700 Subject: Add bluetooth permission annotations Change-Id: I5bc86f8ec6ea5c873f1e14dab0e0c47c5c9df7f7 --- framework/java/android/bluetooth/BluetoothManager.java | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index b1618cf328..e355a1c0a5 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -16,6 +16,8 @@ package android.bluetooth; +import android.Manifest; +import android.annotation.RequiresPermission; import android.content.Context; import android.os.RemoteException; import android.util.Log; @@ -89,6 +91,7 @@ public final class BluetoothManager { * {@link BluetoothProfile#STATE_DISCONNECTED}, * {@link BluetoothProfile#STATE_DISCONNECTING} */ + @RequiresPermission(Manifest.permission.BLUETOOTH) public int getConnectionState(BluetoothDevice device, int profile) { if (DBG) Log.d(TAG,"getConnectionState()"); @@ -117,6 +120,7 @@ public final class BluetoothManager { * @param profile GATT or GATT_SERVER * @return List of devices. The list will be empty on error. */ + @RequiresPermission(Manifest.permission.BLUETOOTH) public List getConnectedDevices(int profile) { if (DBG) Log.d(TAG,"getConnectedDevices"); if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { @@ -161,6 +165,7 @@ public final class BluetoothManager { * {@link BluetoothProfile#STATE_DISCONNECTING}, * @return List of devices. The list will be empty on error. */ + @RequiresPermission(Manifest.permission.BLUETOOTH) public List getDevicesMatchingConnectionStates(int profile, int[] states) { if (DBG) Log.d(TAG,"getDevicesMatchingConnectionStates"); -- cgit v1.2.3 From d684c548283f89b8df6e2bd213b16b76053b5aa0 Mon Sep 17 00:00:00 2001 From: Ajay Panicker Date: Thu, 31 Mar 2016 14:14:27 -0700 Subject: Get name and address without enabling Bluetooth Bug: 27665077 Change-Id: I2a06c1a65f22b2f9095a859f5bdd39d4626da165 --- framework/java/android/bluetooth/BluetoothManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index e355a1c0a5..35437a1fd7 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -144,7 +144,7 @@ public final class BluetoothManager { } /** - * + * * Get a list of devices that match any of the given connection * states. * -- cgit v1.2.3 From e2cf867851868c34fac467aa356ae59c6649c85e Mon Sep 17 00:00:00 2001 From: Hemal Patel Date: Wed, 17 Aug 2016 13:18:14 -0700 Subject: Docs: Fixed the Bluetooth guide link Fixed the link that points to the Bluetooth guide. Bug: 29268546 Change-Id: I51c48cebf45c78481f8853a93ff7bcd8483d69ba --- framework/java/android/bluetooth/BluetoothManager.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 35437a1fd7..00058a9790 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -38,8 +38,11 @@ import java.util.List; * *

*

Developer Guides

- *

For more information about using BLUETOOTH, read the - * Bluetooth developer guide.

+ *

+ * For more information about using BLUETOOTH, read the Bluetooth developer + * guide. + *

*
* * @see Context#getSystemService -- cgit v1.2.3 From 59d15654a5ab54a9f1a228152e3d6d829b108a2b Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Tue, 27 Sep 2016 14:34:33 -0700 Subject: Remove unused mContext from BluetoothGatt[Server]. All that this member variable is doing right now is leaking a reference to a context without any benefit. Bug: 31752040 Bug: 31710795 Change-Id: If2241422533318b866340e8dcc9f5fbd9518349c --- framework/java/android/bluetooth/BluetoothManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 00058a9790..29283e793c 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -236,7 +236,7 @@ public final class BluetoothManager { Log.e(TAG, "Fail to get GATT Server connection"); return null; } - BluetoothGattServer mGattServer = new BluetoothGattServer(context, iGatt,transport); + BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt,transport); Boolean regStatus = mGattServer.registerCallback(callback); return regStatus? mGattServer : null; } catch (RemoteException e) { -- cgit v1.2.3 From fbe8b2299f543aee40d44794f7e68a0abbea112c Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 21 Apr 2017 16:29:27 -0600 Subject: More auto-doc work. Add support for AnyThread, CallSuper, and UiThread. Another related CL started documenting @RequiresPermission, so remove duplicated information in existing APIs. Suppress auto-doc on a handful of classes that are already well-documented. Test: make -j32 offline-sdk-docs Bug: 37526420 Change-Id: I791437dccec0f11d5349a23b982ba098cb551af8 --- framework/java/android/bluetooth/BluetoothManager.java | 6 ------ 1 file changed, 6 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 29283e793c..c7191ba263 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -85,8 +85,6 @@ public final class BluetoothManager { * This can be used by applications like status bar which would just like * to know the state of Bluetooth. * - *

Requires {@link android.Manifest.permission#BLUETOOTH} permission. - * * @param device Remote bluetooth device. * @param profile GATT or GATT_SERVER * @return State of the profile connection. One of @@ -118,8 +116,6 @@ public final class BluetoothManager { * This can be used by applications like status bar which would just like * to know the state of Bluetooth. * - *

Requires {@link android.Manifest.permission#BLUETOOTH} permission. - * * @param profile GATT or GATT_SERVER * @return List of devices. The list will be empty on error. */ @@ -159,8 +155,6 @@ public final class BluetoothManager { * This can be used by applications like status bar which would just like * to know the state of the local adapter. * - *

Requires {@link android.Manifest.permission#BLUETOOTH} permission. - * * @param profile GATT or GATT_SERVER * @param states Array of states. States can be one of * {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, -- cgit v1.2.3 From a9fdcc96dcbcba072896a8440d1558677911dabd Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 2 Jun 2017 17:36:26 -0600 Subject: Annotate @SystemApi with required permissions. Most @SystemApi methods should be protected with system (or higher) permissions, so annotate common methods with @RequiresPermission to make automatic verification easier. Verification is really only relevant when calling into system services (where permissions checking can happen on the other side of a Binder call), so annotate managers with the new @SystemService annotation, which is now automatically documented. This is purely a docs change; no logic changes are being made. Test: make -j32 update-api && make -j32 offline-sdk-docs Bug: 62263906 Change-Id: I2554227202d84465676aa4ab0dd336b5c45fc651 --- framework/java/android/bluetooth/BluetoothManager.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index c7191ba263..e2fa38a930 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -18,6 +18,7 @@ package android.bluetooth; import android.Manifest; import android.annotation.RequiresPermission; +import android.annotation.SystemService; import android.content.Context; import android.os.RemoteException; import android.util.Log; @@ -48,6 +49,7 @@ import java.util.List; * @see Context#getSystemService * @see BluetoothAdapter#getDefaultAdapter() */ +@SystemService(Context.BLUETOOTH_SERVICE) public final class BluetoothManager { private static final String TAG = "BluetoothManager"; private static final boolean DBG = true; -- cgit v1.2.3 From 36354fb05c282dcb0e4b27bd4422fa424b7bc032 Mon Sep 17 00:00:00 2001 From: Marie Janssen Date: Thu, 12 Jan 2017 16:00:30 -0800 Subject: Bluetooth: minor documentation fix to de-confuse It was somewhat unclear which is the preferred method of retrieving a BluetoothAdapter. Make it clear that using BluetoothManager is preferred, and remove references to the old method in BluetoothManager docs since it is only avaialable in API 18 or higher. Test: recompile, check that documentation is updated Fix: 33355430 Change-Id: Ia20b4e45dca03bc2f13c2ab477799b89c5e14f45 --- framework/java/android/bluetooth/BluetoothManager.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 29283e793c..4c21aae13e 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -32,10 +32,7 @@ import java.util.List; * Use {@link android.content.Context#getSystemService(java.lang.String)} * with {@link Context#BLUETOOTH_SERVICE} to create an {@link BluetoothManager}, * then call {@link #getAdapter} to obtain the {@link BluetoothAdapter}. - *

- * Alternately, you can just call the static helper - * {@link BluetoothAdapter#getDefaultAdapter()}. - * + *

*
*

Developer Guides

*

-- cgit v1.2.3 From 910201beb0bde1dcf6b33e4ec5d1eb60042419d8 Mon Sep 17 00:00:00 2001 From: Jack He Date: Tue, 22 Aug 2017 16:06:54 -0700 Subject: Fix checkstyle errors (1/2) * Automatic style corrections through IDE Bug: 63596319 Test: make checkbuild, no manual changes, no functional changes Change-Id: I2397d55abc34c9b7a9b748bec6137778df3421a7 --- .../java/android/bluetooth/BluetoothManager.java | 49 +++++++++++----------- 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index bacce80062..7e3bb05fe0 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -86,17 +86,16 @@ public final class BluetoothManager { * * @param device Remote bluetooth device. * @param profile GATT or GATT_SERVER - * @return State of the profile connection. One of - * {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, - * {@link BluetoothProfile#STATE_DISCONNECTED}, - * {@link BluetoothProfile#STATE_DISCONNECTING} + * @return State of the profile connection. One of {@link BluetoothProfile#STATE_CONNECTED}, + * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, + * {@link BluetoothProfile#STATE_DISCONNECTING} */ @RequiresPermission(Manifest.permission.BLUETOOTH) public int getConnectionState(BluetoothDevice device, int profile) { - if (DBG) Log.d(TAG,"getConnectionState()"); + if (DBG) Log.d(TAG, "getConnectionState()"); List connectedDevices = getConnectedDevices(profile); - for(BluetoothDevice connectedDevice : connectedDevices) { + for (BluetoothDevice connectedDevice : connectedDevices) { if (device.equals(connectedDevice)) { return BluetoothProfile.STATE_CONNECTED; } @@ -120,7 +119,7 @@ public final class BluetoothManager { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public List getConnectedDevices(int profile) { - if (DBG) Log.d(TAG,"getConnectedDevices"); + if (DBG) Log.d(TAG, "getConnectedDevices"); if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { throw new IllegalArgumentException("Profile not supported: " + profile); } @@ -133,16 +132,15 @@ public final class BluetoothManager { if (iGatt == null) return connectedDevices; connectedDevices = iGatt.getDevicesMatchingConnectionStates( - new int[] { BluetoothProfile.STATE_CONNECTED }); + new int[]{BluetoothProfile.STATE_CONNECTED}); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } return connectedDevices; } /** - * * Get a list of devices that match any of the given connection * states. * @@ -155,15 +153,14 @@ public final class BluetoothManager { * to know the state of the local adapter. * * @param profile GATT or GATT_SERVER - * @param states Array of states. States can be one of - * {@link BluetoothProfile#STATE_CONNECTED}, {@link BluetoothProfile#STATE_CONNECTING}, - * {@link BluetoothProfile#STATE_DISCONNECTED}, - * {@link BluetoothProfile#STATE_DISCONNECTING}, + * @param states Array of states. States can be one of {@link BluetoothProfile#STATE_CONNECTED}, + * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, + * {@link BluetoothProfile#STATE_DISCONNECTING}, * @return List of devices. The list will be empty on error. */ @RequiresPermission(Manifest.permission.BLUETOOTH) public List getDevicesMatchingConnectionStates(int profile, int[] states) { - if (DBG) Log.d(TAG,"getDevicesMatchingConnectionStates"); + if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates"); if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { throw new IllegalArgumentException("Profile not supported: " + profile); @@ -177,7 +174,7 @@ public final class BluetoothManager { if (iGatt == null) return devices; devices = iGatt.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } return devices; @@ -189,14 +186,15 @@ public final class BluetoothManager { * as the results of any other GATT server operations. * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer * to conduct GATT server operations. + * * @param context App context * @param callback GATT server callback handler that will receive asynchronous callbacks. * @return BluetoothGattServer instance */ public BluetoothGattServer openGattServer(Context context, - BluetoothGattServerCallback callback) { + BluetoothGattServerCallback callback) { - return (openGattServer (context, callback, BluetoothDevice.TRANSPORT_AUTO)); + return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO)); } /** @@ -205,16 +203,17 @@ public final class BluetoothManager { * as the results of any other GATT server operations. * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer * to conduct GATT server operations. + * * @param context App context * @param callback GATT server callback handler that will receive asynchronous callbacks. - * @param transport preferred transport for GATT connections to remote dual-mode devices - * {@link BluetoothDevice#TRANSPORT_AUTO} or - * {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE} + * @param transport preferred transport for GATT connections to remote dual-mode devices {@link + * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link + * BluetoothDevice#TRANSPORT_LE} * @return BluetoothGattServer instance * @hide */ public BluetoothGattServer openGattServer(Context context, - BluetoothGattServerCallback callback,int transport) { + BluetoothGattServerCallback callback, int transport) { if (context == null || callback == null) { throw new IllegalArgumentException("null parameter: " + context + " " + callback); } @@ -229,11 +228,11 @@ 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); Boolean regStatus = mGattServer.registerCallback(callback); - return regStatus? mGattServer : null; + return regStatus ? mGattServer : null; } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return null; } } -- cgit v1.2.3 From 7f827c29ebf892754bf4103d02cad3122e6b3946 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 16 Feb 2018 10:14:57 -0700 Subject: Add RequiresFeature annotation. Certain APIs require that a device have a specific feature to operate correctly, so start annotating them. Test: builds, boots Bug: 72284763 Change-Id: Ie2f30284bdfdb6acc4067f434eba3b5433837606 Exempt-From-Owner-Approval: simple annotations --- framework/java/android/bluetooth/BluetoothManager.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 7e3bb05fe0..11f8ab7551 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -17,9 +17,11 @@ package android.bluetooth; import android.Manifest; +import android.annotation.RequiresFeature; import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.content.Context; +import android.content.pm.PackageManager; import android.os.RemoteException; import android.util.Log; @@ -47,6 +49,7 @@ import java.util.List; * @see BluetoothAdapter#getDefaultAdapter() */ @SystemService(Context.BLUETOOTH_SERVICE) +@RequiresFeature(PackageManager.FEATURE_BLUETOOTH) public final class BluetoothManager { private static final String TAG = "BluetoothManager"; private static final boolean DBG = true; -- cgit v1.2.3 From 61d17c8019f0a506bde114f87761475ead26e319 Mon Sep 17 00:00:00 2001 From: Myles Watson Date: Wed, 14 Nov 2018 14:40:14 -0800 Subject: BluetoothManager: Set DBG to false Fixes: 71491860 Test: Pair and connect with a phone Change-Id: I3ead8154765267105c74cd800f571933bbe82e07 --- framework/java/android/bluetooth/BluetoothManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 11f8ab7551..e3672a7e06 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -52,8 +52,7 @@ import java.util.List; @RequiresFeature(PackageManager.FEATURE_BLUETOOTH) public final class BluetoothManager { private static final String TAG = "BluetoothManager"; - private static final boolean DBG = true; - private static final boolean VDBG = true; + private static final boolean DBG = false; private final BluetoothAdapter mAdapter; -- cgit v1.2.3 From 9ef032b089f5340abc204ee26091b459e636b810 Mon Sep 17 00:00:00 2001 From: Zach Johnson Date: Wed, 12 Dec 2018 17:11:25 -0800 Subject: Pass package name as part of startDiscovery Test: manual Bug: 118347252 Change-Id: Icbc2e7e756b16ffd181924b586a0292c2bf32ec5 --- framework/java/android/bluetooth/BluetoothManager.java | 1 + 1 file changed, 1 insertion(+) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index e3672a7e06..e08d405324 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -67,6 +67,7 @@ public final class BluetoothManager { } // Legacy api - getDefaultAdapter does not take in the context mAdapter = BluetoothAdapter.getDefaultAdapter(); + mAdapter.setContext(context); } /** -- cgit v1.2.3 From 945c249742252f1da5888adef3b1799eb5943f7d Mon Sep 17 00:00:00 2001 From: bohu Date: Tue, 18 Dec 2018 12:18:45 -0800 Subject: bluetooth: fix crash on accessing null adapter By checking bluetooth adaptor before accessing it, because it could be null on devices that do not support bluetooth, such as android emulator. BUG: 121129248 Test: lunch sdk_gphone_x86-userdebug make -j emulator launch chrome and it should not crash Change-Id: Ia75b5ee1efa3a8195056dada079239931eb9d901 --- framework/java/android/bluetooth/BluetoothManager.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index e08d405324..adedff3e93 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -67,7 +67,9 @@ public final class BluetoothManager { } // Legacy api - getDefaultAdapter does not take in the context mAdapter = BluetoothAdapter.getDefaultAdapter(); - mAdapter.setContext(context); + if (mAdapter != null) { + mAdapter.setContext(context); + } } /** -- cgit v1.2.3 From 658617b40f1526657c0ad59a406068af78598ccb Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Fri, 15 Nov 2019 12:41:09 -0800 Subject: Pipe featureId from app to noteOp in BT code FeatureIds are not yet available, hence in BTManager we assume always a "null" featureId. The effect of this change is that for apps that opt into using featureIds, they will have one BluetoothAdapter per feature, not one per process as before. In my testing this caused no problem. Most apps won't use featureIds, hence for most apps there is no change in behavior. Test: used bluetooth Bug: 136595429 Change-Id: Ic40326ea331c60f764f213bb2673cb4c49a81604 --- .../java/android/bluetooth/BluetoothManager.java | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index adedff3e93..cff4c2d75d 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -22,7 +22,9 @@ import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.content.Context; import android.content.pm.PackageManager; +import android.os.IBinder; import android.os.RemoteException; +import android.os.ServiceManager; import android.util.Log; import java.util.ArrayList; @@ -60,22 +62,34 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - context = context.getApplicationContext(); - if (context == null) { - throw new IllegalArgumentException( - "context not associated with any application (using a mock context?)"); + if (null == null) { + context = context.getApplicationContext(); + if (context == null) { + throw new IllegalArgumentException( + "context not associated with any application (using a mock context?)"); + } + + mAdapter = BluetoothAdapter.getDefaultAdapter(); + } else { + IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE); + if (b != null) { + mAdapter = new BluetoothAdapter(IBluetoothManager.Stub.asInterface(b)); + } else { + Log.e(TAG, "Bluetooth binder is null"); + mAdapter = null; + } } - // Legacy api - getDefaultAdapter does not take in the context - mAdapter = BluetoothAdapter.getDefaultAdapter(); + + // Context is not initialized in constructor if (mAdapter != null) { mAdapter.setContext(context); } } /** - * Get the default BLUETOOTH Adapter for this device. + * Get the BLUETOOTH Adapter for this device. * - * @return the default BLUETOOTH Adapter + * @return the BLUETOOTH Adapter */ public BluetoothAdapter getAdapter() { return mAdapter; -- cgit v1.2.3 From a188ac2ea6791550982fa6a3bf3636eb0d050b80 Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Fri, 15 Nov 2019 12:41:09 -0800 Subject: Pipe featureId from app to noteOp in BT code Test: atest CtsAppOpsTestCases (Now with canary test for this code) Bug: 136595429 Change-Id: I90bb6b017da4f03038fce76760a860390b727f00 --- framework/java/android/bluetooth/BluetoothManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index cff4c2d75d..7ff6466334 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -62,7 +62,7 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - if (null == null) { + if (context.getFeatureId() == null) { context = context.getApplicationContext(); if (context == null) { throw new IllegalArgumentException( -- cgit v1.2.3 From a3b85b3c16c08a442997e3775156bd7abf797a3b Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Thu, 5 Mar 2020 15:01:29 -0800 Subject: Rename featureId -> attributionTag In the core functionality this changes everything including aidl's and field names: - Context - ContentProvider - AppOps* - Package parsing For the rest, this is a shallow change to only change to the changed APIs. This keeps the change small-ish Exempt-From-Owner-Approval: Rename Fixes: 148792795 Test: TH Change-Id: I2a2245fe76e09e62cb13d5785d2efb4a304ba54a Merged-In: I2a2245fe76e09e62cb13d5785d2efb4a304ba54a --- framework/java/android/bluetooth/BluetoothManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 7ff6466334..3b4fe0a30b 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -62,7 +62,7 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - if (context.getFeatureId() == null) { + if (context.getAttributionTag() == null) { context = context.getApplicationContext(); if (context == null) { throw new IllegalArgumentException( -- cgit v1.2.3 From 5d55645dbd60248cb7437c543f4631d3dbbdb243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rymanowski?= Date: Thu, 23 Jul 2020 14:57:36 +0200 Subject: gatt: Allow to set eatt support With this patch it is possible to enable eatt_support as a GATT Client or GATT Server. Tag: #feature Bug: 159786353 Test: manually verified against device supporting EATT Sponsor: jpawlowski@ Change-Id: I6835a2bbd1b0ab9d6d64ee2bac5dfc96c0563afd --- .../java/android/bluetooth/BluetoothManager.java | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 3b4fe0a30b..d5c1c3e2d6 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -216,6 +216,24 @@ public final class BluetoothManager { return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO)); } + /** + * Open a GATT Server + * The callback is used to deliver results to Caller, such as connection status as well + * as the results of any other GATT server operations. + * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer + * to conduct GATT server operations. + * + * @param context App context + * @param callback GATT server callback handler that will receive asynchronous callbacks. + * @param eatt_support idicates if server should use eatt channel for notifications. + * @return BluetoothGattServer instance + * @hide + */ + public BluetoothGattServer openGattServer(Context context, + BluetoothGattServerCallback callback, boolean eatt_support) { + return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO, eatt_support)); + } + /** * Open a GATT Server * The callback is used to deliver results to Caller, such as connection status as well @@ -233,6 +251,27 @@ public final class BluetoothManager { */ public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, int transport) { + return (openGattServer(context, callback, transport, false)); + } + + /** + * Open a GATT Server + * The callback is used to deliver results to Caller, such as connection status as well + * as the results of any other GATT server operations. + * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer + * to conduct GATT server operations. + * + * @param context App context + * @param callback GATT server callback handler that will receive asynchronous callbacks. + * @param transport preferred transport for GATT connections to remote dual-mode devices {@link + * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link + * BluetoothDevice#TRANSPORT_LE} + * @param eatt_support idicates if server should use eatt channel for notifications. + * @return BluetoothGattServer instance + * @hide + */ + public BluetoothGattServer openGattServer(Context context, + BluetoothGattServerCallback callback, int transport, boolean eatt_support) { if (context == null || callback == null) { throw new IllegalArgumentException("null parameter: " + context + " " + callback); } @@ -248,7 +287,7 @@ public final class BluetoothManager { return null; } BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport); - Boolean regStatus = mGattServer.registerCallback(callback); + Boolean regStatus = mGattServer.registerCallback(callback, eatt_support); return regStatus ? mGattServer : null; } catch (RemoteException e) { Log.e(TAG, "", e); -- cgit v1.2.3 From 8f80e4a05b3f1b227f40de5ec0e9a6297154ffc0 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 2 Apr 2021 08:06:09 -0600 Subject: Update Bluetooth API annotations. Recent work has introduced a new "Nearby devices" runtime permission which protects all existing Bluetooth APIs; we've done this by defining a to convert the old BLUETOOTH and BLUETOOTH_ADMIN permissions into one of three new permissions: * BLUETOOTH_ADVERTISE: Required to be able to advertise to nearby Bluetooth devices. * BLUETOOTH_CONNECT: Allows applications to connect to paired bluetooth devices. * BLUETOOTH_SCAN: Required to be able to discover and pair nearby Bluetooth devices. At its core, this change begins updating the Bluetooth APIs to have correct @RequiresPermission indicating which permission is actually enforced internally. To ensure alignment across Binder, the newly added "RequiresPermissionChecker" Error Prone checker was used to discover any inconsistencies, ensuring correctness from server-side enforcement up through to the public APIs. In addition, since developers will continue building apps for both modern and legacy platforms, this change introduces new auto-doc annotations which will emit helpful consistent documentation describing the behavior of older devices that are still using the old permission model. Bug: 183626724 Test: ./build/soong/soong_ui.bash --make-mode Bluetooth RUN_ERROR_PRONE=true Change-Id: I02aa127e8e07f239561f4f2a3bbdfc6fccb82f7f --- framework/java/android/bluetooth/BluetoothManager.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index d5c1c3e2d6..a1e1b63050 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -20,6 +20,8 @@ import android.Manifest; import android.annotation.RequiresFeature; import android.annotation.RequiresPermission; import android.annotation.SystemService; +import android.bluetooth.annotations.RequiresBluetoothConnectPermission; +import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; import android.content.Context; import android.content.pm.PackageManager; import android.os.IBinder; @@ -109,7 +111,9 @@ public final class BluetoothManager { * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, * {@link BluetoothProfile#STATE_DISCONNECTING} */ - @RequiresPermission(Manifest.permission.BLUETOOTH) + @RequiresLegacyBluetoothPermission + @RequiresBluetoothConnectPermission + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public int getConnectionState(BluetoothDevice device, int profile) { if (DBG) Log.d(TAG, "getConnectionState()"); @@ -136,7 +140,9 @@ public final class BluetoothManager { * @param profile GATT or GATT_SERVER * @return List of devices. The list will be empty on error. */ - @RequiresPermission(Manifest.permission.BLUETOOTH) + @RequiresLegacyBluetoothPermission + @RequiresBluetoothConnectPermission + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public List getConnectedDevices(int profile) { if (DBG) Log.d(TAG, "getConnectedDevices"); if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { @@ -177,7 +183,9 @@ public final class BluetoothManager { * {@link BluetoothProfile#STATE_DISCONNECTING}, * @return List of devices. The list will be empty on error. */ - @RequiresPermission(Manifest.permission.BLUETOOTH) + @RequiresLegacyBluetoothPermission + @RequiresBluetoothConnectPermission + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public List getDevicesMatchingConnectionStates(int profile, int[] states) { if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates"); @@ -210,6 +218,7 @@ public final class BluetoothManager { * @param callback GATT server callback handler that will receive asynchronous callbacks. * @return BluetoothGattServer instance */ + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback) { @@ -229,6 +238,7 @@ public final class BluetoothManager { * @return BluetoothGattServer instance * @hide */ + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, boolean eatt_support) { return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO, eatt_support)); @@ -249,6 +259,7 @@ public final class BluetoothManager { * @return BluetoothGattServer instance * @hide */ + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, int transport) { return (openGattServer(context, callback, transport, false)); @@ -270,6 +281,7 @@ public final class BluetoothManager { * @return BluetoothGattServer instance * @hide */ + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, int transport, boolean eatt_support) { if (context == null || callback == null) { -- cgit v1.2.3 From 5ba8bfca7e9adf5c6d8ee8180aebad6f04037d6c Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 16 Apr 2021 09:53:23 -0600 Subject: More Bluetooth API annotation updates. This change adds a "BluetoothPermissionChecker" that ensures that all Bluetooth permission annotations are consistent. In addition, it verifies that all Bluetooth public APIs have been audited to be permission protected where relevant. We've currently standardized on saying that APIs that return device or Bluetooth state information (without sharing details about any particular remote Bluetooth device) do not need to be permission protected. This change is only annotations and has no behavior changes. Bug: 183626724 Test: ./build/soong/soong_ui.bash --make-mode Bluetooth RUN_ERROR_PRONE=true Change-Id: Ie80b15b058359bf1e9a6ee881b89cb3e5b584ca1 --- framework/java/android/bluetooth/BluetoothManager.java | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index a1e1b63050..2374f1cdc5 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -18,7 +18,9 @@ package android.bluetooth; import android.Manifest; import android.annotation.RequiresFeature; +import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; +import android.annotation.SuppressLint; import android.annotation.SystemService; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; @@ -93,6 +95,7 @@ public final class BluetoothManager { * * @return the BLUETOOTH Adapter */ + @RequiresNoPermission public BluetoothAdapter getAdapter() { return mAdapter; } @@ -218,6 +221,7 @@ public final class BluetoothManager { * @param callback GATT server callback handler that will receive asynchronous callbacks. * @return BluetoothGattServer instance */ + @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback) { @@ -238,6 +242,7 @@ public final class BluetoothManager { * @return BluetoothGattServer instance * @hide */ + @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, boolean eatt_support) { @@ -259,6 +264,7 @@ public final class BluetoothManager { * @return BluetoothGattServer instance * @hide */ + @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, int transport) { @@ -281,6 +287,7 @@ public final class BluetoothManager { * @return BluetoothGattServer instance * @hide */ + @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public BluetoothGattServer openGattServer(Context context, BluetoothGattServerCallback callback, int transport, boolean eatt_support) { -- cgit v1.2.3 From 4e4c9c4796c2c16d32e87417e084a1f724e9f258 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 22 Apr 2021 12:21:46 -0600 Subject: Refinement of AttributionSource handling. Previous CLs had started passing AttributionSource values across Binder calls inside BluetoothDevice instances, but this can cause confuse the permission check logic in the future; we should instead always aim to use the AttributionSource closest to the app making the call, instead of parceling it. This change also improves logging to highlight when we're quietly treating a permission as denied, and when a UID is mismatched. Bug: 186106084 Test: atest BluetoothInstrumentationTests Change-Id: I5d3fdb3c573cb9e77474952d8680caa4c4c464eb --- .../java/android/bluetooth/BluetoothManager.java | 28 +++------------------- 1 file changed, 3 insertions(+), 25 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 2374f1cdc5..69f9a79c73 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -16,19 +16,15 @@ package android.bluetooth; -import android.Manifest; import android.annotation.RequiresFeature; import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; -import android.annotation.SuppressLint; import android.annotation.SystemService; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; import android.content.Context; import android.content.pm.PackageManager; -import android.os.IBinder; import android.os.RemoteException; -import android.os.ServiceManager; import android.util.Log; import java.util.ArrayList; @@ -66,27 +62,9 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - if (context.getAttributionTag() == null) { - context = context.getApplicationContext(); - if (context == null) { - throw new IllegalArgumentException( - "context not associated with any application (using a mock context?)"); - } - - mAdapter = BluetoothAdapter.getDefaultAdapter(); - } else { - IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE); - if (b != null) { - mAdapter = new BluetoothAdapter(IBluetoothManager.Stub.asInterface(b)); - } else { - Log.e(TAG, "Bluetooth binder is null"); - mAdapter = null; - } - } - - // Context is not initialized in constructor - if (mAdapter != null) { - mAdapter.setContext(context); + mAdapter = BluetoothAdapter.createAdapter(); + if (context != null) { + mAdapter.setAttributionSource(context.getAttributionSource()); } } -- cgit v1.2.3 From 4dabcb764f1948823dcd74eefb3440afcab07db2 Mon Sep 17 00:00:00 2001 From: Oli Lan Date: Thu, 22 Apr 2021 19:05:17 +0100 Subject: 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 --- .../java/android/bluetooth/BluetoothManager.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') 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) { -- cgit v1.2.3 From f9e176c3dcafb82f251a123751578539e3484deb Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 22 Apr 2021 16:01:29 -0600 Subject: 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 --- .../java/android/bluetooth/BluetoothManager.java | 59 +++++++++++++--------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 07af8dbd23..b13ccaf7e5 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -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 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 connectedDevices = new ArrayList(); - - 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) { -- cgit v1.2.3 From 98f3044ce87c7ab9d2a0efbfb8ef6a16872262df Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 3 Jun 2021 09:26:53 -0600 Subject: More Binder call AttributionSource assignment. Since developers can use a BluetoothDevice object can make remote calls, it needs to have an accurate AttributionSource. Previous CLs had updated many places where these BluetoothDevice instances were passed across Binder interfaces, but this change updates several remaining locations which had been missed. Introduces new "Attributable" marker interface to offer consistent tooling when applying AttributionSource updates. Bug: 187097694 Test: atest BluetoothInstrumentationTests Change-Id: Icad3b9726591f0fbad58a493cefa5a0af7648280 --- framework/java/android/bluetooth/BluetoothManager.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index b13ccaf7e5..c21362cd89 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -16,6 +16,8 @@ package android.bluetooth; +import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.RequiresFeature; import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; @@ -24,6 +26,7 @@ import android.app.ActivityThread; import android.app.AppGlobals; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; +import android.content.Attributable; import android.content.AttributionSource; import android.content.Context; import android.content.pm.PackageManager; @@ -71,7 +74,7 @@ public final class BluetoothManager { } /** {@hide} */ - public static AttributionSource resolveAttributionSource(Context context) { + public static @NonNull AttributionSource resolveAttributionSource(@Nullable Context context) { AttributionSource res = null; if (context != null) { res = context.getAttributionSource(); @@ -193,7 +196,7 @@ public final class BluetoothManager { IBluetoothManager managerService = mAdapter.getBluetoothManager(); IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) return devices; - devices = BluetoothDevice.setAttributionSource( + devices = Attributable.setAttributionSource( iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource), mAttributionSource); } catch (RemoteException e) { -- cgit v1.2.3 From 199a385e223710fd5d3fd02e10d18f1217a233d8 Mon Sep 17 00:00:00 2001 From: William Escande Date: Wed, 22 Sep 2021 20:22:36 +0200 Subject: Use AttributionSource Builder Attribution source constructor are hidden api Add a Builder option to take a AttributionSource as parameter Test: atest BluetoothInstrumentationTests Bug: 195144968 Tag: #refactor Ignore-AOSP-First: No such thing on aosp Change-Id: I901c8afff6a40bd8484fd8e10baf290aa483c280 --- framework/java/android/bluetooth/BluetoothManager.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index c21362cd89..20152f3d24 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -88,8 +88,9 @@ public final class BluetoothManager { uid = android.os.Process.SYSTEM_UID; } try { - res = new AttributionSource(uid, - AppGlobals.getPackageManager().getPackagesForUid(uid)[0], null); + res = new AttributionSource.Builder(uid) + .setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0]) + .build(); } catch (RemoteException ignored) { } } -- cgit v1.2.3 From 39639385f90678e97aa6848ae3b43de6bfb3b49e Mon Sep 17 00:00:00 2001 From: Etienne Ruffieux Date: Wed, 13 Oct 2021 10:46:12 +0000 Subject: Removed BluetoothDevice#prepareToEnterProcess Tag: #feature Bug: 200202780 Test: manual Change-Id: I8d4be1da1bcb5b819c324f1a3a89c7dc317c31d6 --- framework/java/android/bluetooth/BluetoothManager.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 20152f3d24..b5df4db246 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -62,15 +62,15 @@ public final class BluetoothManager { private static final String TAG = "BluetoothManager"; private static final boolean DBG = false; - private final AttributionSource mAttributionSource; + private static AttributionSource sAttributionSource = null; private final BluetoothAdapter mAdapter; /** * @hide */ public BluetoothManager(Context context) { - mAttributionSource = resolveAttributionSource(context); - mAdapter = BluetoothAdapter.createAdapter(mAttributionSource); + sAttributionSource = resolveAttributionSource(context); + mAdapter = BluetoothAdapter.createAdapter(sAttributionSource); } /** {@hide} */ @@ -79,6 +79,9 @@ public final class BluetoothManager { if (context != null) { res = context.getAttributionSource(); } + else if (sAttributionSource != null) { + return sAttributionSource; + } if (res == null) { res = ActivityThread.currentAttributionSource(); } @@ -198,8 +201,8 @@ public final class BluetoothManager { IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) return devices; devices = Attributable.setAttributionSource( - iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource), - mAttributionSource); + iGatt.getDevicesMatchingConnectionStates(states, sAttributionSource), + sAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } -- cgit v1.2.3 From cc3d58735326ef2199b0ec6f7e1f25dbe38495a7 Mon Sep 17 00:00:00 2001 From: Etienne Ruffieux Date: Mon, 25 Oct 2021 21:38:16 +0000 Subject: Revert "Removed BluetoothDevice#prepareToEnterProcess" This reverts commit 39639385f90678e97aa6848ae3b43de6bfb3b49e. Reason for revert: Introducing regression for gms core Test: None Change-Id: I644b10c1869c12e1622300de43bfbdb57fb583d8 --- framework/java/android/bluetooth/BluetoothManager.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index b5df4db246..20152f3d24 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -62,15 +62,15 @@ public final class BluetoothManager { private static final String TAG = "BluetoothManager"; private static final boolean DBG = false; - private static AttributionSource sAttributionSource = null; + private final AttributionSource mAttributionSource; private final BluetoothAdapter mAdapter; /** * @hide */ public BluetoothManager(Context context) { - sAttributionSource = resolveAttributionSource(context); - mAdapter = BluetoothAdapter.createAdapter(sAttributionSource); + mAttributionSource = resolveAttributionSource(context); + mAdapter = BluetoothAdapter.createAdapter(mAttributionSource); } /** {@hide} */ @@ -79,9 +79,6 @@ public final class BluetoothManager { if (context != null) { res = context.getAttributionSource(); } - else if (sAttributionSource != null) { - return sAttributionSource; - } if (res == null) { res = ActivityThread.currentAttributionSource(); } @@ -201,8 +198,8 @@ public final class BluetoothManager { IBluetoothGatt iGatt = managerService.getBluetoothGatt(); if (iGatt == null) return devices; devices = Attributable.setAttributionSource( - iGatt.getDevicesMatchingConnectionStates(states, sAttributionSource), - sAttributionSource); + iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource), + mAttributionSource); } catch (RemoteException e) { Log.e(TAG, "", e); } -- cgit v1.2.3 From d517127a57b499305f82ec6eea6da8e0bd485e87 Mon Sep 17 00:00:00 2001 From: William Escande Date: Tue, 14 Dec 2021 16:16:11 +0100 Subject: Copy attributable to Bluetooth Attributable is called by bluetooth and it's hidden. By copying into bluetooth we are now allowed to call it Bug: 210467788 Test: build Tag: #refactor Change-Id: I73ea07c9439988ab5477c82799f718c6d81513be --- framework/java/android/bluetooth/BluetoothManager.java | 1 - 1 file changed, 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 20152f3d24..c93de41b5b 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -26,7 +26,6 @@ import android.app.ActivityThread; import android.app.AppGlobals; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; -import android.content.Attributable; import android.content.AttributionSource; import android.content.Context; import android.content.pm.PackageManager; -- cgit v1.2.3 From 9cc5b0f8b0f0acd4a55b48378fa77c0f1fea5798 Mon Sep 17 00:00:00 2001 From: Etienne Ruffieux Date: Tue, 14 Dec 2021 18:39:55 +0000 Subject: Moved AttributionSource related APIs in AttributionSource Modified myAttributionSource() to check for global AS for process in ActivityThread and fallback to building new AS with PackageManager#getPackageForUid(myUid()) if null. Tag: #feature Bug: 210467846 Bug: 210468546 Test: build Change-Id: I7aa75395469bf0bb806100420faaf98c52057355 CTS-Coverage-Bug: 210906055 --- .../java/android/bluetooth/BluetoothManager.java | 34 ++-------------------- 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothManager.java') diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java index 20152f3d24..2d8625098c 100644 --- a/framework/java/android/bluetooth/BluetoothManager.java +++ b/framework/java/android/bluetooth/BluetoothManager.java @@ -16,14 +16,10 @@ package android.bluetooth; -import android.annotation.NonNull; -import android.annotation.Nullable; import android.annotation.RequiresFeature; 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.Attributable; @@ -69,37 +65,11 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - mAttributionSource = resolveAttributionSource(context); + mAttributionSource = (context != null) ? context.getAttributionSource() : + AttributionSource.myAttributionSource(); mAdapter = BluetoothAdapter.createAdapter(mAttributionSource); } - /** {@hide} */ - public static @NonNull AttributionSource resolveAttributionSource(@Nullable 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.Builder(uid) - .setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0]) - .build(); - } catch (RemoteException ignored) { - } - } - if (res == null) { - throw new IllegalStateException("Failed to resolve AttributionSource"); - } - return res; - } - /** * Get the BLUETOOTH Adapter for this device. * -- cgit v1.2.3