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