Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f4c887d1 authored by Ömer Faruk Yılmaz's avatar Ömer Faruk Yılmaz
Browse files

Add new API `getIdentityAddressType` to BluetoothDevice.java

Bug: 377171798
Bug: 373899888
Test: m com.android.btservices
Change-Id: I48ddc08c40350bbd1050d53c0da0a23c978558b0
parent 54edfd68
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.BluetoothSinkAudioPolicy;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice.BluetoothAddress;
import android.bluetooth.BluetoothQualityReport;
import android.bluetooth.IncomingRfcommSocketInfo;
import android.bluetooth.OobData;
@@ -40,6 +41,8 @@ import android.os.ParcelUuid;
import android.os.ParcelFileDescriptor;
import android.os.ResultReceiver;

parcelable BluetoothDevice.BluetoothAddress;

/**
 * System private API for talking with the Bluetooth service.
 *
@@ -63,6 +66,8 @@ interface IBluetooth
    boolean setName(in String name, in AttributionSource attributionSource);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
    String getIdentityAddress(in String address);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
    BluetoothDevice.BluetoothAddress getIdentityAddressWithType(in String address);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    String getName(in AttributionSource attributionSource);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE)")
+50 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.ActiveDeviceProfile;
import android.bluetooth.BluetoothAdapter.ActiveDeviceUse;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice.BluetoothAddress;
import android.bluetooth.BluetoothFrameworkInitializer;
import android.bluetooth.BluetoothMap;
import android.bluetooth.BluetoothProfile;
@@ -2387,6 +2388,23 @@ public class AdapterService extends Service {
            return service.getIdentityAddress(address);
        }

        @Override
        @NonNull
        public BluetoothAddress getIdentityAddressWithType(@NonNull String address) {
            AdapterService service = getService();
            if (service == null
                    || !callerIsSystemOrActiveOrManagedUser(
                            service, TAG, "getIdentityAddressWithType")
                    || !Utils.checkConnectPermissionForDataDelivery(
                            service,
                            Utils.getCallingAttributionSource(mService),
                            "AdapterService getIdentityAddressWithType")) {
                return new BluetoothAddress(null, BluetoothDevice.ADDRESS_TYPE_UNKNOWN);
            }
            service.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            return service.getIdentityAddressWithType(address);
        }

        @Override
        public String getName(AttributionSource source) {
            AdapterService service = getService();
@@ -4895,6 +4913,38 @@ public class AdapterService extends Service {
        }
    }

    /**
     * Returns the identity address and identity address type.
     *
     * @param address of remote device
     * @return a {@link BluetoothDevice.BluetoothAddress} containing identity address and identity
     *     address type
     */
    @NonNull
    public BluetoothAddress getIdentityAddressWithType(@NonNull String address) {
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice(Ascii.toUpperCase(address));
        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);

        String identityAddress = null;
        int identityAddressType = BluetoothDevice.ADDRESS_TYPE_UNKNOWN;

        if (deviceProp != null) {
            if (deviceProp.getIdentityAddress() != null) {
                identityAddress = deviceProp.getIdentityAddress();
            }
            identityAddressType = deviceProp.getIdentityAddressType();
        } else {
            if (Flags.identityAddressNullIfNotKnown()) {
                identityAddress = null;
            } else {
                identityAddress = address;
            }
        }

        return new BluetoothAddress(identityAddress, identityAddressType);
    }

    private static class CallerInfo {
        public String callerPackageName;
        public UserHandle user;
+31 −0
Original line number Diff line number Diff line
@@ -978,6 +978,37 @@ public class AdapterServiceTest {
        assertThat(mLooper.nextMessage()).isNull();
    }

    @Test
    @EnableFlags(Flags.FLAG_IDENTITY_ADDRESS_TYPE_API)
    public void testIdentityAddressType() {
        RemoteDevices remoteDevices = mAdapterService.getRemoteDevices();
        remoteDevices.addDeviceProperties(Utils.getBytesFromAddress((TEST_BT_ADDR_1)));

        int identityAddressTypePublic = 0x00; // Should map to BluetoothDevice.ADDRESS_TYPE_PUBLIC
        int identityAddressTypeRandom = 0x01; // Should map to BluetoothDevice.ADDRESS_TYPE_RANDOM

        remoteDevices.leAddressAssociateCallback(
                Utils.getBytesFromAddress(TEST_BT_ADDR_1),
                Utils.getBytesFromAddress(TEST_BT_ADDR_2),
                identityAddressTypePublic);

        BluetoothDevice.BluetoothAddress bluetoothAddress =
                mAdapterService.getIdentityAddressWithType(TEST_BT_ADDR_1);
        assertThat(bluetoothAddress.getAddress()).isEqualTo(TEST_BT_ADDR_2);
        assertThat(bluetoothAddress.getAddressType())
                .isEqualTo(BluetoothDevice.ADDRESS_TYPE_PUBLIC);

        remoteDevices.leAddressAssociateCallback(
                Utils.getBytesFromAddress(TEST_BT_ADDR_1),
                Utils.getBytesFromAddress(TEST_BT_ADDR_2),
                identityAddressTypeRandom);

        bluetoothAddress = mAdapterService.getIdentityAddressWithType(TEST_BT_ADDR_1);
        assertThat(bluetoothAddress.getAddress()).isEqualTo(TEST_BT_ADDR_2);
        assertThat(bluetoothAddress.getAddressType())
                .isEqualTo(BluetoothDevice.ADDRESS_TYPE_RANDOM);
    }

    @Test
    @EnableFlags(Flags.FLAG_IDENTITY_ADDRESS_NULL_IF_NOT_KNOWN)
    public void testIdentityAddressNullIfUnknown() {
+10 −0
Original line number Diff line number Diff line
@@ -541,6 +541,7 @@ package android.bluetooth {
    method @Nullable @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public String getAlias();
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothClass getBluetoothClass();
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public int getBondState();
    method @FlaggedApi("com.android.bluetooth.flags.identity_address_type_api") @NonNull @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public android.bluetooth.BluetoothDevice.BluetoothAddress getIdentityAddressWithType();
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public String getName();
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public int getType();
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.os.ParcelUuid[] getUuids();
@@ -607,6 +608,15 @@ package android.bluetooth {
    field public static final int TRANSPORT_LE = 2; // 0x2
  }

  @FlaggedApi("com.android.bluetooth.flags.identity_address_type_api") public static final class BluetoothDevice.BluetoothAddress implements android.os.Parcelable {
    ctor public BluetoothDevice.BluetoothAddress(@Nullable String, int);
    method public int describeContents();
    method @Nullable public String getAddress();
    method public int getAddressType();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.bluetooth.BluetoothDevice.BluetoothAddress> CREATOR;
  }

  public final class BluetoothGatt implements android.bluetooth.BluetoothProfile {
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public void abortReliableWrite();
    method @Deprecated @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public void abortReliableWrite(android.bluetooth.BluetoothDevice);
+76 −0
Original line number Diff line number Diff line
@@ -1725,6 +1725,33 @@ public final class BluetoothDevice implements Parcelable, Attributable {
        return null;
    }

    /**
     * Returns the identity address and identity address type of this BluetoothDevice.
     *
     * @return a {@link #BluetoothAddress} containing identity address and identity address type. If
     *     Bluetooth is not enabled or identity address type is not available, it will return a
     *     {@link #BluetoothAddress} containing {@link #ADDRESS_TYPE_UNKNOWN} device for the
     *     identity address type.
     */
    @FlaggedApi(Flags.FLAG_IDENTITY_ADDRESS_TYPE_API)
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @NonNull
    public BluetoothAddress getIdentityAddressWithType() {
        if (DBG) log("getIdentityAddressWithType()");
        final IBluetooth service = getService();
        if (service == null || !isBluetoothEnabled()) {
            Log.e(TAG, "BT not enabled. Cannot get identity address with type");
        } else {
            try {
                return service.getIdentityAddressWithType(mAddress);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        return new BluetoothAddress(null, BluetoothDevice.ADDRESS_TYPE_UNKNOWN);
    }

    /**
     * Get the friendly Bluetooth name of the remote device.
     *
@@ -3748,4 +3775,53 @@ public final class BluetoothDevice implements Parcelable, Attributable {
    private static void log(String msg) {
        Log.d(TAG, msg);
    }

    /** A data class for Bluetooth address and address type. */
    @FlaggedApi(Flags.FLAG_IDENTITY_ADDRESS_TYPE_API)
    public static final class BluetoothAddress implements Parcelable {
        private final @Nullable String mAddress;
        private final @AddressType int mAddressType;

        public BluetoothAddress(@Nullable String address, @AddressType int addressType) {
            mAddress = address;
            mAddressType = addressType;
        }

        @Nullable
        public String getAddress() {
            return mAddress;
        }

        @AddressType
        public int getAddressType() {
            return mAddressType;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(@NonNull Parcel out, int flags) {
            BluetoothUtils.writeStringToParcel(out, mAddress);
            out.writeInt(mAddressType);
        }

        private BluetoothAddress(@NonNull Parcel in) {
            this(in.readString(), in.readInt());
        }

        /** {@link Parcelable.Creator} interface implementation. */
        public static final @NonNull Parcelable.Creator<BluetoothAddress> CREATOR =
                new Parcelable.Creator<BluetoothAddress>() {
                    public @NonNull BluetoothAddress createFromParcel(Parcel in) {
                        return new BluetoothAddress(in);
                    }

                    public @NonNull BluetoothAddress[] newArray(int size) {
                        return new BluetoothAddress[size];
                    }
                };
    }
}