Jaikumar Ganesh | d5ac1ae | 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 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 20 | import android.os.Build; |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 21 | import android.os.ParcelUuid; |
| 22 | |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 23 | import java.nio.ByteBuffer; |
| 24 | import java.nio.ByteOrder; |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 25 | import java.util.Arrays; |
| 26 | import java.util.HashSet; |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 27 | import java.util.UUID; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 30 | * Static helper methods and constants to decode the ParcelUuid of remote devices. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 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 | */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 42 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 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"); |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 47 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 48 | public static final ParcelUuid AdvAudioDist = |
| 49 | ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB"); |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 50 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 51 | public static final ParcelUuid HSP = |
| 52 | ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB"); |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 53 | public static final ParcelUuid HSP_AG = |
| 54 | ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB"); |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 55 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 56 | public static final ParcelUuid Handsfree = |
| 57 | ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB"); |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 58 | public static final ParcelUuid Handsfree_AG = |
| 59 | ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB"); |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 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"); |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 64 | @UnsupportedAppUsage |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 65 | public static final ParcelUuid ObexObjectPush = |
| 66 | ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb"); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 67 | public static final ParcelUuid Hid = |
Adam Powell | df7627d | 2010-06-21 16:23:42 -0700 | [diff] [blame] | 68 | ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb"); |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 69 | @UnsupportedAppUsage |
Andre Eisenbach | c60bed4 | 2013-03-07 18:07:35 -0800 | [diff] [blame] | 70 | public static final ParcelUuid Hogp = |
| 71 | ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb"); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 72 | public static final ParcelUuid PANU = |
| 73 | ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB"); |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 74 | @UnsupportedAppUsage |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 75 | public static final ParcelUuid NAP = |
| 76 | ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB"); |
Jaikumar Ganesh | 3453618 | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 77 | public static final ParcelUuid BNEP = |
| 78 | ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB"); |
Matthew Xie | 6862361 | 2014-11-20 21:53:26 -0800 | [diff] [blame] | 79 | public static final ParcelUuid PBAP_PCE = |
| 80 | ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB"); |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 81 | @UnsupportedAppUsage |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 82 | public static final ParcelUuid PBAP_PSE = |
| 83 | ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 84 | public static final ParcelUuid MAP = |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 85 | ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 86 | public static final ParcelUuid MNS = |
| 87 | ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 88 | public static final ParcelUuid MAS = |
| 89 | ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 90 | public static final ParcelUuid SAP = |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 91 | ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB"); |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 92 | public static final ParcelUuid HearingAid = |
Jakub Pawlowski | 5027679 | 2018-02-22 15:10:08 -0800 | [diff] [blame] | 93 | ParcelUuid.fromString("0000FDF0-0000-1000-8000-00805f9b34fb"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 94 | |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 95 | public static final ParcelUuid BASE_UUID = |
| 96 | ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB"); |
| 97 | |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 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; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 104 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 105 | @UnsupportedAppUsage |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 106 | public static final ParcelUuid[] RESERVED_UUIDS = { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 107 | AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget, |
| 108 | ObexObjectPush, PANU, NAP, MAP, MNS, MAS, SAP}; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 109 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 110 | @UnsupportedAppUsage |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 111 | public static boolean isAudioSource(ParcelUuid uuid) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 112 | return uuid.equals(AudioSource); |
| 113 | } |
| 114 | |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 115 | public static boolean isAudioSink(ParcelUuid uuid) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 116 | return uuid.equals(AudioSink); |
| 117 | } |
| 118 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 119 | @UnsupportedAppUsage |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 120 | public static boolean isAdvAudioDist(ParcelUuid uuid) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 121 | return uuid.equals(AdvAudioDist); |
| 122 | } |
| 123 | |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 124 | public static boolean isHandsfree(ParcelUuid uuid) { |
Jaikumar Ganesh | 7f74d53 | 2009-07-14 12:21:26 -0700 | [diff] [blame] | 125 | return uuid.equals(Handsfree); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 128 | public static boolean isHeadset(ParcelUuid uuid) { |
Jaikumar Ganesh | ade4052 | 2009-07-30 13:32:25 -0700 | [diff] [blame] | 129 | return uuid.equals(HSP); |
| 130 | } |
| 131 | |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 132 | public static boolean isAvrcpController(ParcelUuid uuid) { |
Jaikumar Ganesh | ade4052 | 2009-07-30 13:32:25 -0700 | [diff] [blame] | 133 | return uuid.equals(AvrcpController); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 134 | } |
Jackson Fan | 9abcf09 | 2009-08-19 21:01:29 +0800 | [diff] [blame] | 135 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 136 | @UnsupportedAppUsage |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 137 | public static boolean isAvrcpTarget(ParcelUuid uuid) { |
Jackson Fan | 9abcf09 | 2009-08-19 21:01:29 +0800 | [diff] [blame] | 138 | return uuid.equals(AvrcpTarget); |
| 139 | } |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 140 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 141 | public static boolean isInputDevice(ParcelUuid uuid) { |
| 142 | return uuid.equals(Hid); |
| 143 | } |
| 144 | |
Jaikumar Ganesh | 3453618 | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 145 | public static boolean isPanu(ParcelUuid uuid) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 146 | return uuid.equals(PANU); |
| 147 | } |
| 148 | |
Jaikumar Ganesh | 3453618 | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 149 | public static boolean isNap(ParcelUuid uuid) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 150 | return uuid.equals(NAP); |
| 151 | } |
Jaikumar Ganesh | 3453618 | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 152 | |
| 153 | public static boolean isBnep(ParcelUuid uuid) { |
| 154 | return uuid.equals(BNEP); |
| 155 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 156 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 157 | public static boolean isMap(ParcelUuid uuid) { |
| 158 | return uuid.equals(MAP); |
| 159 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 160 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 161 | public static boolean isMns(ParcelUuid uuid) { |
| 162 | return uuid.equals(MNS); |
| 163 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 164 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 165 | public static boolean isMas(ParcelUuid uuid) { |
| 166 | return uuid.equals(MAS); |
| 167 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 168 | |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 169 | public static boolean isSap(ParcelUuid uuid) { |
| 170 | return uuid.equals(SAP); |
| 171 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 172 | |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Returns true if ParcelUuid is present in uuidArray |
| 175 | * |
| 176 | * @param uuidArray - Array of ParcelUuids |
| 177 | * @param uuid |
| 178 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 179 | @UnsupportedAppUsage |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 180 | public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 181 | if ((uuidArray == null || uuidArray.length == 0) && uuid == null) { |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 182 | return true; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 183 | } |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 184 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 185 | if (uuidArray == null) { |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 186 | return false; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 187 | } |
Jaikumar Ganesh | 10eac97 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 188 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 189 | for (ParcelUuid element : uuidArray) { |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 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 |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 200 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 201 | @UnsupportedAppUsage |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 202 | public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { |
| 203 | if (uuidA == null && uuidB == null) return true; |
Jaikumar Ganesh | dca2f0f | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 204 | |
| 205 | if (uuidA == null) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 206 | return uuidB.length == 0; |
Jaikumar Ganesh | dca2f0f | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (uuidB == null) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 210 | return uuidA.length == 0; |
Jaikumar Ganesh | dca2f0f | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 211 | } |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 212 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 213 | HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA)); |
| 214 | for (ParcelUuid uuid : uuidB) { |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 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 |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 226 | */ |
| 227 | public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { |
| 228 | if (uuidA == null && uuidB == null) return true; |
Jaikumar Ganesh | dca2f0f | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 229 | |
| 230 | if (uuidA == null) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 231 | return uuidB.length == 0; |
Jaikumar Ganesh | dca2f0f | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | if (uuidB == null) return true; |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 235 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 236 | HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA)); |
| 237 | for (ParcelUuid uuid : uuidB) { |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 238 | if (!uuidSet.contains(uuid)) return false; |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 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 |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 247 | * |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 248 | * @param parcelUuid |
| 249 | * @return the service identifier. |
| 250 | */ |
| 251 | public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) { |
| 252 | UUID uuid = parcelUuid.getUuid(); |
Ruina Liu | 044e0fe | 2017-07-22 18:41:48 +0800 | [diff] [blame] | 253 | long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 254 | return (int) value; |
Jaikumar Ganesh | 84690c8 | 2010-12-10 12:48:58 -0800 | [diff] [blame] | 255 | } |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 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 | */ |
| 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; |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 271 | if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT |
| 272 | && length != UUID_BYTES_128_BIT) { |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 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 { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 291 | shortUuid = uuidBytes[0] & 0xFF; |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 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 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 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). |
Jakub Pawlowski | f67ee83 | 2016-10-24 13:56:54 -0700 | [diff] [blame] | 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 | */ |
| 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); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 317 | uuidBytes[0] = (byte) (uuidVal & 0xFF); |
| 318 | uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8); |
Jakub Pawlowski | f67ee83 | 2016-10-24 13:56:54 -0700 | [diff] [blame] | 319 | return uuidBytes; |
| 320 | } |
| 321 | |
| 322 | if (is32BitUuid(uuid)) { |
| 323 | byte[] uuidBytes = new byte[UUID_BYTES_32_BIT]; |
| 324 | int uuidVal = getServiceIdentifierFromParcelUuid(uuid); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 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); |
Jakub Pawlowski | f67ee83 | 2016-10-24 13:56:54 -0700 | [diff] [blame] | 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 | /** |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 344 | * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid. |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 345 | * |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 346 | * @param parcelUuid |
| 347 | * @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise. |
| 348 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 349 | @UnsupportedAppUsage |
Wei Wang | 5180084 | 2014-05-07 14:54:43 -0700 | [diff] [blame] | 350 | public static boolean is16BitUuid(ParcelUuid parcelUuid) { |
Wei Wang | 82500dd | 2014-04-15 14:57:16 -0700 | [diff] [blame] | 351 | UUID uuid = parcelUuid.getUuid(); |
| 352 | if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) { |
| 353 | return false; |
| 354 | } |
| 355 | return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L); |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 356 | } |
Wei Wang | 5180084 | 2014-05-07 14:54:43 -0700 | [diff] [blame] | 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 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 365 | @UnsupportedAppUsage |
Wei Wang | 5180084 | 2014-05-07 14:54:43 -0700 | [diff] [blame] | 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 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 376 | } |