blob: 7e96c23af4b998e2117e14f96e7e1b302d056a36 [file] [log] [blame]
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -07001/*
2 * Copyright (C) 2009 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
17package android.bluetooth;
18
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
Mathew Inwood7d543892018-08-01 15:07:20 +010022import android.annotation.UnsupportedAppUsage;
Nick Pelly03759e52009-09-28 12:33:17 -070023import android.os.ParcelUuid;
24
Wei Wangc3868042014-04-15 14:57:16 -070025import java.nio.ByteBuffer;
26import java.nio.ByteOrder;
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070027import java.util.Arrays;
28import java.util.HashSet;
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080029import java.util.UUID;
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070030
31/**
Jack He910201b2017-08-22 16:06:54 -070032 * Static helper methods and constants to decode the ParcelUuid of remote devices.
33 *
34 * @hide
35 */
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080036@SystemApi
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070037public final class BluetoothUuid {
38
39 /* See Bluetooth Assigned Numbers document - SDP section, to get the values of UUIDs
40 * for the various services.
41 *
42 * The following 128 bit values are calculated as:
43 * uuid * 2^96 + BASE_UUID
44 */
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080045
46 /** @hide */
47 @NonNull
48 @SystemApi
49 public static final ParcelUuid A2DP_SINK =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070050 ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080051 /** @hide */
52 @NonNull
53 @SystemApi
54 public static final ParcelUuid A2DP_SOURCE =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070055 ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080056 /** @hide */
57 @NonNull
58 @SystemApi
59 public static final ParcelUuid ADV_AUDIO_DIST =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070060 ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080061 /** @hide */
62 @NonNull
63 @SystemApi
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070064 public static final ParcelUuid HSP =
65 ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080066 /** @hide */
67 @NonNull
68 @SystemApi
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080069 public static final ParcelUuid HSP_AG =
70 ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080071 /** @hide */
72 @NonNull
73 @SystemApi
74 public static final ParcelUuid HFP =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070075 ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080076 /** @hide */
77 @NonNull
78 @SystemApi
79 public static final ParcelUuid HFP_AG =
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080080 ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080081 /** @hide */
82 @NonNull
83 @SystemApi
84 public static final ParcelUuid AVRCP_CONTROLLER =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070085 ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080086 /** @hide */
87 @NonNull
88 @SystemApi
89 public static final ParcelUuid AVRCP_TARGET =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070090 ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080091 /** @hide */
92 @NonNull
93 @SystemApi
94 public static final ParcelUuid OBEX_OBJECT_PUSH =
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070095 ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -080096 /** @hide */
97 @NonNull
98 @SystemApi
99 public static final ParcelUuid HID =
Adam Powell026e8572010-06-21 16:23:42 -0700100 ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800101 /** @hide */
102 @NonNull
103 @SystemApi
104 public static final ParcelUuid HOGP =
Andre Eisenbach1f0c3da2013-03-07 18:07:35 -0800105 ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800106 /** @hide */
107 @NonNull
108 @SystemApi
Danica Chang41f1a092010-08-11 14:54:43 -0700109 public static final ParcelUuid PANU =
110 ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800111 /** @hide */
112 @NonNull
113 @SystemApi
Danica Chang41f1a092010-08-11 14:54:43 -0700114 public static final ParcelUuid NAP =
115 ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800116 /** @hide */
117 @NonNull
118 @SystemApi
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700119 public static final ParcelUuid BNEP =
120 ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800121 /** @hide */
122 @NonNull
123 @SystemApi
Matthew Xie947b3922014-11-20 21:53:26 -0800124 public static final ParcelUuid PBAP_PCE =
125 ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800126 /** @hide */
127 @NonNull
128 @SystemApi
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800129 public static final ParcelUuid PBAP_PSE =
130 ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800131 /** @hide */
132 @NonNull
133 @SystemApi
Matthew Xiece145222013-07-18 17:31:50 -0700134 public static final ParcelUuid MAP =
Kim Schulz6816ee22013-08-22 11:18:02 +0200135 ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800136 /** @hide */
137 @NonNull
138 @SystemApi
Matthew Xiece145222013-07-18 17:31:50 -0700139 public static final ParcelUuid MNS =
140 ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800141 /** @hide */
142 @NonNull
143 @SystemApi
Kim Schulz6816ee22013-08-22 11:18:02 +0200144 public static final ParcelUuid MAS =
145 ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800146 /** @hide */
147 @NonNull
148 @SystemApi
Jack He910201b2017-08-22 16:06:54 -0700149 public static final ParcelUuid SAP =
Casper Bonded8355fb2015-03-19 10:36:45 +0100150 ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB");
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800151 /** @hide */
152 @NonNull
153 @SystemApi
154 public static final ParcelUuid HEARING_AID =
Jakub Pawlowski79c54da2018-02-22 15:10:08 -0800155 ParcelUuid.fromString("0000FDF0-0000-1000-8000-00805f9b34fb");
Jack He910201b2017-08-22 16:06:54 -0700156
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800157 /** @hide */
158 @NonNull
159 @SystemApi
Wei Wang2f7544c2013-10-29 21:05:37 -0700160 public static final ParcelUuid BASE_UUID =
161 ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB");
162
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700163 /**
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800164 * Length of bytes for 16 bit UUID
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700165 *
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800166 * @hide
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700167 */
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800168 @SystemApi
169 public static final int UUID_BYTES_16_BIT = 2;
170 /**
171 * Length of bytes for 32 bit UUID
172 *
173 * @hide
174 */
175 @SystemApi
176 public static final int UUID_BYTES_32_BIT = 4;
177 /**
178 * Length of bytes for 128 bit UUID
179 *
180 * @hide
181 */
182 @SystemApi
183 public static final int UUID_BYTES_128_BIT = 16;
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700184
185 /**
186 * Returns true if there any common ParcelUuids in uuidA and uuidB.
187 *
188 * @param uuidA - List of ParcelUuids
189 * @param uuidB - List of ParcelUuids
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800190 *
191 * @hide
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700192 */
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800193 @SystemApi
194 public static boolean containsAnyUuid(@Nullable ParcelUuid[] uuidA,
195 @Nullable ParcelUuid[] uuidB) {
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700196 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700197
198 if (uuidA == null) {
Jack He9e045d22017-08-22 21:21:23 -0700199 return uuidB.length == 0;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700200 }
201
202 if (uuidB == null) {
Jack He9e045d22017-08-22 21:21:23 -0700203 return uuidA.length == 0;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700204 }
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700205
Jack He910201b2017-08-22 16:06:54 -0700206 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
207 for (ParcelUuid uuid : uuidB) {
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700208 if (uuidSet.contains(uuid)) return true;
209 }
210 return false;
211 }
212
213 /**
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800214 * Extract the Service Identifier or the actual uuid from the Parcel Uuid.
215 * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
216 * this function will return 110B
Jack He910201b2017-08-22 16:06:54 -0700217 *
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800218 * @param parcelUuid
219 * @return the service identifier.
220 */
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800221 private static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800222 UUID uuid = parcelUuid.getUuid();
Ruina Liu3bff59e2017-07-22 18:41:48 +0800223 long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32;
Jack He910201b2017-08-22 16:06:54 -0700224 return (int) value;
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800225 }
Wei Wang2f7544c2013-10-29 21:05:37 -0700226
227 /**
Wei Wangc3868042014-04-15 14:57:16 -0700228 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
229 * but the returned UUID is always in 128-bit format.
230 * Note UUID is little endian in Bluetooth.
231 *
232 * @param uuidBytes Byte representation of uuid.
233 * @return {@link ParcelUuid} parsed from bytes.
234 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800235 *
236 * @hide
Wei Wangc3868042014-04-15 14:57:16 -0700237 */
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800238 @NonNull
239 @SystemApi
240 public static ParcelUuid parseUuidFrom(@Nullable byte[] uuidBytes) {
Wei Wangc3868042014-04-15 14:57:16 -0700241 if (uuidBytes == null) {
242 throw new IllegalArgumentException("uuidBytes cannot be null");
243 }
244 int length = uuidBytes.length;
Jack He9e045d22017-08-22 21:21:23 -0700245 if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT
246 && length != UUID_BYTES_128_BIT) {
Wei Wangc3868042014-04-15 14:57:16 -0700247 throw new IllegalArgumentException("uuidBytes length invalid - " + length);
248 }
249
250 // Construct a 128 bit UUID.
251 if (length == UUID_BYTES_128_BIT) {
252 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
253 long msb = buf.getLong(8);
254 long lsb = buf.getLong(0);
255 return new ParcelUuid(new UUID(msb, lsb));
256 }
257
258 // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
259 // 128_bit_value = uuid * 2^96 + BASE_UUID
260 long shortUuid;
261 if (length == UUID_BYTES_16_BIT) {
262 shortUuid = uuidBytes[0] & 0xFF;
263 shortUuid += (uuidBytes[1] & 0xFF) << 8;
264 } else {
Jack He910201b2017-08-22 16:06:54 -0700265 shortUuid = uuidBytes[0] & 0xFF;
Wei Wangc3868042014-04-15 14:57:16 -0700266 shortUuid += (uuidBytes[1] & 0xFF) << 8;
267 shortUuid += (uuidBytes[2] & 0xFF) << 16;
268 shortUuid += (uuidBytes[3] & 0xFF) << 24;
269 }
270 long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
271 long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
272 return new ParcelUuid(new UUID(msb, lsb));
273 }
274
275 /**
Jack He910201b2017-08-22 16:06:54 -0700276 * Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or
277 * 128-bit UUID, Note returned value is little endian (Bluetooth).
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700278 *
279 * @param uuid uuid to parse.
280 * @return shortest representation of {@code uuid} as bytes.
281 * @throws IllegalArgumentException If the {@code uuid} is null.
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800282 *
283 * @hide
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700284 */
285 public static byte[] uuidToBytes(ParcelUuid uuid) {
286 if (uuid == null) {
287 throw new IllegalArgumentException("uuid cannot be null");
288 }
289
290 if (is16BitUuid(uuid)) {
291 byte[] uuidBytes = new byte[UUID_BYTES_16_BIT];
292 int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
Jack He910201b2017-08-22 16:06:54 -0700293 uuidBytes[0] = (byte) (uuidVal & 0xFF);
294 uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700295 return uuidBytes;
296 }
297
298 if (is32BitUuid(uuid)) {
299 byte[] uuidBytes = new byte[UUID_BYTES_32_BIT];
300 int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
Jack He910201b2017-08-22 16:06:54 -0700301 uuidBytes[0] = (byte) (uuidVal & 0xFF);
302 uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
303 uuidBytes[2] = (byte) ((uuidVal & 0xFF0000) >> 16);
304 uuidBytes[3] = (byte) ((uuidVal & 0xFF000000) >> 24);
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700305 return uuidBytes;
306 }
307
308 // Construct a 128 bit UUID.
309 long msb = uuid.getUuid().getMostSignificantBits();
310 long lsb = uuid.getUuid().getLeastSignificantBits();
311
312 byte[] uuidBytes = new byte[UUID_BYTES_128_BIT];
313 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
314 buf.putLong(8, msb);
315 buf.putLong(0, lsb);
316 return uuidBytes;
317 }
318
319 /**
Wei Wang2f7544c2013-10-29 21:05:37 -0700320 * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
Wei Wangc3868042014-04-15 14:57:16 -0700321 *
Wei Wang2f7544c2013-10-29 21:05:37 -0700322 * @param parcelUuid
323 * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800324 *
325 * @hide
Wei Wang2f7544c2013-10-29 21:05:37 -0700326 */
Mathew Inwood7d543892018-08-01 15:07:20 +0100327 @UnsupportedAppUsage
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700328 public static boolean is16BitUuid(ParcelUuid parcelUuid) {
Wei Wangc3868042014-04-15 14:57:16 -0700329 UUID uuid = parcelUuid.getUuid();
330 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
331 return false;
332 }
333 return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
Wei Wang2f7544c2013-10-29 21:05:37 -0700334 }
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700335
336
337 /**
338 * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
339 *
340 * @param parcelUuid
341 * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800342 *
343 * @hide
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700344 */
Mathew Inwood7d543892018-08-01 15:07:20 +0100345 @UnsupportedAppUsage
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700346 public static boolean is32BitUuid(ParcelUuid parcelUuid) {
347 UUID uuid = parcelUuid.getUuid();
348 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
349 return false;
350 }
351 if (is16BitUuid(parcelUuid)) {
352 return false;
353 }
354 return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
355 }
Rahul Sabnisbc9f48f2019-12-03 11:48:05 -0800356
357 private BluetoothUuid() {}
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700358}