| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 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 | |
| Nick Pelly | 03759e5 | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 19 | import android.os.ParcelUuid; |
| 20 | |
| Wei Wang | c386804 | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 21 | import java.nio.ByteBuffer; |
| 22 | import java.nio.ByteOrder; |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 23 | import java.util.Arrays; |
| 24 | import java.util.HashSet; |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 25 | import java.util.UUID; |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 28 | * Static helper methods and constants to decode the ParcelUuid of remote devices. |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 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 | */ |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 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"); |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 47 | public static final ParcelUuid HSP_AG = |
| 48 | ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 49 | public static final ParcelUuid Handsfree = |
| 50 | ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 51 | public static final ParcelUuid Handsfree_AG = |
| 52 | ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 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"); |
| Jaikumar Ganesh | 5d0b83e | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 59 | public static final ParcelUuid Hid = |
| Adam Powell | 026e857 | 2010-06-21 16:23:42 -0700 | [diff] [blame] | 60 | ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb"); |
| Andre Eisenbach | 1f0c3da | 2013-03-07 18:07:35 -0800 | [diff] [blame] | 61 | public static final ParcelUuid Hogp = |
| 62 | ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb"); |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 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"); |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 67 | public static final ParcelUuid BNEP = |
| 68 | ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 69 | public static final ParcelUuid PBAP_PSE = |
| 70 | ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB"); |
| Matthew Xie | ce14522 | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 71 | public static final ParcelUuid MAP = |
| Kim Schulz | 6816ee2 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 72 | ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB"); |
| Matthew Xie | ce14522 | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 73 | public static final ParcelUuid MNS = |
| 74 | ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB"); |
| Kim Schulz | 6816ee2 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 75 | public static final ParcelUuid MAS = |
| 76 | ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB"); |
| 77 | |
| Wei Wang | 2f7544c | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 78 | public static final ParcelUuid BASE_UUID = |
| 79 | ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB"); |
| 80 | |
| Wei Wang | c386804 | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 81 | /** 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 Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 87 | |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 88 | public static final ParcelUuid[] RESERVED_UUIDS = { |
| 89 | AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget, |
| Kim Schulz | 6816ee2 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 90 | ObexObjectPush, PANU, NAP, MAP, MNS, MAS}; |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 91 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 92 | public static boolean isAudioSource(ParcelUuid uuid) { |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 93 | return uuid.equals(AudioSource); |
| 94 | } |
| 95 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 96 | public static boolean isAudioSink(ParcelUuid uuid) { |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 97 | return uuid.equals(AudioSink); |
| 98 | } |
| 99 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 100 | public static boolean isAdvAudioDist(ParcelUuid uuid) { |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 101 | return uuid.equals(AdvAudioDist); |
| 102 | } |
| 103 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 104 | public static boolean isHandsfree(ParcelUuid uuid) { |
| Jaikumar Ganesh | 4eb5ccc | 2009-07-14 12:21:26 -0700 | [diff] [blame] | 105 | return uuid.equals(Handsfree); |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 108 | public static boolean isHeadset(ParcelUuid uuid) { |
| Jaikumar Ganesh | 671065d | 2009-07-30 13:32:25 -0700 | [diff] [blame] | 109 | return uuid.equals(HSP); |
| 110 | } |
| 111 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 112 | public static boolean isAvrcpController(ParcelUuid uuid) { |
| Jaikumar Ganesh | 671065d | 2009-07-30 13:32:25 -0700 | [diff] [blame] | 113 | return uuid.equals(AvrcpController); |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 114 | } |
| Jackson Fan | 0d53876 | 2009-08-19 21:01:29 +0800 | [diff] [blame] | 115 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 116 | public static boolean isAvrcpTarget(ParcelUuid uuid) { |
| Jackson Fan | 0d53876 | 2009-08-19 21:01:29 +0800 | [diff] [blame] | 117 | return uuid.equals(AvrcpTarget); |
| 118 | } |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 119 | |
| Jaikumar Ganesh | 5d0b83e | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 120 | public static boolean isInputDevice(ParcelUuid uuid) { |
| 121 | return uuid.equals(Hid); |
| 122 | } |
| 123 | |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 124 | public static boolean isPanu(ParcelUuid uuid) { |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 125 | return uuid.equals(PANU); |
| 126 | } |
| 127 | |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 128 | public static boolean isNap(ParcelUuid uuid) { |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 129 | return uuid.equals(NAP); |
| 130 | } |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 131 | |
| 132 | public static boolean isBnep(ParcelUuid uuid) { |
| 133 | return uuid.equals(BNEP); |
| 134 | } |
| Matthew Xie | ce14522 | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 135 | 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 Schulz | 6816ee2 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 141 | public static boolean isMas(ParcelUuid uuid) { |
| 142 | return uuid.equals(MAS); |
| 143 | } |
| Matthew Xie | ce14522 | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 144 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 145 | /** |
| 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 Ganesh | 391e8a7 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 152 | if ((uuidArray == null || uuidArray.length == 0) && uuid == null) |
| 153 | return true; |
| 154 | |
| 155 | if (uuidArray == null) |
| 156 | return false; |
| 157 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 158 | 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 Ganesh | 97c84ce | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 173 | |
| 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 Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 181 | |
| 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 Ganesh | 97c84ce | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 199 | |
| 200 | if (uuidA == null) { |
| 201 | return uuidB.length == 0 ? true : false; |
| 202 | } |
| 203 | |
| 204 | if (uuidB == null) return true; |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 205 | |
| 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 Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 213 | /** |
| 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 Wang | 2f7544c | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 225 | |
| 226 | /** |
| Wei Wang | c386804 | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 227 | * 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 Wang | 2f7544c | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 271 | * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid. |
| Wei Wang | c386804 | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 272 | * |
| Wei Wang | 2f7544c | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 273 | * @param parcelUuid |
| 274 | * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise. |
| 275 | */ |
| Wei Wang | a8f5c7b | 2014-05-07 14:54:43 -0700 | [diff] [blame^] | 276 | public static boolean is16BitUuid(ParcelUuid parcelUuid) { |
| Wei Wang | c386804 | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 277 | UUID uuid = parcelUuid.getUuid(); |
| 278 | if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) { |
| 279 | return false; |
| 280 | } |
| 281 | return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L); |
| Wei Wang | 2f7544c | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 282 | } |
| Wei Wang | a8f5c7b | 2014-05-07 14:54:43 -0700 | [diff] [blame^] | 283 | |
| 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 Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 301 | } |