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.os.Parcel; 20 import android.os.Parcelable; 21 22 /** 23 * Represents a Bluetooth class, which describes general characteristics 24 * and capabilities of a device. For example, a Bluetooth class will 25 * specify the general device type such as a phone, a computer, or 26 * headset, and whether it's capable of services such as audio or telephony. 27 * 28 * <p>The Bluetooth class is useful as a hint to roughly describe a device (for example to 29 * show an icon in the UI), but does not reliably describe which Bluetooth 30 * profiles or services are actually supported by a device. 31 * 32 * <p>Every Bluetooth class is composed of zero or more service classes, and 33 * exactly one device class. The device class is further broken down into major 34 * and minor device class components. 35 * 36 * <p>{@link BluetoothClass} is useful as a hint to roughly describe a device 37 * (for example to show an icon in the UI), but does not reliably describe which 38 * Bluetooth profiles or services are actually supported by a device. Accurate 39 * service discovery is done through SDP requests, which are automatically 40 * performed when creating an RFCOMM socket with {@link 41 * BluetoothDevice#createRfcommSocketToServiceRecord(UUID)} and {@link 42 * BluetoothAdapter#listenUsingRfcommWithServiceRecord(String,UUID)}</p> 43 * 44 * <p>Use {@link BluetoothDevice#getBluetoothClass} to retrieve the class for 45 * a remote device. 46 * 47 * <!-- 48 * The Bluetooth class is a 32 bit field. The format of these bits is defined at 49 * http://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm 50 * (login required). This class contains that 32 bit field, and provides 51 * constants and methods to determine which Service Class(es) and Device Class 52 * are encoded in that field. 53 * --> 54 */ 55 public final class BluetoothClass implements Parcelable { 56 /** 57 * Legacy error value. Applications should use null instead. 58 * @hide 59 */ 60 public static final int ERROR = 0xFF000000; 61 62 private final int mClass; 63 64 /** @hide */ BluetoothClass(int classInt)65 public BluetoothClass(int classInt) { 66 mClass = classInt; 67 } 68 69 @Override equals(Object o)70 public boolean equals(Object o) { 71 if (o instanceof BluetoothClass) { 72 return mClass == ((BluetoothClass)o).mClass; 73 } 74 return false; 75 } 76 77 @Override hashCode()78 public int hashCode() { 79 return mClass; 80 } 81 82 @Override toString()83 public String toString() { 84 return Integer.toHexString(mClass); 85 } 86 describeContents()87 public int describeContents() { 88 return 0; 89 } 90 91 public static final Parcelable.Creator<BluetoothClass> CREATOR = 92 new Parcelable.Creator<BluetoothClass>() { 93 public BluetoothClass createFromParcel(Parcel in) { 94 return new BluetoothClass(in.readInt()); 95 } 96 public BluetoothClass[] newArray(int size) { 97 return new BluetoothClass[size]; 98 } 99 }; 100 writeToParcel(Parcel out, int flags)101 public void writeToParcel(Parcel out, int flags) { 102 out.writeInt(mClass); 103 } 104 105 /** 106 * Defines all service class constants. 107 * <p>Each {@link BluetoothClass} encodes zero or more service classes. 108 */ 109 public static final class Service { 110 private static final int BITMASK = 0xFFE000; 111 112 public static final int LIMITED_DISCOVERABILITY = 0x002000; 113 public static final int POSITIONING = 0x010000; 114 public static final int NETWORKING = 0x020000; 115 public static final int RENDER = 0x040000; 116 public static final int CAPTURE = 0x080000; 117 public static final int OBJECT_TRANSFER = 0x100000; 118 public static final int AUDIO = 0x200000; 119 public static final int TELEPHONY = 0x400000; 120 public static final int INFORMATION = 0x800000; 121 } 122 123 /** 124 * Return true if the specified service class is supported by this 125 * {@link BluetoothClass}. 126 * <p>Valid service classes are the public constants in 127 * {@link BluetoothClass.Service}. For example, {@link 128 * BluetoothClass.Service#AUDIO}. 129 * 130 * @param service valid service class 131 * @return true if the service class is supported 132 */ hasService(int service)133 public boolean hasService(int service) { 134 return ((mClass & Service.BITMASK & service) != 0); 135 } 136 137 /** 138 * Defines all device class constants. 139 * <p>Each {@link BluetoothClass} encodes exactly one device class, with 140 * major and minor components. 141 * <p>The constants in {@link 142 * BluetoothClass.Device} represent a combination of major and minor 143 * device components (the complete device class). The constants in {@link 144 * BluetoothClass.Device.Major} represent only major device classes. 145 * <p>See {@link BluetoothClass.Service} for service class constants. 146 */ 147 public static class Device { 148 private static final int BITMASK = 0x1FFC; 149 150 /** 151 * Defines all major device class constants. 152 * <p>See {@link BluetoothClass.Device} for minor classes. 153 */ 154 public static class Major { 155 private static final int BITMASK = 0x1F00; 156 157 public static final int MISC = 0x0000; 158 public static final int COMPUTER = 0x0100; 159 public static final int PHONE = 0x0200; 160 public static final int NETWORKING = 0x0300; 161 public static final int AUDIO_VIDEO = 0x0400; 162 public static final int PERIPHERAL = 0x0500; 163 public static final int IMAGING = 0x0600; 164 public static final int WEARABLE = 0x0700; 165 public static final int TOY = 0x0800; 166 public static final int HEALTH = 0x0900; 167 public static final int UNCATEGORIZED = 0x1F00; 168 } 169 170 // Devices in the COMPUTER major class 171 public static final int COMPUTER_UNCATEGORIZED = 0x0100; 172 public static final int COMPUTER_DESKTOP = 0x0104; 173 public static final int COMPUTER_SERVER = 0x0108; 174 public static final int COMPUTER_LAPTOP = 0x010C; 175 public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110; 176 public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114; 177 public static final int COMPUTER_WEARABLE = 0x0118; 178 179 // Devices in the PHONE major class 180 public static final int PHONE_UNCATEGORIZED = 0x0200; 181 public static final int PHONE_CELLULAR = 0x0204; 182 public static final int PHONE_CORDLESS = 0x0208; 183 public static final int PHONE_SMART = 0x020C; 184 public static final int PHONE_MODEM_OR_GATEWAY = 0x0210; 185 public static final int PHONE_ISDN = 0x0214; 186 187 // Minor classes for the AUDIO_VIDEO major class 188 public static final int AUDIO_VIDEO_UNCATEGORIZED = 0x0400; 189 public static final int AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404; 190 public static final int AUDIO_VIDEO_HANDSFREE = 0x0408; 191 //public static final int AUDIO_VIDEO_RESERVED = 0x040C; 192 public static final int AUDIO_VIDEO_MICROPHONE = 0x0410; 193 public static final int AUDIO_VIDEO_LOUDSPEAKER = 0x0414; 194 public static final int AUDIO_VIDEO_HEADPHONES = 0x0418; 195 public static final int AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C; 196 public static final int AUDIO_VIDEO_CAR_AUDIO = 0x0420; 197 public static final int AUDIO_VIDEO_SET_TOP_BOX = 0x0424; 198 public static final int AUDIO_VIDEO_HIFI_AUDIO = 0x0428; 199 public static final int AUDIO_VIDEO_VCR = 0x042C; 200 public static final int AUDIO_VIDEO_VIDEO_CAMERA = 0x0430; 201 public static final int AUDIO_VIDEO_CAMCORDER = 0x0434; 202 public static final int AUDIO_VIDEO_VIDEO_MONITOR = 0x0438; 203 public static final int AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C; 204 public static final int AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440; 205 //public static final int AUDIO_VIDEO_RESERVED = 0x0444; 206 public static final int AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448; 207 208 // Devices in the WEARABLE major class 209 public static final int WEARABLE_UNCATEGORIZED = 0x0700; 210 public static final int WEARABLE_WRIST_WATCH = 0x0704; 211 public static final int WEARABLE_PAGER = 0x0708; 212 public static final int WEARABLE_JACKET = 0x070C; 213 public static final int WEARABLE_HELMET = 0x0710; 214 public static final int WEARABLE_GLASSES = 0x0714; 215 216 // Devices in the TOY major class 217 public static final int TOY_UNCATEGORIZED = 0x0800; 218 public static final int TOY_ROBOT = 0x0804; 219 public static final int TOY_VEHICLE = 0x0808; 220 public static final int TOY_DOLL_ACTION_FIGURE = 0x080C; 221 public static final int TOY_CONTROLLER = 0x0810; 222 public static final int TOY_GAME = 0x0814; 223 224 // Devices in the HEALTH major class 225 public static final int HEALTH_UNCATEGORIZED = 0x0900; 226 public static final int HEALTH_BLOOD_PRESSURE = 0x0904; 227 public static final int HEALTH_THERMOMETER = 0x0908; 228 public static final int HEALTH_WEIGHING = 0x090C; 229 public static final int HEALTH_GLUCOSE = 0x0910; 230 public static final int HEALTH_PULSE_OXIMETER = 0x0914; 231 public static final int HEALTH_PULSE_RATE = 0x0918; 232 public static final int HEALTH_DATA_DISPLAY = 0x091C; 233 } 234 235 /** 236 * Return the major device class component of this {@link BluetoothClass}. 237 * <p>Values returned from this function can be compared with the 238 * public constants in {@link BluetoothClass.Device.Major} to determine 239 * which major class is encoded in this Bluetooth class. 240 * 241 * @return major device class component 242 */ getMajorDeviceClass()243 public int getMajorDeviceClass() { 244 return (mClass & Device.Major.BITMASK); 245 } 246 247 /** 248 * Return the (major and minor) device class component of this 249 * {@link BluetoothClass}. 250 * <p>Values returned from this function can be compared with the 251 * public constants in {@link BluetoothClass.Device} to determine which 252 * device class is encoded in this Bluetooth class. 253 * 254 * @return device class component 255 */ getDeviceClass()256 public int getDeviceClass() { 257 return (mClass & Device.BITMASK); 258 } 259 260 /** @hide */ 261 public static final int PROFILE_HEADSET = 0; 262 /** @hide */ 263 public static final int PROFILE_A2DP = 1; 264 /** @hide */ 265 public static final int PROFILE_OPP = 2; 266 267 /** 268 * Check class bits for possible bluetooth profile support. 269 * This is a simple heuristic that tries to guess if a device with the 270 * given class bits might support specified profile. It is not accurate for all 271 * devices. It tries to err on the side of false positives. 272 * @param profile The profile to be checked 273 * @return True if this device might support specified profile. 274 * @hide 275 */ doesClassMatch(int profile)276 public boolean doesClassMatch(int profile) { 277 if (profile == PROFILE_A2DP) { 278 if (hasService(Service.RENDER)) { 279 return true; 280 } 281 // By the A2DP spec, sinks must indicate the RENDER service. 282 // However we found some that do not (Chordette). So lets also 283 // match on some other class bits. 284 switch (getDeviceClass()) { 285 case Device.AUDIO_VIDEO_HIFI_AUDIO: 286 case Device.AUDIO_VIDEO_HEADPHONES: 287 case Device.AUDIO_VIDEO_LOUDSPEAKER: 288 case Device.AUDIO_VIDEO_CAR_AUDIO: 289 return true; 290 default: 291 return false; 292 } 293 } else if (profile == PROFILE_HEADSET) { 294 // The render service class is required by the spec for HFP, so is a 295 // pretty good signal 296 if (hasService(Service.RENDER)) { 297 return true; 298 } 299 // Just in case they forgot the render service class 300 switch (getDeviceClass()) { 301 case Device.AUDIO_VIDEO_HANDSFREE: 302 case Device.AUDIO_VIDEO_WEARABLE_HEADSET: 303 case Device.AUDIO_VIDEO_CAR_AUDIO: 304 return true; 305 default: 306 return false; 307 } 308 } else if (profile == PROFILE_OPP) { 309 if (hasService(Service.OBJECT_TRANSFER)) { 310 return true; 311 } 312 313 switch (getDeviceClass()) { 314 case Device.COMPUTER_UNCATEGORIZED: 315 case Device.COMPUTER_DESKTOP: 316 case Device.COMPUTER_SERVER: 317 case Device.COMPUTER_LAPTOP: 318 case Device.COMPUTER_HANDHELD_PC_PDA: 319 case Device.COMPUTER_PALM_SIZE_PC_PDA: 320 case Device.COMPUTER_WEARABLE: 321 case Device.PHONE_UNCATEGORIZED: 322 case Device.PHONE_CELLULAR: 323 case Device.PHONE_CORDLESS: 324 case Device.PHONE_SMART: 325 case Device.PHONE_MODEM_OR_GATEWAY: 326 case Device.PHONE_ISDN: 327 return true; 328 default: 329 return false; 330 } 331 } else { 332 return false; 333 } 334 } 335 } 336