Add BluetoothUuids.java
Test: unit test
Bug: 200231384
Change-Id: Ic9008ac6f5a9bb86743bc99f50e13a1a5449ee99
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/BluetoothUuids.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/BluetoothUuids.java
new file mode 100644
index 0000000..c5475a6
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/BluetoothUuids.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2021 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 com.android.server.nearby.common.bluetooth.fastpair;
+
+import java.util.UUID;
+
+/**
+ * Utilities for dealing with UUIDs assigned by the Bluetooth SIG. Has a lot in common with
+ * com.android.BluetoothUuid, but that class is hidden.
+ */
+public class BluetoothUuids {
+
+ /**
+ * The Base UUID is used for calculating 128-bit UUIDs from "short UUIDs" (16- and 32-bit).
+ *
+ * @see {https://www.bluetooth.com/specifications/assigned-numbers/service-discovery}
+ */
+ private static final UUID BASE_UUID = UUID.fromString("00000000-0000-1000-8000-00805F9B34FB");
+
+ /**
+ * Fast Pair custom GATT characteristics 128-bit UUIDs base.
+ *
+ * <p>Notes: The 16-bit value locates at the 3rd and 4th bytes.
+ *
+ * @see {go/fastpair-128bit-gatt}
+ */
+ private static final UUID FAST_PAIR_BASE_UUID =
+ UUID.fromString("FE2C0000-8366-4814-8EB0-01DE32100BEA");
+
+ private static final int BIT_INDEX_OF_16_BIT_UUID = 32;
+
+ private BluetoothUuids() {}
+
+ /**
+ * Returns the 16-bit version of the UUID. If this is not a 16-bit UUID, throws
+ * IllegalArgumentException.
+ */
+ public static short get16BitUuid(UUID uuid) {
+ if (!is16BitUuid(uuid)) {
+ throw new IllegalArgumentException("Not a 16-bit Bluetooth UUID: " + uuid);
+ }
+ return (short) (uuid.getMostSignificantBits() >> BIT_INDEX_OF_16_BIT_UUID);
+ }
+
+ /** Checks whether the UUID is 16 bit */
+ public static boolean is16BitUuid(UUID uuid) {
+ // See Service Discovery Protocol in the Bluetooth Core Specification. Bits at index 32-48
+ // are the 16-bit UUID, and the rest must match the Base UUID.
+ return uuid.getLeastSignificantBits() == BASE_UUID.getLeastSignificantBits()
+ && (uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL)
+ == BASE_UUID.getMostSignificantBits();
+ }
+
+ /** Converts short UUID to 128 bit UUID */
+ public static UUID to128BitUuid(short shortUuid) {
+ return new UUID(
+ ((shortUuid & 0xFFFFL) << BIT_INDEX_OF_16_BIT_UUID)
+ | BASE_UUID.getMostSignificantBits(), BASE_UUID.getLeastSignificantBits());
+ }
+
+ /** Transfers the 16-bit Fast Pair custom GATT characteristics to 128-bit. */
+ public static UUID toFastPair128BitUuid(short shortUuid) {
+ return new UUID(
+ ((shortUuid & 0xFFFFL) << BIT_INDEX_OF_16_BIT_UUID)
+ | FAST_PAIR_BASE_UUID.getMostSignificantBits(),
+ FAST_PAIR_BASE_UUID.getLeastSignificantBits());
+ }
+}
diff --git a/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/BluetoothUuidsTest.java b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/BluetoothUuidsTest.java
new file mode 100644
index 0000000..c5bf407
--- /dev/null
+++ b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/BluetoothUuidsTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2021 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 com.android.server.nearby.common.bluetooth.fastpair;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.platform.test.annotations.Presubmit;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.UUID;
+
+/** Unit tests for {@link BluetoothUuids}. */
+@Presubmit
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class BluetoothUuidsTest {
+
+ // According to {@code android.bluetooth.BluetoothUuid}
+ private static final short A2DP_SINK_SHORT_UUID = (short) 0x110B;
+ private static final UUID A2DP_SINK_CHARACTERISTICS =
+ UUID.fromString("0000110B-0000-1000-8000-00805F9B34FB");
+
+ // According to {go/fastpair-128bit-gatt}, the short uuid locates at the 3rd and 4th bytes based
+ // on the Fast Pair custom GATT characteristics 128-bit UUIDs base -
+ // "FE2C0000-8366-4814-8EB0-01DE32100BEA".
+ private static final short CUSTOM_SHORT_UUID = (short) 0x9487;
+ private static final UUID CUSTOM_CHARACTERISTICS =
+ UUID.fromString("FE2C9487-8366-4814-8EB0-01DE32100BEA");
+
+ @Test
+ public void get16BitUuid() {
+ assertThat(BluetoothUuids.get16BitUuid(A2DP_SINK_CHARACTERISTICS))
+ .isEqualTo(A2DP_SINK_SHORT_UUID);
+ }
+
+ @Test
+ public void is16BitUuid() {
+ assertThat(BluetoothUuids.is16BitUuid(A2DP_SINK_CHARACTERISTICS)).isTrue();
+ }
+
+ @Test
+ public void to128BitUuid() {
+ assertThat(BluetoothUuids.to128BitUuid(A2DP_SINK_SHORT_UUID))
+ .isEqualTo(A2DP_SINK_CHARACTERISTICS);
+ }
+
+ @Test
+ public void toFastPair128BitUuid() {
+ assertThat(BluetoothUuids.toFastPair128BitUuid(CUSTOM_SHORT_UUID))
+ .isEqualTo(CUSTOM_CHARACTERISTICS);
+ }
+}