• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.nearby.common.bluetooth.fastpair;
18 
19 import java.util.UUID;
20 
21 /**
22  * Utilities for dealing with UUIDs assigned by the Bluetooth SIG. Has a lot in common with
23  * com.android.BluetoothUuid, but that class is hidden.
24  */
25 public class BluetoothUuids {
26 
27     /**
28      * The Base UUID is used for calculating 128-bit UUIDs from "short UUIDs" (16- and 32-bit).
29      *
30      * @see {https://www.bluetooth.com/specifications/assigned-numbers/service-discovery}
31      */
32     private static final UUID BASE_UUID = UUID.fromString("00000000-0000-1000-8000-00805F9B34FB");
33 
34     /**
35      * Fast Pair custom GATT characteristics 128-bit UUIDs base.
36      *
37      * <p>Notes: The 16-bit value locates at the 3rd and 4th bytes.
38      *
39      * @see {go/fastpair-128bit-gatt}
40      */
41     private static final UUID FAST_PAIR_BASE_UUID =
42             UUID.fromString("FE2C0000-8366-4814-8EB0-01DE32100BEA");
43 
44     private static final int BIT_INDEX_OF_16_BIT_UUID = 32;
45 
BluetoothUuids()46     private BluetoothUuids() {}
47 
48     /**
49      * Returns the 16-bit version of the UUID. If this is not a 16-bit UUID, throws
50      * IllegalArgumentException.
51      */
get16BitUuid(UUID uuid)52     public static short get16BitUuid(UUID uuid) {
53         if (!is16BitUuid(uuid)) {
54             throw new IllegalArgumentException("Not a 16-bit Bluetooth UUID: " + uuid);
55         }
56         return (short) (uuid.getMostSignificantBits() >> BIT_INDEX_OF_16_BIT_UUID);
57     }
58 
59     /** Checks whether the UUID is 16 bit */
is16BitUuid(UUID uuid)60     public static boolean is16BitUuid(UUID uuid) {
61         // See Service Discovery Protocol in the Bluetooth Core Specification. Bits at index 32-48
62         // are the 16-bit UUID, and the rest must match the Base UUID.
63         return uuid.getLeastSignificantBits() == BASE_UUID.getLeastSignificantBits()
64                 && (uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL)
65                 == BASE_UUID.getMostSignificantBits();
66     }
67 
68     /** Converts short UUID to 128 bit UUID */
to128BitUuid(short shortUuid)69     public static UUID to128BitUuid(short shortUuid) {
70         return new UUID(
71                 ((shortUuid & 0xFFFFL) << BIT_INDEX_OF_16_BIT_UUID)
72                         | BASE_UUID.getMostSignificantBits(), BASE_UUID.getLeastSignificantBits());
73     }
74 
75     /** Transfers the 16-bit Fast Pair custom GATT characteristics to 128-bit. */
toFastPair128BitUuid(short shortUuid)76     public static UUID toFastPair128BitUuid(short shortUuid) {
77         return new UUID(
78                 ((shortUuid & 0xFFFFL) << BIT_INDEX_OF_16_BIT_UUID)
79                         | FAST_PAIR_BASE_UUID.getMostSignificantBits(),
80                 FAST_PAIR_BASE_UUID.getLeastSignificantBits());
81     }
82 }
83