diff options
| -rw-r--r-- | core/api/current.txt | 1 | ||||
| -rw-r--r-- | core/api/system-current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/bluetooth/BluetoothDevice.java | 32 | ||||
| -rw-r--r-- | core/java/android/companion/CompanionDeviceManager.java | 28 |
4 files changed, 51 insertions, 11 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index bb501e89b3b3..03a15c039e1b 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -8999,6 +8999,7 @@ package android.bluetooth { method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public String getName(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public int getType(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public android.os.ParcelUuid[] getUuids(); + method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public boolean setAlias(@NonNull String); method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setPairingConfirmation(boolean); method public boolean setPin(byte[]); method public void writeToParcel(android.os.Parcel, int); diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 02fd463eb2e4..e5814005f31c 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -2178,6 +2178,7 @@ package android.bluetooth.le { package android.companion { public final class CompanionDeviceManager { + method @RequiresPermission("android.permission.MANAGE_COMPANION_DEVICES") public boolean canPairWithoutPrompt(@NonNull String, @NonNull String, int); method @RequiresPermission("android.permission.MANAGE_COMPANION_DEVICES") public boolean isDeviceAssociatedForWifiConnection(@NonNull String, @NonNull android.net.MacAddress, @NonNull android.os.UserHandle); } diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java index 89086497a446..a96c14f216f3 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -26,6 +26,7 @@ import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SuppressLint; import android.annotation.SystemApi; import android.app.PropertyInvalidatedCache; +import android.companion.AssociationRequest; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.os.Build; @@ -1231,8 +1232,7 @@ public final class BluetoothDevice implements Parcelable { } /** - * Get the Bluetooth alias of the remote device. - * <p>Alias is the locally modified name of a remote device. + * Get the locally modifiable name (alias) of the remote Bluetooth device. * * @return the Bluetooth alias, the friendly device name if no alias, or * null if there was a problem @@ -1258,25 +1258,35 @@ public final class BluetoothDevice implements Parcelable { } /** - * Set the Bluetooth alias of the remote device. - * <p>Alias is the locally modified name of a remote device. - * <p>This methoid overwrites the alias. The changed - * alias is saved in the local storage so that the change - * is preserved over power cycle. + * Sets the locally modifiable name (alias) of the remote Bluetooth device. This method + * overwrites the previously stored alias. The new alias is saved in local + * storage so that the change is preserved over power cycles. * - * @return true on success, false on error - * @hide + * <p>This method requires the calling app to be associated with Companion Device Manager (see + * {@link android.companion.CompanionDeviceManager#associate(AssociationRequest, + * android.companion.CompanionDeviceManager.Callback, Handler)}) and have the {@link + * android.Manifest.permission#BLUETOOTH} permission. Alternatively, if the caller has the + * {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED} permission, they can bypass the + * Companion Device Manager association requirement. + * + * @param alias is the new locally modifiable name for the remote Bluetooth device which must be + * non-null and not the empty string. + * @return {@code true} if the alias is successfully set, {@code false} on error + * @throws IllegalArgumentException if the alias is {@code null} or the empty string */ - @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) @RequiresPermission(Manifest.permission.BLUETOOTH) public boolean setAlias(@NonNull String alias) { + if (alias == null || alias.isEmpty()) { + throw new IllegalArgumentException("Cannot set the alias to null or the empty string"); + } final IBluetooth service = sService; if (service == null) { Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); return false; } try { - return service.setRemoteAlias(this, alias); + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + return service.setRemoteAlias(this, alias, adapter.getOpPackageName()); } catch (RemoteException e) { Log.e(TAG, "", e); } diff --git a/core/java/android/companion/CompanionDeviceManager.java b/core/java/android/companion/CompanionDeviceManager.java index b441b364cec8..86bd8a2b7a4e 100644 --- a/core/java/android/companion/CompanionDeviceManager.java +++ b/core/java/android/companion/CompanionDeviceManager.java @@ -25,6 +25,7 @@ import android.annotation.SystemService; import android.app.Activity; import android.app.Application; import android.app.PendingIntent; +import android.bluetooth.BluetoothDevice; import android.content.ComponentName; import android.content.Context; import android.content.IntentSender; @@ -331,6 +332,33 @@ public final class CompanionDeviceManager { } /** + * Checks whether the bluetooth device represented by the mac address was recently associated + * with the companion app. This allows these devices to skip the Bluetooth pairing dialog if + * their pairing variant is {@link BluetoothDevice#PAIRING_VARIANT_CONSENT}. + * + * @param packageName the package name of the calling app + * @param deviceMacAddress the bluetooth device's mac address + * @param userId the calling user's identifier + * @return true if it was recently associated and we can bypass the dialog, false otherwise + * @hide + */ + @SystemApi + @RequiresPermission(android.Manifest.permission.MANAGE_COMPANION_DEVICES) + public boolean canPairWithoutPrompt(@NonNull String packageName, + @NonNull String deviceMacAddress, int userId) { + if (!checkFeaturePresent()) { + return false; + } + Objects.requireNonNull(packageName, "package name cannot be null"); + Objects.requireNonNull(deviceMacAddress, "device mac address cannot be null"); + try { + return mService.canPairWithoutPrompt(packageName, deviceMacAddress, userId); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Register to receive callbacks whenever the associated device comes in and out of range. * * The provided device must be {@link #associate associated} with the calling app before |