blob: 243579a31ce028f6b0a0ef87cd293a37de883592 [file] [log] [blame]
Jaikumar Ganeshd5ac1ae2009-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 Pellyaef439e2009-09-28 12:33:17 -070019import android.os.ParcelUuid;
20
Wei Wang82500dd2014-04-15 14:57:16 -070021import java.nio.ByteBuffer;
22import java.nio.ByteOrder;
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -070023import java.util.Arrays;
24import java.util.HashSet;
Jaikumar Ganesh84690c82010-12-10 12:48:58 -080025import java.util.UUID;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070026
27/**
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -070028* Static helper methods and constants to decode the ParcelUuid of remote devices.
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070029* @hide
30*/
31public final class BluetoothUuid {
32
33 /* See Bluetooth Assigned Numbers document - SDP section, to get the values of UUIDs
34 * for the various services.
35 *
36 * The following 128 bit values are calculated as:
37 * uuid * 2^96 + BASE_UUID
38 */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -070039 public static final ParcelUuid AudioSink =
40 ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
41 public static final ParcelUuid AudioSource =
42 ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
43 public static final ParcelUuid AdvAudioDist =
44 ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB");
45 public static final ParcelUuid HSP =
46 ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh84690c82010-12-10 12:48:58 -080047 public static final ParcelUuid HSP_AG =
48 ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB");
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -070049 public static final ParcelUuid Handsfree =
50 ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh84690c82010-12-10 12:48:58 -080051 public static final ParcelUuid Handsfree_AG =
52 ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB");
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -070053 public static final ParcelUuid AvrcpController =
54 ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB");
55 public static final ParcelUuid AvrcpTarget =
56 ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB");
57 public static final ParcelUuid ObexObjectPush =
58 ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb");
Jaikumar Ganesh545e6702010-06-04 10:23:03 -070059 public static final ParcelUuid Hid =
Adam Powelldf7627d2010-06-21 16:23:42 -070060 ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb");
Andre Eisenbachc60bed42013-03-07 18:07:35 -080061 public static final ParcelUuid Hogp =
62 ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb");
Danica Chang6fdd0c62010-08-11 14:54:43 -070063 public static final ParcelUuid PANU =
64 ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB");
65 public static final ParcelUuid NAP =
66 ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh34536182010-08-23 18:32:03 -070067 public static final ParcelUuid BNEP =
68 ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB");
Matthew Xie68623612014-11-20 21:53:26 -080069 public static final ParcelUuid PBAP_PCE =
70 ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh84690c82010-12-10 12:48:58 -080071 public static final ParcelUuid PBAP_PSE =
72 ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB");
Matthew Xiefe3807a2013-07-18 17:31:50 -070073 public static final ParcelUuid MAP =
Kim Schulz0d376052013-08-22 11:18:02 +020074 ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB");
Matthew Xiefe3807a2013-07-18 17:31:50 -070075 public static final ParcelUuid MNS =
76 ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB");
Kim Schulz0d376052013-08-22 11:18:02 +020077 public static final ParcelUuid MAS =
78 ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB");
Casper Bonde2a5f6082015-03-19 10:36:45 +010079 public static final ParcelUuid SAP =
80 ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB");
81
Wei Wang18c76932013-10-29 21:05:37 -070082 public static final ParcelUuid BASE_UUID =
83 ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB");
84
Wei Wang82500dd2014-04-15 14:57:16 -070085 /** Length of bytes for 16 bit UUID */
86 public static final int UUID_BYTES_16_BIT = 2;
87 /** Length of bytes for 32 bit UUID */
88 public static final int UUID_BYTES_32_BIT = 4;
89 /** Length of bytes for 128 bit UUID */
90 public static final int UUID_BYTES_128_BIT = 16;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070091
Nick Pelly24bb9b82009-10-02 20:34:18 -070092 public static final ParcelUuid[] RESERVED_UUIDS = {
93 AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget,
Casper Bonde2a5f6082015-03-19 10:36:45 +010094 ObexObjectPush, PANU, NAP, MAP, MNS, MAS, SAP};
Nick Pelly24bb9b82009-10-02 20:34:18 -070095
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -070096 public static boolean isAudioSource(ParcelUuid uuid) {
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -070097 return uuid.equals(AudioSource);
98 }
99
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700100 public static boolean isAudioSink(ParcelUuid uuid) {
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700101 return uuid.equals(AudioSink);
102 }
103
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700104 public static boolean isAdvAudioDist(ParcelUuid uuid) {
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700105 return uuid.equals(AdvAudioDist);
106 }
107
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700108 public static boolean isHandsfree(ParcelUuid uuid) {
Jaikumar Ganesh7f74d532009-07-14 12:21:26 -0700109 return uuid.equals(Handsfree);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700110 }
111
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700112 public static boolean isHeadset(ParcelUuid uuid) {
Jaikumar Ganeshade40522009-07-30 13:32:25 -0700113 return uuid.equals(HSP);
114 }
115
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700116 public static boolean isAvrcpController(ParcelUuid uuid) {
Jaikumar Ganeshade40522009-07-30 13:32:25 -0700117 return uuid.equals(AvrcpController);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700118 }
Jackson Fan9abcf092009-08-19 21:01:29 +0800119
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700120 public static boolean isAvrcpTarget(ParcelUuid uuid) {
Jackson Fan9abcf092009-08-19 21:01:29 +0800121 return uuid.equals(AvrcpTarget);
122 }
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700123
Jaikumar Ganesh545e6702010-06-04 10:23:03 -0700124 public static boolean isInputDevice(ParcelUuid uuid) {
125 return uuid.equals(Hid);
126 }
127
Jaikumar Ganesh34536182010-08-23 18:32:03 -0700128 public static boolean isPanu(ParcelUuid uuid) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700129 return uuid.equals(PANU);
130 }
131
Jaikumar Ganesh34536182010-08-23 18:32:03 -0700132 public static boolean isNap(ParcelUuid uuid) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700133 return uuid.equals(NAP);
134 }
Jaikumar Ganesh34536182010-08-23 18:32:03 -0700135
136 public static boolean isBnep(ParcelUuid uuid) {
137 return uuid.equals(BNEP);
138 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700139 public static boolean isMap(ParcelUuid uuid) {
140 return uuid.equals(MAP);
141 }
142 public static boolean isMns(ParcelUuid uuid) {
143 return uuid.equals(MNS);
144 }
Kim Schulz0d376052013-08-22 11:18:02 +0200145 public static boolean isMas(ParcelUuid uuid) {
146 return uuid.equals(MAS);
147 }
Casper Bonde2a5f6082015-03-19 10:36:45 +0100148 public static boolean isSap(ParcelUuid uuid) {
149 return uuid.equals(SAP);
150 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700151
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700152 /**
153 * Returns true if ParcelUuid is present in uuidArray
154 *
155 * @param uuidArray - Array of ParcelUuids
156 * @param uuid
157 */
158 public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) {
Jaikumar Ganesh10eac972009-09-21 12:48:51 -0700159 if ((uuidArray == null || uuidArray.length == 0) && uuid == null)
160 return true;
161
162 if (uuidArray == null)
163 return false;
164
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700165 for (ParcelUuid element: uuidArray) {
166 if (element.equals(uuid)) return true;
167 }
168 return false;
169 }
170
171 /**
172 * Returns true if there any common ParcelUuids in uuidA and uuidB.
173 *
174 * @param uuidA - List of ParcelUuids
175 * @param uuidB - List of ParcelUuids
176 *
177 */
178 public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
179 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganeshdca2f0f2009-09-16 17:50:52 -0700180
181 if (uuidA == null) {
182 return uuidB.length == 0 ? true : false;
183 }
184
185 if (uuidB == null) {
186 return uuidA.length == 0 ? true : false;
187 }
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700188
189 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid> (Arrays.asList(uuidA));
190 for (ParcelUuid uuid: uuidB) {
191 if (uuidSet.contains(uuid)) return true;
192 }
193 return false;
194 }
195
196 /**
197 * Returns true if all the ParcelUuids in ParcelUuidB are present in
198 * ParcelUuidA
199 *
200 * @param uuidA - Array of ParcelUuidsA
201 * @param uuidB - Array of ParcelUuidsB
202 *
203 */
204 public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
205 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganeshdca2f0f2009-09-16 17:50:52 -0700206
207 if (uuidA == null) {
208 return uuidB.length == 0 ? true : false;
209 }
210
211 if (uuidB == null) return true;
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700212
213 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid> (Arrays.asList(uuidA));
214 for (ParcelUuid uuid: uuidB) {
215 if (!uuidSet.contains(uuid)) return false;
216 }
217 return true;
218 }
219
Jaikumar Ganesh84690c82010-12-10 12:48:58 -0800220 /**
221 * Extract the Service Identifier or the actual uuid from the Parcel Uuid.
222 * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
223 * this function will return 110B
224 * @param parcelUuid
225 * @return the service identifier.
226 */
227 public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
228 UUID uuid = parcelUuid.getUuid();
229 long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
230 return (int)value;
231 }
Wei Wang18c76932013-10-29 21:05:37 -0700232
233 /**
Wei Wang82500dd2014-04-15 14:57:16 -0700234 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
235 * but the returned UUID is always in 128-bit format.
236 * Note UUID is little endian in Bluetooth.
237 *
238 * @param uuidBytes Byte representation of uuid.
239 * @return {@link ParcelUuid} parsed from bytes.
240 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
241 */
242 public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
243 if (uuidBytes == null) {
244 throw new IllegalArgumentException("uuidBytes cannot be null");
245 }
246 int length = uuidBytes.length;
247 if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT &&
248 length != UUID_BYTES_128_BIT) {
249 throw new IllegalArgumentException("uuidBytes length invalid - " + length);
250 }
251
252 // Construct a 128 bit UUID.
253 if (length == UUID_BYTES_128_BIT) {
254 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
255 long msb = buf.getLong(8);
256 long lsb = buf.getLong(0);
257 return new ParcelUuid(new UUID(msb, lsb));
258 }
259
260 // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
261 // 128_bit_value = uuid * 2^96 + BASE_UUID
262 long shortUuid;
263 if (length == UUID_BYTES_16_BIT) {
264 shortUuid = uuidBytes[0] & 0xFF;
265 shortUuid += (uuidBytes[1] & 0xFF) << 8;
266 } else {
267 shortUuid = uuidBytes[0] & 0xFF ;
268 shortUuid += (uuidBytes[1] & 0xFF) << 8;
269 shortUuid += (uuidBytes[2] & 0xFF) << 16;
270 shortUuid += (uuidBytes[3] & 0xFF) << 24;
271 }
272 long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
273 long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
274 return new ParcelUuid(new UUID(msb, lsb));
275 }
276
277 /**
Jakub Pawlowskif67ee832016-10-24 13:56:54 -0700278 * Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or 128-bit UUID,
279 * Note returned value is little endian (Bluetooth).
280 *
281 * @param uuid uuid to parse.
282 * @return shortest representation of {@code uuid} as bytes.
283 * @throws IllegalArgumentException If the {@code uuid} is null.
284 */
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);
293 uuidBytes[0] = (byte)(uuidVal & 0xFF);
294 uuidBytes[1] = (byte)((uuidVal & 0xFF00) >> 8);
295 return uuidBytes;
296 }
297
298 if (is32BitUuid(uuid)) {
299 byte[] uuidBytes = new byte[UUID_BYTES_32_BIT];
300 int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
301 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);
305 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 Wang18c76932013-10-29 21:05:37 -0700320 * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
Wei Wang82500dd2014-04-15 14:57:16 -0700321 *
Wei Wang18c76932013-10-29 21:05:37 -0700322 * @param parcelUuid
323 * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
324 */
Wei Wang51800842014-05-07 14:54:43 -0700325 public static boolean is16BitUuid(ParcelUuid parcelUuid) {
Wei Wang82500dd2014-04-15 14:57:16 -0700326 UUID uuid = parcelUuid.getUuid();
327 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
328 return false;
329 }
330 return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
Wei Wang18c76932013-10-29 21:05:37 -0700331 }
Wei Wang51800842014-05-07 14:54:43 -0700332
333
334 /**
335 * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
336 *
337 * @param parcelUuid
338 * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
339 */
340 public static boolean is32BitUuid(ParcelUuid parcelUuid) {
341 UUID uuid = parcelUuid.getUuid();
342 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
343 return false;
344 }
345 if (is16BitUuid(parcelUuid)) {
346 return false;
347 }
348 return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
349 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700350}