blob: 1e22eb39289eb08b17a291b90e173c2f1699a6ae [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/**
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070028* Static helper methods and constants to decode the ParcelUuid of remote devices.
Jaikumar Ganesh879bf5b2009-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 Ganesh55a0c032009-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 Ganesh166a60e2010-12-10 12:48:58 -080047 public static final ParcelUuid HSP_AG =
48 ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070049 public static final ParcelUuid Handsfree =
50 ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080051 public static final ParcelUuid Handsfree_AG =
52 ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh55a0c032009-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 Ganesh5d0b83e2010-06-04 10:23:03 -070059 public static final ParcelUuid Hid =
Adam Powell026e8572010-06-21 16:23:42 -070060 ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb");
Andre Eisenbach1f0c3da2013-03-07 18:07:35 -080061 public static final ParcelUuid Hogp =
62 ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb");
Danica Chang41f1a092010-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 Ganesh43d545d2010-08-23 18:32:03 -070067 public static final ParcelUuid BNEP =
68 ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB");
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -080069 public static final ParcelUuid PBAP_PSE =
70 ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB");
Matthew Xiece145222013-07-18 17:31:50 -070071 public static final ParcelUuid MAP =
Kim Schulz6816ee22013-08-22 11:18:02 +020072 ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB");
Matthew Xiece145222013-07-18 17:31:50 -070073 public static final ParcelUuid MNS =
74 ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB");
Kim Schulz6816ee22013-08-22 11:18:02 +020075 public static final ParcelUuid MAS =
76 ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB");
77
Wei Wang2f7544c2013-10-29 21:05:37 -070078 public static final ParcelUuid BASE_UUID =
79 ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB");
80
Wei Wangc3868042014-04-15 14:57:16 -070081 /** Length of bytes for 16 bit UUID */
82 public static final int UUID_BYTES_16_BIT = 2;
83 /** Length of bytes for 32 bit UUID */
84 public static final int UUID_BYTES_32_BIT = 4;
85 /** Length of bytes for 128 bit UUID */
86 public static final int UUID_BYTES_128_BIT = 16;
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070087
Nick Pellyee1402d2009-10-02 20:34:18 -070088 public static final ParcelUuid[] RESERVED_UUIDS = {
89 AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget,
Kim Schulz6816ee22013-08-22 11:18:02 +020090 ObexObjectPush, PANU, NAP, MAP, MNS, MAS};
Nick Pellyee1402d2009-10-02 20:34:18 -070091
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070092 public static boolean isAudioSource(ParcelUuid uuid) {
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070093 return uuid.equals(AudioSource);
94 }
95
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -070096 public static boolean isAudioSink(ParcelUuid uuid) {
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -070097 return uuid.equals(AudioSink);
98 }
99
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700100 public static boolean isAdvAudioDist(ParcelUuid uuid) {
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700101 return uuid.equals(AdvAudioDist);
102 }
103
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700104 public static boolean isHandsfree(ParcelUuid uuid) {
Jaikumar Ganesh4eb5ccc2009-07-14 12:21:26 -0700105 return uuid.equals(Handsfree);
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700106 }
107
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700108 public static boolean isHeadset(ParcelUuid uuid) {
Jaikumar Ganesh671065d2009-07-30 13:32:25 -0700109 return uuid.equals(HSP);
110 }
111
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700112 public static boolean isAvrcpController(ParcelUuid uuid) {
Jaikumar Ganesh671065d2009-07-30 13:32:25 -0700113 return uuid.equals(AvrcpController);
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700114 }
Jackson Fan0d538762009-08-19 21:01:29 +0800115
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700116 public static boolean isAvrcpTarget(ParcelUuid uuid) {
Jackson Fan0d538762009-08-19 21:01:29 +0800117 return uuid.equals(AvrcpTarget);
118 }
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700119
Jaikumar Ganesh5d0b83e2010-06-04 10:23:03 -0700120 public static boolean isInputDevice(ParcelUuid uuid) {
121 return uuid.equals(Hid);
122 }
123
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700124 public static boolean isPanu(ParcelUuid uuid) {
Danica Chang41f1a092010-08-11 14:54:43 -0700125 return uuid.equals(PANU);
126 }
127
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700128 public static boolean isNap(ParcelUuid uuid) {
Danica Chang41f1a092010-08-11 14:54:43 -0700129 return uuid.equals(NAP);
130 }
Jaikumar Ganesh43d545d2010-08-23 18:32:03 -0700131
132 public static boolean isBnep(ParcelUuid uuid) {
133 return uuid.equals(BNEP);
134 }
Matthew Xiece145222013-07-18 17:31:50 -0700135 public static boolean isMap(ParcelUuid uuid) {
136 return uuid.equals(MAP);
137 }
138 public static boolean isMns(ParcelUuid uuid) {
139 return uuid.equals(MNS);
140 }
Kim Schulz6816ee22013-08-22 11:18:02 +0200141 public static boolean isMas(ParcelUuid uuid) {
142 return uuid.equals(MAS);
143 }
Matthew Xiece145222013-07-18 17:31:50 -0700144
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700145 /**
146 * Returns true if ParcelUuid is present in uuidArray
147 *
148 * @param uuidArray - Array of ParcelUuids
149 * @param uuid
150 */
151 public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) {
Jaikumar Ganesh391e8a72009-09-21 12:48:51 -0700152 if ((uuidArray == null || uuidArray.length == 0) && uuid == null)
153 return true;
154
155 if (uuidArray == null)
156 return false;
157
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700158 for (ParcelUuid element: uuidArray) {
159 if (element.equals(uuid)) return true;
160 }
161 return false;
162 }
163
164 /**
165 * Returns true if there any common ParcelUuids in uuidA and uuidB.
166 *
167 * @param uuidA - List of ParcelUuids
168 * @param uuidB - List of ParcelUuids
169 *
170 */
171 public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
172 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700173
174 if (uuidA == null) {
175 return uuidB.length == 0 ? true : false;
176 }
177
178 if (uuidB == null) {
179 return uuidA.length == 0 ? true : false;
180 }
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700181
182 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid> (Arrays.asList(uuidA));
183 for (ParcelUuid uuid: uuidB) {
184 if (uuidSet.contains(uuid)) return true;
185 }
186 return false;
187 }
188
189 /**
190 * Returns true if all the ParcelUuids in ParcelUuidB are present in
191 * ParcelUuidA
192 *
193 * @param uuidA - Array of ParcelUuidsA
194 * @param uuidB - Array of ParcelUuidsB
195 *
196 */
197 public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
198 if (uuidA == null && uuidB == null) return true;
Jaikumar Ganesh97c84ce2009-09-16 17:50:52 -0700199
200 if (uuidA == null) {
201 return uuidB.length == 0 ? true : false;
202 }
203
204 if (uuidB == null) return true;
Jaikumar Ganesh55a0c032009-09-16 12:30:02 -0700205
206 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid> (Arrays.asList(uuidA));
207 for (ParcelUuid uuid: uuidB) {
208 if (!uuidSet.contains(uuid)) return false;
209 }
210 return true;
211 }
212
Jaikumar Ganesh166a60e2010-12-10 12:48:58 -0800213 /**
214 * 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
217 * @param parcelUuid
218 * @return the service identifier.
219 */
220 public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
221 UUID uuid = parcelUuid.getUuid();
222 long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
223 return (int)value;
224 }
Wei Wang2f7544c2013-10-29 21:05:37 -0700225
226 /**
Wei Wangc3868042014-04-15 14:57:16 -0700227 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
228 * but the returned UUID is always in 128-bit format.
229 * Note UUID is little endian in Bluetooth.
230 *
231 * @param uuidBytes Byte representation of uuid.
232 * @return {@link ParcelUuid} parsed from bytes.
233 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
234 */
235 public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
236 if (uuidBytes == null) {
237 throw new IllegalArgumentException("uuidBytes cannot be null");
238 }
239 int length = uuidBytes.length;
240 if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT &&
241 length != UUID_BYTES_128_BIT) {
242 throw new IllegalArgumentException("uuidBytes length invalid - " + length);
243 }
244
245 // Construct a 128 bit UUID.
246 if (length == UUID_BYTES_128_BIT) {
247 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
248 long msb = buf.getLong(8);
249 long lsb = buf.getLong(0);
250 return new ParcelUuid(new UUID(msb, lsb));
251 }
252
253 // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
254 // 128_bit_value = uuid * 2^96 + BASE_UUID
255 long shortUuid;
256 if (length == UUID_BYTES_16_BIT) {
257 shortUuid = uuidBytes[0] & 0xFF;
258 shortUuid += (uuidBytes[1] & 0xFF) << 8;
259 } else {
260 shortUuid = uuidBytes[0] & 0xFF ;
261 shortUuid += (uuidBytes[1] & 0xFF) << 8;
262 shortUuid += (uuidBytes[2] & 0xFF) << 16;
263 shortUuid += (uuidBytes[3] & 0xFF) << 24;
264 }
265 long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
266 long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
267 return new ParcelUuid(new UUID(msb, lsb));
268 }
269
270 /**
Wei Wang2f7544c2013-10-29 21:05:37 -0700271 * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
Wei Wangc3868042014-04-15 14:57:16 -0700272 *
Wei Wang2f7544c2013-10-29 21:05:37 -0700273 * @param parcelUuid
274 * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
275 */
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700276 public static boolean is16BitUuid(ParcelUuid parcelUuid) {
Wei Wangc3868042014-04-15 14:57:16 -0700277 UUID uuid = parcelUuid.getUuid();
278 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
279 return false;
280 }
281 return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
Wei Wang2f7544c2013-10-29 21:05:37 -0700282 }
Wei Wanga8f5c7b2014-05-07 14:54:43 -0700283
284
285 /**
286 * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
287 *
288 * @param parcelUuid
289 * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
290 */
291 public static boolean is32BitUuid(ParcelUuid parcelUuid) {
292 UUID uuid = parcelUuid.getUuid();
293 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
294 return false;
295 }
296 if (is16BitUuid(parcelUuid)) {
297 return false;
298 }
299 return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
300 }
Jaikumar Ganesh879bf5b2009-05-05 22:26:12 -0700301}