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.os.ParcelUuid; 20 21 import java.nio.ByteBuffer; 22 import java.nio.ByteOrder; 23 import java.util.Arrays; 24 import java.util.HashSet; 25 import java.util.UUID; 26 27 /** 28 * Static helper methods and constants to decode the ParcelUuid of remote devices. 29 * @hide 30 */ 31 public 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 */ 39 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"); 47 public static final ParcelUuid HSP_AG = 48 ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB"); 49 public static final ParcelUuid Handsfree = 50 ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB"); 51 public static final ParcelUuid Handsfree_AG = 52 ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB"); 53 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"); 59 public static final ParcelUuid Hid = 60 ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb"); 61 public static final ParcelUuid Hogp = 62 ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb"); 63 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"); 67 public static final ParcelUuid BNEP = 68 ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB"); 69 public static final ParcelUuid PBAP_PCE = 70 ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB"); 71 public static final ParcelUuid PBAP_PSE = 72 ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB"); 73 public static final ParcelUuid MAP = 74 ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB"); 75 public static final ParcelUuid MNS = 76 ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB"); 77 public static final ParcelUuid MAS = 78 ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB"); 79 public static final ParcelUuid SAP = 80 ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB"); 81 82 public static final ParcelUuid BASE_UUID = 83 ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB"); 84 85 /** 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; 91 92 public static final ParcelUuid[] RESERVED_UUIDS = { 93 AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget, 94 ObexObjectPush, PANU, NAP, MAP, MNS, MAS, SAP}; 95 isAudioSource(ParcelUuid uuid)96 public static boolean isAudioSource(ParcelUuid uuid) { 97 return uuid.equals(AudioSource); 98 } 99 isAudioSink(ParcelUuid uuid)100 public static boolean isAudioSink(ParcelUuid uuid) { 101 return uuid.equals(AudioSink); 102 } 103 isAdvAudioDist(ParcelUuid uuid)104 public static boolean isAdvAudioDist(ParcelUuid uuid) { 105 return uuid.equals(AdvAudioDist); 106 } 107 isHandsfree(ParcelUuid uuid)108 public static boolean isHandsfree(ParcelUuid uuid) { 109 return uuid.equals(Handsfree); 110 } 111 isHeadset(ParcelUuid uuid)112 public static boolean isHeadset(ParcelUuid uuid) { 113 return uuid.equals(HSP); 114 } 115 isAvrcpController(ParcelUuid uuid)116 public static boolean isAvrcpController(ParcelUuid uuid) { 117 return uuid.equals(AvrcpController); 118 } 119 isAvrcpTarget(ParcelUuid uuid)120 public static boolean isAvrcpTarget(ParcelUuid uuid) { 121 return uuid.equals(AvrcpTarget); 122 } 123 isInputDevice(ParcelUuid uuid)124 public static boolean isInputDevice(ParcelUuid uuid) { 125 return uuid.equals(Hid); 126 } 127 isPanu(ParcelUuid uuid)128 public static boolean isPanu(ParcelUuid uuid) { 129 return uuid.equals(PANU); 130 } 131 isNap(ParcelUuid uuid)132 public static boolean isNap(ParcelUuid uuid) { 133 return uuid.equals(NAP); 134 } 135 isBnep(ParcelUuid uuid)136 public static boolean isBnep(ParcelUuid uuid) { 137 return uuid.equals(BNEP); 138 } isMap(ParcelUuid uuid)139 public static boolean isMap(ParcelUuid uuid) { 140 return uuid.equals(MAP); 141 } isMns(ParcelUuid uuid)142 public static boolean isMns(ParcelUuid uuid) { 143 return uuid.equals(MNS); 144 } isMas(ParcelUuid uuid)145 public static boolean isMas(ParcelUuid uuid) { 146 return uuid.equals(MAS); 147 } isSap(ParcelUuid uuid)148 public static boolean isSap(ParcelUuid uuid) { 149 return uuid.equals(SAP); 150 } 151 152 /** 153 * Returns true if ParcelUuid is present in uuidArray 154 * 155 * @param uuidArray - Array of ParcelUuids 156 * @param uuid 157 */ isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid)158 public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) { 159 if ((uuidArray == null || uuidArray.length == 0) && uuid == null) 160 return true; 161 162 if (uuidArray == null) 163 return false; 164 165 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 */ containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB)178 public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { 179 if (uuidA == null && uuidB == null) return true; 180 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 } 188 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 */ containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB)204 public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { 205 if (uuidA == null && uuidB == null) return true; 206 207 if (uuidA == null) { 208 return uuidB.length == 0 ? true : false; 209 } 210 211 if (uuidB == null) return true; 212 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 220 /** 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 */ getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid)227 public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) { 228 UUID uuid = parcelUuid.getUuid(); 229 long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32; 230 return (int)value; 231 } 232 233 /** 234 * 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 */ parseUuidFrom(byte[] uuidBytes)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 /** 278 * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid. 279 * 280 * @param parcelUuid 281 * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise. 282 */ is16BitUuid(ParcelUuid parcelUuid)283 public static boolean is16BitUuid(ParcelUuid parcelUuid) { 284 UUID uuid = parcelUuid.getUuid(); 285 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) { 286 return false; 287 } 288 return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L); 289 } 290 291 292 /** 293 * Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid. 294 * 295 * @param parcelUuid 296 * @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise. 297 */ is32BitUuid(ParcelUuid parcelUuid)298 public static boolean is32BitUuid(ParcelUuid parcelUuid) { 299 UUID uuid = parcelUuid.getUuid(); 300 if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) { 301 return false; 302 } 303 if (is16BitUuid(parcelUuid)) { 304 return false; 305 } 306 return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L); 307 } 308 } 309