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