| 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 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 21 | import java.util.Arrays; |
| 22 | import java.util.HashSet; |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame^] | 23 | import java.util.UUID; |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 26 | * Static helper methods and constants to decode the ParcelUuid of remote devices. |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 27 | * @hide |
| 28 | */ |
| 29 | public final class BluetoothUuid { |
| 30 | |
| 31 | /* See Bluetooth Assigned Numbers document - SDP section, to get the values of UUIDs |
| 32 | * for the various services. |
| 33 | * |
| 34 | * The following 128 bit values are calculated as: |
| 35 | * uuid * 2^96 + BASE_UUID |
| 36 | */ |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 37 | public static final ParcelUuid AudioSink = |
| 38 | ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB"); |
| 39 | public static final ParcelUuid AudioSource = |
| 40 | ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB"); |
| 41 | public static final ParcelUuid AdvAudioDist = |
| 42 | ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB"); |
| 43 | public static final ParcelUuid HSP = |
| 44 | ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame^] | 45 | public static final ParcelUuid HSP_AG = |
| 46 | ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 47 | public static final ParcelUuid Handsfree = |
| 48 | ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame^] | 49 | public static final ParcelUuid Handsfree_AG = |
| 50 | ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 51 | public static final ParcelUuid AvrcpController = |
| 52 | ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB"); |
| 53 | public static final ParcelUuid AvrcpTarget = |
| 54 | ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB"); |
| 55 | public static final ParcelUuid ObexObjectPush = |
| 56 | ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb"); |
| Jaikumar Ganesh | 5d0b83e | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 57 | public static final ParcelUuid Hid = |
| Adam Powell | 026e857 | 2010-06-21 16:23:42 -0700 | [diff] [blame] | 58 | ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb"); |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 59 | public static final ParcelUuid PANU = |
| 60 | ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB"); |
| 61 | public static final ParcelUuid NAP = |
| 62 | ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 63 | public static final ParcelUuid BNEP = |
| 64 | ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame^] | 65 | public static final ParcelUuid PBAP_PSE = |
| 66 | ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB"); |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 67 | |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 68 | public static final ParcelUuid[] RESERVED_UUIDS = { |
| 69 | AudioSink, AudioSource, AdvAudioDist, HSP, Handsfree, AvrcpController, AvrcpTarget, |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 70 | ObexObjectPush, PANU, NAP}; |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 71 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 72 | public static boolean isAudioSource(ParcelUuid uuid) { |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 73 | return uuid.equals(AudioSource); |
| 74 | } |
| 75 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 76 | public static boolean isAudioSink(ParcelUuid uuid) { |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 77 | return uuid.equals(AudioSink); |
| 78 | } |
| 79 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 80 | public static boolean isAdvAudioDist(ParcelUuid uuid) { |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 81 | return uuid.equals(AdvAudioDist); |
| 82 | } |
| 83 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 84 | public static boolean isHandsfree(ParcelUuid uuid) { |
| Jaikumar Ganesh | 4eb5ccc | 2009-07-14 12:21:26 -0700 | [diff] [blame] | 85 | return uuid.equals(Handsfree); |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 88 | public static boolean isHeadset(ParcelUuid uuid) { |
| Jaikumar Ganesh | 671065d | 2009-07-30 13:32:25 -0700 | [diff] [blame] | 89 | return uuid.equals(HSP); |
| 90 | } |
| 91 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 92 | public static boolean isAvrcpController(ParcelUuid uuid) { |
| Jaikumar Ganesh | 671065d | 2009-07-30 13:32:25 -0700 | [diff] [blame] | 93 | return uuid.equals(AvrcpController); |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 94 | } |
| Jackson Fan | 0d53876 | 2009-08-19 21:01:29 +0800 | [diff] [blame] | 95 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 96 | public static boolean isAvrcpTarget(ParcelUuid uuid) { |
| Jackson Fan | 0d53876 | 2009-08-19 21:01:29 +0800 | [diff] [blame] | 97 | return uuid.equals(AvrcpTarget); |
| 98 | } |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 99 | |
| Jaikumar Ganesh | 5d0b83e | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 100 | public static boolean isInputDevice(ParcelUuid uuid) { |
| 101 | return uuid.equals(Hid); |
| 102 | } |
| 103 | |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 104 | public static boolean isPanu(ParcelUuid uuid) { |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 105 | return uuid.equals(PANU); |
| 106 | } |
| 107 | |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 108 | public static boolean isNap(ParcelUuid uuid) { |
| Danica Chang | 41f1a09 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 109 | return uuid.equals(NAP); |
| 110 | } |
| Jaikumar Ganesh | 43d545d | 2010-08-23 18:32:03 -0700 | [diff] [blame] | 111 | |
| 112 | public static boolean isBnep(ParcelUuid uuid) { |
| 113 | return uuid.equals(BNEP); |
| 114 | } |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Returns true if ParcelUuid is present in uuidArray |
| 117 | * |
| 118 | * @param uuidArray - Array of ParcelUuids |
| 119 | * @param uuid |
| 120 | */ |
| 121 | public static boolean isUuidPresent(ParcelUuid[] uuidArray, ParcelUuid uuid) { |
| Jaikumar Ganesh | 391e8a7 | 2009-09-21 12:48:51 -0700 | [diff] [blame] | 122 | if ((uuidArray == null || uuidArray.length == 0) && uuid == null) |
| 123 | return true; |
| 124 | |
| 125 | if (uuidArray == null) |
| 126 | return false; |
| 127 | |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 128 | for (ParcelUuid element: uuidArray) { |
| 129 | if (element.equals(uuid)) return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns true if there any common ParcelUuids in uuidA and uuidB. |
| 136 | * |
| 137 | * @param uuidA - List of ParcelUuids |
| 138 | * @param uuidB - List of ParcelUuids |
| 139 | * |
| 140 | */ |
| 141 | public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { |
| 142 | if (uuidA == null && uuidB == null) return true; |
| Jaikumar Ganesh | 97c84ce | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 143 | |
| 144 | if (uuidA == null) { |
| 145 | return uuidB.length == 0 ? true : false; |
| 146 | } |
| 147 | |
| 148 | if (uuidB == null) { |
| 149 | return uuidA.length == 0 ? true : false; |
| 150 | } |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 151 | |
| 152 | HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid> (Arrays.asList(uuidA)); |
| 153 | for (ParcelUuid uuid: uuidB) { |
| 154 | if (uuidSet.contains(uuid)) return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns true if all the ParcelUuids in ParcelUuidB are present in |
| 161 | * ParcelUuidA |
| 162 | * |
| 163 | * @param uuidA - Array of ParcelUuidsA |
| 164 | * @param uuidB - Array of ParcelUuidsB |
| 165 | * |
| 166 | */ |
| 167 | public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) { |
| 168 | if (uuidA == null && uuidB == null) return true; |
| Jaikumar Ganesh | 97c84ce | 2009-09-16 17:50:52 -0700 | [diff] [blame] | 169 | |
| 170 | if (uuidA == null) { |
| 171 | return uuidB.length == 0 ? true : false; |
| 172 | } |
| 173 | |
| 174 | if (uuidB == null) return true; |
| Jaikumar Ganesh | 55a0c03 | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 175 | |
| 176 | HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid> (Arrays.asList(uuidA)); |
| 177 | for (ParcelUuid uuid: uuidB) { |
| 178 | if (!uuidSet.contains(uuid)) return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
| Jaikumar Ganesh | 166a60e | 2010-12-10 12:48:58 -0800 | [diff] [blame^] | 183 | /** |
| 184 | * Extract the Service Identifier or the actual uuid from the Parcel Uuid. |
| 185 | * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid, |
| 186 | * this function will return 110B |
| 187 | * @param parcelUuid |
| 188 | * @return the service identifier. |
| 189 | */ |
| 190 | public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) { |
| 191 | UUID uuid = parcelUuid.getUuid(); |
| 192 | long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32; |
| 193 | return (int)value; |
| 194 | } |
| Jaikumar Ganesh | 879bf5b | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 195 | } |