1 /* 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 17 package android.bluetooth; 18 19 import android.annotation.UnsupportedAppUsage; 20 import android.os.Build; 21 import android.os.ParcelUuid; 22 23 import java.nio.ByteBuffer; 24 import java.nio.ByteOrder; 25 import java.util.Arrays; 26 import java.util.HashSet; 27 import java.util.UUID; 28 29 /** 30 * Static helper methods and constants to decode the ParcelUuid of remote devices. 31 * 32 * @hide 33 */ 34 public final class BluetoothUuid { 35 36 /* See Bluetooth Assigned Numbers document - SDP section, to get the values of UUIDs 37 * for the various services. 38 * 39 * The following 128 bit values are calculated as: 40 * uuid * 2^96 + BASE_UUID 41 */ 42 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 43 public static final ParcelUuid AudioSink = 44 ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB"); 45 public static final ParcelUuid AudioSource = 46 ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB"); 47 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 48 public static final ParcelUuid AdvAudioDist = 49 ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB"); 50 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 51 public static final ParcelUuid HSP = 52 ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB"); 53 public static final ParcelUuid HSP_AG = 54 ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB"); 55 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 56 public static final ParcelUuid Handsfree = 57 ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB"); 58 public static final ParcelUuid Handsfree_AG = 59 ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB"); 60 public static final ParcelUuid AvrcpController = 61 ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB"); 62 public static final ParcelUuid AvrcpTarget = 63 ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB"); 64 @UnsupportedAppUsage 65 public static final ParcelUuid ObexObjectPush = 66 ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb"); 67 public static final ParcelUuid Hid = 68 ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb"); 69 @UnsupportedAppUsage 70 public static final ParcelUuid Hogp = 71 ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb"); 72 public static final ParcelUuid PANU = 73 ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB"); 74 @UnsupportedAppUsage 75 public static final ParcelUuid NAP = 76 ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB"); 77 public static final ParcelUuid BNEP = 78 ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB"); 79 public static final ParcelUuid PBAP_PCE = 80 ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB"); 81 @UnsupportedAppUsage 82 public static final ParcelUuid PBAP_PSE = 83 ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB"); 84 public static final ParcelUuid MAP = 85 ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB"); 86 public static final ParcelUuid MNS = 87 ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB"); 88 public static final ParcelUuid MAS = 89 ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB"); 90 public static final ParcelUuid SAP = 91 ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB"); 92 public static final ParcelUuid HearingAid = 93 ParcelUuid.fromString("0000FDF0-0000-1000-8000-00805f9b34fb"); 94 95 public static final ParcelUuid BASE_UUID = 96 ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB"); 97 98 /** Length of bytes for 16 bit UUID */ 99 public static final int UUID_BYTES_16_BIT = 2; 100 /** Length of bytes for 32 bit UUID */ 101 public static final int UUID_BYTES_32_BIT = 4; 102 /** Length of bytes for 128 bit UUID */ 103 public static final int UUID_BYTES_128_BIT = 16; 104 105 @UnsupportedAppUsage 106 public static final ParcelUuid[] RESERVED_UUIDS = { 107 AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget, 108 ObexObjectPush, PANU, NAP, MAP, MNS, MAS, SAP}; 109 110 @UnsupportedAppUsage isAudioSource(ParcelUuid uuid)111 public static boolean isAudioSource(ParcelUuid uuid) { 112 return uuid.equals(AudioSource); 113 } 114 isAudioSink(ParcelUuid uuid)115 public static boolean isAudioSink(ParcelUuid uuid) { 116 return uuid.equals(AudioSink); 117 } 118 119 @UnsupportedAppUsage isAdvAudioDist(ParcelUuid uuid)120 public static boolean isAdvAudioDist(ParcelUuid uuid) { 121 return uuid.equals(AdvAudioDist); 122 } 123 isHandsfree(ParcelUuid uuid)124 public static boolean isHandsfree(ParcelUuid uuid) { 125 return uuid.equals(Handsfree); 126 } 127 isHeadset(ParcelUuid uuid)128 public static boolean isHeadset(ParcelUuid uuid) { 129 return uuid.equals(HSP); 130 } 131 isAvrcpController(ParcelUuid uuid)132 public static boolean isAvrcpController(ParcelUuid uuid) { 133 return uuid.equals(AvrcpController); 134 } 135 136 @UnsupportedAppUsage isAvrcpTarget(ParcelUuid uuid)137 public static boolean isAvrcpTarget(ParcelUuid uuid) { 138 return uuid.equals(AvrcpTarget); 139 } 140 isInputDevice(ParcelUuid uuid)141 public static boolean isInputDevice(ParcelUuid uuid) { 142 return uuid.equals(Hid); 143 } 144 isPanu(ParcelUuid uuid)145 public static boolean isPanu(ParcelUuid uuid) { 146 return uuid.equals(PANU); 147 } 148 isNap(ParcelUuid uuid)149 public static boolean isNap(ParcelUuid uuid) { 150 return uuid.equals(NAP); 151 } 152 isBnep(ParcelUuid uuid)153 public static boolean isBnep(ParcelUuid uuid) { 154 return uuid.equals(BNEP); 155 } 156 isMap(ParcelUuid uuid)157 public static boolean isMap(ParcelUuid uuid) { 158 return uuid.equals(MAP); 159 } 160 isMns(ParcelUuid uuid)161 public static boolean isMns(ParcelUuid uuid) { 162 return uuid.equals(MNS); 163 } 164 isMas(ParcelUuid uuid)165 public static boolean isMas(ParcelUuid uuid) { 166 return uuid.equals(MAS); 167 } 168 isSap(ParcelUuid uuid)169 public static boolean isSap(ParcelUuid uuid) { 170 return uuid.equals(SAP); 171 } 172 173 /** 174 * Returns true if ParcelUuid is present in uuidArray 175 * 176 * @param uuidArray - Array of ParcelUuids 177 * @param uuid 178 */ 179 @UnsupportedAppUsage isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid)180 public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) { 181 if ((uuidArray == null || uuidArray.length == 0) && uuid == null) { 182 return true; 183 } 184 185 if (uuidArray == null) { 186 return false; 187 } 188 189 for (ParcelUuid element : uuidArray) { 190 if (element.equals(uuid)) return true; 191 } 192 return false; 193 } 194 195 /** 196 * Returns true if there any common ParcelUuids in uuidA and uuidB. 197 * 198 * @param uuidA - List of ParcelUuids 199 * @param uuidB - List of ParcelUuids 200 */ 201 @UnsupportedAppUsage containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB)202 public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { 203 if (uuidA == null && uuidB == null) return true; 204 205 if (uuidA == null) { 206 return uuidB.length == 0; 207 } 208 209 if (uuidB == null) { 210 return uuidA.length == 0; 211 } 212 213 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA)); 214 for (ParcelUuid uuid : uuidB) { 215 if (uuidSet.contains(uuid)) return true; 216 } 217 return false; 218 } 219 220 /** 221 * Returns true if all the ParcelUuids in ParcelUuidB are present in 222 * ParcelUuidA 223 * 224 * @param uuidA - Array of ParcelUuidsA 225 * @param uuidB - Array of ParcelUuidsB 226 */ containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB)227 public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { 228 if (uuidA == null && uuidB == null) return true; 229 230 if (uuidA == null) { 231 return uuidB.length == 0; 232 } 233 234 if (uuidB == null) return true; 235 236 HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA)); 237 for (ParcelUuid uuid : uuidB) { 238 if (!uuidSet.contains(uuid)) return false; 239 } 240 return true; 241 } 242 243 /** 244 * Extract the Service Identifier or the actual uuid from the Parcel Uuid. 245 * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid, 246 * this function will return 110B 247 * 248 * @param parcelUuid 249 * @return the service identifier. 250 */ getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid)251 public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) { 252 UUID uuid = parcelUuid.getUuid(); 253 long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32; 254 return (int) value; 255 } 256 257 /** 258 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID, 259 * but the returned UUID is always in 128-bit format. 260 * Note UUID is little endian in Bluetooth. 261 * 262 * @param uuidBytes Byte representation of uuid. 263 * @return {@link ParcelUuid} parsed from bytes. 264 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed. 265 */ parseUuidFrom(byte[] uuidBytes)266 public static ParcelUuid parseUuidFrom(byte[] uuidBytes) { 267 if (uuidBytes == null) { 268 throw new IllegalArgumentException("uuidBytes cannot be null"); 269 } 270 int length = uuidBytes.length; 271 if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT 272 && length != UUID_BYTES_128_BIT) { 273 throw new IllegalArgumentException("uuidBytes length invalid - " + length); 274 } 275 276 // Construct a 128 bit UUID. 277 if (length == UUID_BYTES_128_BIT) { 278 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN); 279 long msb = buf.getLong(8); 280 long lsb = buf.getLong(0); 281 return new ParcelUuid(new UUID(msb, lsb)); 282 } 283 284 // For 16 bit and 32 bit UUID we need to convert them to 128 bit value. 285 // 128_bit_value = uuid * 2^96 + BASE_UUID 286 long shortUuid; 287 if (length == UUID_BYTES_16_BIT) { 288 shortUuid = uuidBytes[0] & 0xFF; 289 shortUuid += (uuidBytes[1] & 0xFF) << 8; 290 } else { 291 shortUuid = uuidBytes[0] & 0xFF; 292 shortUuid += (uuidBytes[1] & 0xFF) << 8; 293 shortUuid += (uuidBytes[2] & 0xFF) << 16; 294 shortUuid += (uuidBytes[3] & 0xFF) << 24; 295 } 296 long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32); 297 long lsb = BASE_UUID.getUuid().getLeastSignificantBits(); 298 return new ParcelUuid(new UUID(msb, lsb)); 299 } 300 301 /** 302 * Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or 303 * 128-bit UUID, Note returned value is little endian (Bluetooth). 304 * 305 * @param uuid uuid to parse. 306 * @return shortest representation of {@code uuid} as bytes. 307 * @throws IllegalArgumentException If the {@code uuid} is null. 308 */ uuidToBytes(ParcelUuid uuid)309 public static byte[] uuidToBytes(ParcelUuid uuid) { 310 if (uuid == null) { 311 throw new IllegalArgumentException("uuid cannot be null"); 312 } 313 314 if (is16BitUuid(uuid)) { 315 byte[] uuidBytes = new byte[UUID_BYTES_16_BIT]; 316 int uuidVal = getServiceIdentifierFromParcelUuid(uuid); 317 uuidBytes[0] = (byte) (uuidVal & 0xFF); 318 uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8); 319 return uuidBytes; 320 } 321 322 if (is32BitUuid(uuid)) { 323 byte[] uuidBytes = new byte[UUID_BYTES_32_BIT]; 324 int uuidVal = getServiceIdentifierFromParcelUuid(uuid); 325 uuidBytes[0] = (byte) (uuidVal & 0xFF); 326 uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8); 327 uuidBytes[2] = (byte) ((uuidVal & 0xFF0000) >> 16); 328 uuidBytes[3] = (byte) ((uuidVal & 0xFF000000) >> 24); 329 return uuidBytes; 330 } 331 332 // Construct a 128 bit UUID. 333 long msb = uuid.getUuid().getMostSignificantBits(); 334 long lsb = uuid.getUuid().getLeastSignificantBits(); 335 336 byte[] uuidBytes = new byte[UUID_BYTES_128_BIT]; 337 ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN); 338 buf.putLong(8, msb); 339 buf.putLong(0, lsb); 340 return uuidBytes; 341 } 342 343 /** 344 * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid. 345 * 346 * @param parcelUuid 347 * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise. 348 */ 349 @UnsupportedAppUsage is16BitUuid(ParcelUuid parcelUuid)350 public static boolean is16BitUuid(ParcelUuid parcelUuid) { 351 UUID uuid = parcelUuid.getUuid(); 352 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) { 353 return false; 354 } 355 return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L); 356 } 357 358 359 /** 360 * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid. 361 * 362 * @param parcelUuid 363 * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise. 364 */ 365 @UnsupportedAppUsage is32BitUuid(ParcelUuid parcelUuid)366 public static boolean is32BitUuid(ParcelUuid parcelUuid) { 367 UUID uuid = parcelUuid.getUuid(); 368 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) { 369 return false; 370 } 371 if (is16BitUuid(parcelUuid)) { 372 return false; 373 } 374 return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L); 375 } 376 } 377