1 /* 2 * Copyright (C) 2008 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.annotation.Nullable; 20 import android.annotation.SystemApi; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.os.Build; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.nio.ByteBuffer; 27 import java.nio.ByteOrder; 28 import java.util.Arrays; 29 30 /** 31 * Represents a Bluetooth class, which describes general characteristics 32 * and capabilities of a device. For example, a Bluetooth class will 33 * specify the general device type such as a phone, a computer, or 34 * headset, and whether it's capable of services such as audio or telephony. 35 * 36 * <p>Every Bluetooth class is composed of zero or more service classes, and 37 * exactly one device class. The device class is further broken down into major 38 * and minor device class components. 39 * 40 * <p>{@link BluetoothClass} is useful as a hint to roughly describe a device 41 * (for example to show an icon in the UI), but does not reliably describe which 42 * Bluetooth profiles or services are actually supported by a device. Accurate 43 * service discovery is done through SDP requests, which are automatically 44 * performed when creating an RFCOMM socket with {@link 45 * BluetoothDevice#createRfcommSocketToServiceRecord} and {@link 46 * BluetoothAdapter#listenUsingRfcommWithServiceRecord}</p> 47 * 48 * <p>Use {@link BluetoothDevice#getBluetoothClass} to retrieve the class for 49 * a remote device. 50 * 51 * <!-- 52 * The Bluetooth class is a 32 bit field. The format of these bits is defined at 53 * http://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm 54 * (login required). This class contains that 32 bit field, and provides 55 * constants and methods to determine which Service Class(es) and Device Class 56 * are encoded in that field. 57 * --> 58 */ 59 public final class BluetoothClass implements Parcelable { 60 /** 61 * Legacy error value. Applications should use null instead. 62 * 63 * @hide 64 */ 65 public static final int ERROR = 0xFF000000; 66 67 private final int mClass; 68 69 /** @hide */ 70 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) BluetoothClass(int classInt)71 public BluetoothClass(int classInt) { 72 mClass = classInt; 73 } 74 75 @Override equals(@ullable Object o)76 public boolean equals(@Nullable Object o) { 77 if (o instanceof BluetoothClass) { 78 return mClass == ((BluetoothClass) o).mClass; 79 } 80 return false; 81 } 82 83 @Override hashCode()84 public int hashCode() { 85 return mClass; 86 } 87 88 @Override toString()89 public String toString() { 90 return Integer.toHexString(mClass); 91 } 92 93 @Override describeContents()94 public int describeContents() { 95 return 0; 96 } 97 98 public static final @android.annotation.NonNull Parcelable.Creator<BluetoothClass> CREATOR = 99 new Parcelable.Creator<BluetoothClass>() { 100 public BluetoothClass createFromParcel(Parcel in) { 101 return new BluetoothClass(in.readInt()); 102 } 103 104 public BluetoothClass[] newArray(int size) { 105 return new BluetoothClass[size]; 106 } 107 }; 108 109 @Override writeToParcel(Parcel out, int flags)110 public void writeToParcel(Parcel out, int flags) { 111 out.writeInt(mClass); 112 } 113 114 /** 115 * Defines all service class constants. 116 * <p>Each {@link BluetoothClass} encodes zero or more service classes. 117 */ 118 public static final class Service { 119 private static final int BITMASK = 0xFFE000; 120 121 public static final int LIMITED_DISCOVERABILITY = 0x002000; 122 /** Represent devices LE audio service */ 123 public static final int LE_AUDIO = 0x004000; 124 public static final int POSITIONING = 0x010000; 125 public static final int NETWORKING = 0x020000; 126 public static final int RENDER = 0x040000; 127 public static final int CAPTURE = 0x080000; 128 public static final int OBJECT_TRANSFER = 0x100000; 129 public static final int AUDIO = 0x200000; 130 public static final int TELEPHONY = 0x400000; 131 public static final int INFORMATION = 0x800000; 132 } 133 134 /** 135 * Return true if the specified service class is supported by this 136 * {@link BluetoothClass}. 137 * <p>Valid service classes are the public constants in 138 * {@link BluetoothClass.Service}. For example, {@link 139 * BluetoothClass.Service#AUDIO}. 140 * 141 * @param service valid service class 142 * @return true if the service class is supported 143 */ hasService(int service)144 public boolean hasService(int service) { 145 return ((mClass & Service.BITMASK & service) != 0); 146 } 147 148 /** 149 * Defines all device class constants. 150 * <p>Each {@link BluetoothClass} encodes exactly one device class, with 151 * major and minor components. 152 * <p>The constants in {@link 153 * BluetoothClass.Device} represent a combination of major and minor 154 * device components (the complete device class). The constants in {@link 155 * BluetoothClass.Device.Major} represent only major device classes. 156 * <p>See {@link BluetoothClass.Service} for service class constants. 157 */ 158 public static class Device { 159 private static final int BITMASK = 0x1FFC; 160 161 /** 162 * Defines all major device class constants. 163 * <p>See {@link BluetoothClass.Device} for minor classes. 164 */ 165 public static class Major { 166 private static final int BITMASK = 0x1F00; 167 168 public static final int MISC = 0x0000; 169 public static final int COMPUTER = 0x0100; 170 public static final int PHONE = 0x0200; 171 public static final int NETWORKING = 0x0300; 172 public static final int AUDIO_VIDEO = 0x0400; 173 public static final int PERIPHERAL = 0x0500; 174 public static final int IMAGING = 0x0600; 175 public static final int WEARABLE = 0x0700; 176 public static final int TOY = 0x0800; 177 public static final int HEALTH = 0x0900; 178 public static final int UNCATEGORIZED = 0x1F00; 179 } 180 181 // Devices in the COMPUTER major class 182 public static final int COMPUTER_UNCATEGORIZED = 0x0100; 183 public static final int COMPUTER_DESKTOP = 0x0104; 184 public static final int COMPUTER_SERVER = 0x0108; 185 public static final int COMPUTER_LAPTOP = 0x010C; 186 public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110; 187 public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114; 188 public static final int COMPUTER_WEARABLE = 0x0118; 189 190 // Devices in the PHONE major class 191 public static final int PHONE_UNCATEGORIZED = 0x0200; 192 public static final int PHONE_CELLULAR = 0x0204; 193 public static final int PHONE_CORDLESS = 0x0208; 194 public static final int PHONE_SMART = 0x020C; 195 public static final int PHONE_MODEM_OR_GATEWAY = 0x0210; 196 public static final int PHONE_ISDN = 0x0214; 197 198 // Minor classes for the AUDIO_VIDEO major class 199 public static final int AUDIO_VIDEO_UNCATEGORIZED = 0x0400; 200 public static final int AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404; 201 public static final int AUDIO_VIDEO_HANDSFREE = 0x0408; 202 //public static final int AUDIO_VIDEO_RESERVED = 0x040C; 203 public static final int AUDIO_VIDEO_MICROPHONE = 0x0410; 204 public static final int AUDIO_VIDEO_LOUDSPEAKER = 0x0414; 205 public static final int AUDIO_VIDEO_HEADPHONES = 0x0418; 206 public static final int AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C; 207 public static final int AUDIO_VIDEO_CAR_AUDIO = 0x0420; 208 public static final int AUDIO_VIDEO_SET_TOP_BOX = 0x0424; 209 public static final int AUDIO_VIDEO_HIFI_AUDIO = 0x0428; 210 public static final int AUDIO_VIDEO_VCR = 0x042C; 211 public static final int AUDIO_VIDEO_VIDEO_CAMERA = 0x0430; 212 public static final int AUDIO_VIDEO_CAMCORDER = 0x0434; 213 public static final int AUDIO_VIDEO_VIDEO_MONITOR = 0x0438; 214 public static final int AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C; 215 public static final int AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440; 216 //public static final int AUDIO_VIDEO_RESERVED = 0x0444; 217 public static final int AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448; 218 219 // Devices in the WEARABLE major class 220 public static final int WEARABLE_UNCATEGORIZED = 0x0700; 221 public static final int WEARABLE_WRIST_WATCH = 0x0704; 222 public static final int WEARABLE_PAGER = 0x0708; 223 public static final int WEARABLE_JACKET = 0x070C; 224 public static final int WEARABLE_HELMET = 0x0710; 225 public static final int WEARABLE_GLASSES = 0x0714; 226 227 // Devices in the TOY major class 228 public static final int TOY_UNCATEGORIZED = 0x0800; 229 public static final int TOY_ROBOT = 0x0804; 230 public static final int TOY_VEHICLE = 0x0808; 231 public static final int TOY_DOLL_ACTION_FIGURE = 0x080C; 232 public static final int TOY_CONTROLLER = 0x0810; 233 public static final int TOY_GAME = 0x0814; 234 235 // Devices in the HEALTH major class 236 public static final int HEALTH_UNCATEGORIZED = 0x0900; 237 public static final int HEALTH_BLOOD_PRESSURE = 0x0904; 238 public static final int HEALTH_THERMOMETER = 0x0908; 239 public static final int HEALTH_WEIGHING = 0x090C; 240 public static final int HEALTH_GLUCOSE = 0x0910; 241 public static final int HEALTH_PULSE_OXIMETER = 0x0914; 242 public static final int HEALTH_PULSE_RATE = 0x0918; 243 public static final int HEALTH_DATA_DISPLAY = 0x091C; 244 245 // Devices in PERIPHERAL major class 246 public static final int PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500; 247 public static final int PERIPHERAL_KEYBOARD = 0x0540; 248 public static final int PERIPHERAL_POINTING = 0x0580; 249 public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0; 250 } 251 252 /** 253 * Return the major device class component of this {@link BluetoothClass}. 254 * <p>Values returned from this function can be compared with the 255 * public constants in {@link BluetoothClass.Device.Major} to determine 256 * which major class is encoded in this Bluetooth class. 257 * 258 * @return major device class component 259 */ getMajorDeviceClass()260 public int getMajorDeviceClass() { 261 return (mClass & Device.Major.BITMASK); 262 } 263 264 /** 265 * Return the (major and minor) device class component of this 266 * {@link BluetoothClass}. 267 * <p>Values returned from this function can be compared with the 268 * public constants in {@link BluetoothClass.Device} to determine which 269 * device class is encoded in this Bluetooth class. 270 * 271 * @return device class component 272 */ getDeviceClass()273 public int getDeviceClass() { 274 return (mClass & Device.BITMASK); 275 } 276 277 /** 278 * Return the Bluetooth Class of Device (CoD) value including the 279 * {@link BluetoothClass.Service}, {@link BluetoothClass.Device.Major} and 280 * minor device fields. 281 * 282 * <p>This value is an integer representation of Bluetooth CoD as in 283 * Bluetooth specification. 284 * 285 * @see <a href="Bluetooth CoD">https://www.bluetooth.com/specifications/assigned-numbers/baseband</a> 286 * 287 * @hide 288 */ getClassOfDevice()289 public int getClassOfDevice() { 290 return mClass; 291 } 292 293 /** 294 * Return the Bluetooth Class of Device (CoD) value including the 295 * {@link BluetoothClass.Service}, {@link BluetoothClass.Device.Major} and 296 * minor device fields. 297 * 298 * <p>This value is a byte array representation of Bluetooth CoD as in 299 * Bluetooth specification. 300 * 301 * <p>Bluetooth COD information is 3 bytes, but stored as an int. Hence the 302 * MSB is useless and needs to be thrown away. The lower 3 bytes are 303 * converted into a byte array MSB to LSB. Hence, using BIG_ENDIAN. 304 * 305 * @see <a href="Bluetooth CoD">https://www.bluetooth.com/specifications/assigned-numbers/baseband</a> 306 * 307 * @hide 308 */ getClassOfDeviceBytes()309 public byte[] getClassOfDeviceBytes() { 310 byte[] bytes = ByteBuffer.allocate(4) 311 .order(ByteOrder.BIG_ENDIAN) 312 .putInt(mClass) 313 .array(); 314 315 // Discard the top byte 316 return Arrays.copyOfRange(bytes, 1, bytes.length); 317 } 318 319 public static final int PROFILE_HEADSET = 0; 320 321 public static final int PROFILE_A2DP = 1; 322 323 /** @hide */ 324 @SystemApi 325 public static final int PROFILE_OPP = 2; 326 327 public static final int PROFILE_HID = 3; 328 329 /** @hide */ 330 @SystemApi 331 public static final int PROFILE_PANU = 4; 332 333 /** @hide */ 334 @SystemApi 335 public static final int PROFILE_NAP = 5; 336 337 /** @hide */ 338 @SystemApi 339 public static final int PROFILE_A2DP_SINK = 6; 340 341 /** 342 * Check class bits for possible bluetooth profile support. 343 * This is a simple heuristic that tries to guess if a device with the 344 * given class bits might support specified profile. It is not accurate for all 345 * devices. It tries to err on the side of false positives. 346 * 347 * @param profile the profile to be checked 348 * @return whether this device supports specified profile 349 */ doesClassMatch(int profile)350 public boolean doesClassMatch(int profile) { 351 if (profile == PROFILE_A2DP) { 352 if (hasService(Service.RENDER)) { 353 return true; 354 } 355 // By the A2DP spec, sinks must indicate the RENDER service. 356 // However we found some that do not (Chordette). So lets also 357 // match on some other class bits. 358 switch (getDeviceClass()) { 359 case Device.AUDIO_VIDEO_HIFI_AUDIO: 360 case Device.AUDIO_VIDEO_HEADPHONES: 361 case Device.AUDIO_VIDEO_LOUDSPEAKER: 362 case Device.AUDIO_VIDEO_CAR_AUDIO: 363 return true; 364 default: 365 return false; 366 } 367 } else if (profile == PROFILE_A2DP_SINK) { 368 if (hasService(Service.CAPTURE)) { 369 return true; 370 } 371 // By the A2DP spec, srcs must indicate the CAPTURE service. 372 // However if some device that do not, we try to 373 // match on some other class bits. 374 switch (getDeviceClass()) { 375 case Device.AUDIO_VIDEO_HIFI_AUDIO: 376 case Device.AUDIO_VIDEO_SET_TOP_BOX: 377 case Device.AUDIO_VIDEO_VCR: 378 return true; 379 default: 380 return false; 381 } 382 } else if (profile == PROFILE_HEADSET) { 383 // The render service class is required by the spec for HFP, so is a 384 // pretty good signal 385 if (hasService(Service.RENDER)) { 386 return true; 387 } 388 // Just in case they forgot the render service class 389 switch (getDeviceClass()) { 390 case Device.AUDIO_VIDEO_HANDSFREE: 391 case Device.AUDIO_VIDEO_WEARABLE_HEADSET: 392 case Device.AUDIO_VIDEO_CAR_AUDIO: 393 return true; 394 default: 395 return false; 396 } 397 } else if (profile == PROFILE_OPP) { 398 if (hasService(Service.OBJECT_TRANSFER)) { 399 return true; 400 } 401 402 switch (getDeviceClass()) { 403 case Device.COMPUTER_UNCATEGORIZED: 404 case Device.COMPUTER_DESKTOP: 405 case Device.COMPUTER_SERVER: 406 case Device.COMPUTER_LAPTOP: 407 case Device.COMPUTER_HANDHELD_PC_PDA: 408 case Device.COMPUTER_PALM_SIZE_PC_PDA: 409 case Device.COMPUTER_WEARABLE: 410 case Device.PHONE_UNCATEGORIZED: 411 case Device.PHONE_CELLULAR: 412 case Device.PHONE_CORDLESS: 413 case Device.PHONE_SMART: 414 case Device.PHONE_MODEM_OR_GATEWAY: 415 case Device.PHONE_ISDN: 416 return true; 417 default: 418 return false; 419 } 420 } else if (profile == PROFILE_HID) { 421 return getMajorDeviceClass() == Device.Major.PERIPHERAL; 422 } else if (profile == PROFILE_PANU || profile == PROFILE_NAP) { 423 // No good way to distinguish between the two, based on class bits. 424 if (hasService(Service.NETWORKING)) { 425 return true; 426 } 427 return getMajorDeviceClass() == Device.Major.NETWORKING; 428 } else { 429 return false; 430 } 431 } 432 } 433