blob: 605dbd21993f2f43a7fadf24a98796881ef01a75 [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
Nick Pelly03759e52009-09-28 12:33:17 -070019import android.os.ParcelUuid;
20
Wei Wangc3868042014-04-15 14:57:16 -070021import java.nio.ByteBuffer;
22import java.nio.ByteOrder;
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070023import java.util.Arrays;
24import java.util.HashSet;
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080025import java.util.UUID;
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070026
27/**
Jack He910201b2017-08-22 16:06:54 -070028 * Static helper methods and constants to decode the ParcelUuid of remote devices.
29 *
30 * @hide
31 */
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070032public final class BluetoothUuid {
33
34 /* See Bluetooth Assigned Numbers document - SDP section, to get the values of UUIDs
35 * for the various services.
36 *
37 * The following 128 bit values are calculated as:
38 * uuid * 2^96 + BASE_UUID
39 */
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070040 public static final ParcelUuid AudioSink =
41 ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
42 public static final ParcelUuid AudioSource =
43 ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
44 public static final ParcelUuid AdvAudioDist =
45 ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB");
46 public static final ParcelUuid HSP =
47 ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080048 public static final ParcelUuid HSP_AG =
49 ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070050 public static final ParcelUuid Handsfree =
51 ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080052 public static final ParcelUuid Handsfree_AG =
53 ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070054 public static final ParcelUuid AvrcpController =
55 ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB");
56 public static final ParcelUuid AvrcpTarget =
57 ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB");
58 public static final ParcelUuid ObexObjectPush =
59 ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb");
Jaikumar Ganesh5d0b83e2010-06-04 10:23:03 -070060 public static final ParcelUuid Hid =
Adam Powell026e8572010-06-21 16:23:42 -070061 ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb");
Andre Eisenbach1f0c3da2013-03-07 18:07:35 -080062 public static final ParcelUuid Hogp =
63 ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb");
Danica Chang41f1a092010-08-11 14:54:43 -070064 public static final ParcelUuid PANU =
65 ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB");
66 public static final ParcelUuid NAP =
67 ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -070068 public static final ParcelUuid BNEP =
69 ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB");
Matthew Xie947b3922014-11-20 21:53:26 -080070 public static final ParcelUuid PBAP_PCE =
71 ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080072 public static final ParcelUuid PBAP_PSE =
73 ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB");
Matthew Xiece145222013-07-18 17:31:50 -070074 public static final ParcelUuid MAP =
Kim Schulz6816ee22013-08-22 11:18:02 +020075 ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB");
Matthew Xiece145222013-07-18 17:31:50 -070076 public static final ParcelUuid MNS =
77 ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB");
Kim Schulz6816ee22013-08-22 11:18:02 +020078 public static final ParcelUuid MAS =
79 ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB");
Jack He910201b2017-08-22 16:06:54 -070080 public static final ParcelUuid SAP =
Casper Bonded8355fb2015-03-19 10:36:45 +010081 ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB");
Jakub Pawlowski3f238732017-11-22 11:02:34 -080082 public static final ParcelUuid HearingAid =
Jakub Pawlowski79c54da2018-02-22 15:10:08 -080083 ParcelUuid.fromString("0000FDF0-0000-1000-8000-00805f9b34fb");
Jack He910201b2017-08-22 16:06:54 -070084
Wei Wang2f7544c2013-10-29 21:05:37 -070085 public static final ParcelUuid BASE_UUID =
86 ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB");
87
Wei Wangc3868042014-04-15 14:57:16 -070088 /** Length of bytes for 16 bit UUID */
89 public static final int UUID_BYTES_16_BIT = 2;
90 /** Length of bytes for 32 bit UUID */
91 public static final int UUID_BYTES_32_BIT = 4;
92 /** Length of bytes for 128 bit UUID */
93 public static final int UUID_BYTES_128_BIT = 16;
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070094
Nick Pellyee1402d2009-10-02 20:34:18 -070095 public static final ParcelUuid[] RESERVED_UUIDS = {
Jack He910201b2017-08-22 16:06:54 -070096 AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget,
97 ObexObjectPush, PANU, NAP, MAP, MNS, MAS, SAP};
Nick Pellyee1402d2009-10-02 20:34:18 -070098
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070099 public static boolean isAudioSource(ParcelUuid uuid) {
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700100 return uuid.equals(AudioSource);
101 }
102
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700103 public static boolean isAudioSink(ParcelUuid uuid) {
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700104 return uuid.equals(AudioSink);
105 }
106
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700107 public static boolean isAdvAudioDist(ParcelUuid uuid) {
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700108 return uuid.equals(AdvAudioDist);
109 }
110
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700111 public static boolean isHandsfree(ParcelUuid uuid) {
Jaikumar Ganesh4eb5ccc2009-07-14 12:21:26 -0700112 return uuid.equals(Handsfree);
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700113 }
114
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700115 public static boolean isHeadset(ParcelUuid uuid) {
Jaikumar Ganesh671065d2009-07-30 13:32:25 -0700116 return uuid.equals(HSP);
117 }
118
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700119 public static boolean isAvrcpController(ParcelUuid uuid) {
Jaikumar Ganesh671065d2009-07-30 13:32:25 -0700120 return uuid.equals(AvrcpController);
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700121 }
Jackson Fan0d538762009-08-19 21:01:29 +0800122
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700123 public static boolean isAvrcpTarget(ParcelUuid uuid) {
Jackson Fan0d538762009-08-19 21:01:29 +0800124 return uuid.equals(AvrcpTarget);
125 }
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700126
Jaikumar Ganesh5d0b83e2010-06-04 10:23:03 -0700127 public static boolean isInputDevice(ParcelUuid uuid) {
128 return uuid.equals(Hid);
129 }
130
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700131 public static boolean isPanu(ParcelUuid uuid) {
Danica Chang41f1a092010-08-11 14:54:43 -0700132 return uuid.equals(PANU);
133 }
134
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700135 public static boolean isNap(ParcelUuid uuid) {
Danica Chang41f1a092010-08-11 14:54:43 -0700136 return uuid.equals(NAP);
137 }
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700138
139 public static boolean isBnep(ParcelUuid uuid) {
140 return uuid.equals(BNEP);
141 }
Jack He910201b2017-08-22 16:06:54 -0700142
Matthew Xiece145222013-07-18 17:31:50 -0700143 public static boolean isMap(ParcelUuid uuid) {
144 return uuid.equals(MAP);
145 }
Jack He910201b2017-08-22 16:06:54 -0700146
Matthew Xiece145222013-07-18 17:31:50 -0700147 public static boolean isMns(ParcelUuid uuid) {
148 return uuid.equals(MNS);
149 }
Jack He910201b2017-08-22 16:06:54 -0700150
Kim Schulz6816ee22013-08-22 11:18:02 +0200151 public static boolean isMas(ParcelUuid uuid) {
152 return uuid.equals(MAS);
153 }
Jack He910201b2017-08-22 16:06:54 -0700154
Casper Bonded8355fb2015-03-19 10:36:45 +0100155 public static boolean isSap(ParcelUuid uuid) {
156 return uuid.equals(SAP);
157 }
Matthew Xiece145222013-07-18 17:31:50 -0700158
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700159 /**
160 * Returns true if ParcelUuid is present in uuidArray
161 *
162 * @param uuidArray - Array of ParcelUuids
163 * @param uuid
164 */
165 public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) {
Jack He910201b2017-08-22 16:06:54 -0700166 if ((uuidArray == null || uuidArray.length == 0) && uuid == null) {
Jaikumar Ganesh391e8a72009-09-21 12:48:51 -0700167 return true;
Jack He910201b2017-08-22 16:06:54 -0700168 }
Jaikumar Ganesh391e8a72009-09-21 12:48:51 -0700169
Jack He910201b2017-08-22 16:06:54 -0700170 if (uuidArray == null) {
Jaikumar Ganesh391e8a72009-09-21 12:48:51 -0700171 return false;
Jack He910201b2017-08-22 16:06:54 -0700172 }
Jaikumar Ganesh391e8a72009-09-21 12:48:51 -0700173
Jack He910201b2017-08-22 16:06:54 -0700174 for (ParcelUuid element : uuidArray) {
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700175 if (element.equals(uuid)) return true;
176 }
177 return false;
178 }
179
180 /**
181 * Returns true if there any common ParcelUuids in uuidA and uuidB.
182 *
183 * @param uuidA - List of ParcelUuids
184 * @param uuidB - List of ParcelUuids
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700185 */
186 public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
187 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700188
189 if (uuidA == null) {
Jack He9e045d22017-08-22 21:21:23 -0700190 return uuidB.length == 0;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700191 }
192
193 if (uuidB == null) {
Jack He9e045d22017-08-22 21:21:23 -0700194 return uuidA.length == 0;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700195 }
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700196
Jack He910201b2017-08-22 16:06:54 -0700197 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
198 for (ParcelUuid uuid : uuidB) {
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700199 if (uuidSet.contains(uuid)) return true;
200 }
201 return false;
202 }
203
204 /**
205 * Returns true if all the ParcelUuids in ParcelUuidB are present in
206 * ParcelUuidA
207 *
208 * @param uuidA - Array of ParcelUuidsA
209 * @param uuidB - Array of ParcelUuidsB
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700210 */
211 public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
212 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700213
214 if (uuidA == null) {
Jack He9e045d22017-08-22 21:21:23 -0700215 return uuidB.length == 0;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700216 }
217
218 if (uuidB == null) return true;
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700219
Jack He910201b2017-08-22 16:06:54 -0700220 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
221 for (ParcelUuid uuid : uuidB) {
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700222 if (!uuidSet.contains(uuid)) return false;
223 }
224 return true;
225 }
226
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800227 /**
228 * Extract the Service Identifier or the actual uuid from the Parcel Uuid.
229 * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
230 * this function will return 110B
Jack He910201b2017-08-22 16:06:54 -0700231 *
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800232 * @param parcelUuid
233 * @return the service identifier.
234 */
235 public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
236 UUID uuid = parcelUuid.getUuid();
Ruina Liu3bff59e2017-07-22 18:41:48 +0800237 long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32;
Jack He910201b2017-08-22 16:06:54 -0700238 return (int) value;
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800239 }
Wei Wang2f7544c2013-10-29 21:05:37 -0700240
241 /**
Wei Wangc3868042014-04-15 14:57:16 -0700242 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
243 * but the returned UUID is always in 128-bit format.
244 * Note UUID is little endian in Bluetooth.
245 *
246 * @param uuidBytes Byte representation of uuid.
247 * @return {@link ParcelUuid} parsed from bytes.
248 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
249 */
250 public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
251 if (uuidBytes == null) {
252 throw new IllegalArgumentException("uuidBytes cannot be null");
253 }
254 int length = uuidBytes.length;
Jack He9e045d22017-08-22 21:21:23 -0700255 if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT
256 && length != UUID_BYTES_128_BIT) {
Wei Wangc3868042014-04-15 14:57:16 -0700257 throw new IllegalArgumentException("uuidBytes length invalid - " + length);
258 }
259
260 // Construct a 128 bit UUID.
261 if (length == UUID_BYTES_128_BIT) {
262 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
263 long msb = buf.getLong(8);
264 long lsb = buf.getLong(0);
265 return new ParcelUuid(new UUID(msb, lsb));
266 }
267
268 // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
269 // 128_bit_value = uuid * 2^96 + BASE_UUID
270 long shortUuid;
271 if (length == UUID_BYTES_16_BIT) {
272 shortUuid = uuidBytes[0] & 0xFF;
273 shortUuid += (uuidBytes[1] & 0xFF) << 8;
274 } else {
Jack He910201b2017-08-22 16:06:54 -0700275 shortUuid = uuidBytes[0] & 0xFF;
Wei Wangc3868042014-04-15 14:57:16 -0700276 shortUuid += (uuidBytes[1] & 0xFF) << 8;
277 shortUuid += (uuidBytes[2] & 0xFF) << 16;
278 shortUuid += (uuidBytes[3] & 0xFF) << 24;
279 }
280 long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
281 long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
282 return new ParcelUuid(new UUID(msb, lsb));
283 }
284
285 /**
Jack He910201b2017-08-22 16:06:54 -0700286 * Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or
287 * 128-bit UUID, Note returned value is little endian (Bluetooth).
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700288 *
289 * @param uuid uuid to parse.
290 * @return shortest representation of {@code uuid} as bytes.
291 * @throws IllegalArgumentException If the {@code uuid} is null.
292 */
293 public static byte[] uuidToBytes(ParcelUuid uuid) {
294 if (uuid == null) {
295 throw new IllegalArgumentException("uuid cannot be null");
296 }
297
298 if (is16BitUuid(uuid)) {
299 byte[] uuidBytes = new byte[UUID_BYTES_16_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);
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700303 return uuidBytes;
304 }
305
306 if (is32BitUuid(uuid)) {
307 byte[] uuidBytes = new byte[UUID_BYTES_32_BIT];
308 int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
Jack He910201b2017-08-22 16:06:54 -0700309 uuidBytes[0] = (byte) (uuidVal & 0xFF);
310 uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
311 uuidBytes[2] = (byte) ((uuidVal & 0xFF0000) >> 16);
312 uuidBytes[3] = (byte) ((uuidVal & 0xFF000000) >> 24);
Jakub Pawlowski4648fc32016-10-24 13:56:54 -0700313 return uuidBytes;
314 }
315
316 // Construct a 128 bit UUID.
317 long msb = uuid.getUuid().getMostSignificantBits();
318 long lsb = uuid.getUuid().getLeastSignificantBits();
319
320 byte[] uuidBytes = new byte[UUID_BYTES_128_BIT];
321 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
322 buf.putLong(8, msb);
323 buf.putLong(0, lsb);
324 return uuidBytes;
325 }
326
327 /**
Wei Wang2f7544c2013-10-29 21:05:37 -0700328 * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
Wei Wangc3868042014-04-15 14:57:16 -0700329 *
Wei Wang2f7544c2013-10-29 21:05:37 -0700330 * @param parcelUuid
331 * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
332 */
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700333 public static boolean is16BitUuid(ParcelUuid parcelUuid) {
Wei Wangc3868042014-04-15 14:57:16 -0700334 UUID uuid = parcelUuid.getUuid();
335 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
336 return false;
337 }
338 return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
Wei Wang2f7544c2013-10-29 21:05:37 -0700339 }
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700340
341
342 /**
343 * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
344 *
345 * @param parcelUuid
346 * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
347 */
348 public static boolean is32BitUuid(ParcelUuid parcelUuid) {
349 UUID uuid = parcelUuid.getUuid();
350 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
351 return false;
352 }
353 if (is16BitUuid(parcelUuid)) {
354 return false;
355 }
356 return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
357 }
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700358}