• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010-2016 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.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.RequiresNoPermission;
22 import android.annotation.SuppressLint;
23 import android.annotation.SystemApi;
24 import android.compat.annotation.UnsupportedAppUsage;
25 import android.os.Build;
26 import android.os.IBinder;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.List;
31 
32 /**
33  * Public APIs for the Bluetooth Profiles.
34  *
35  * <p>Clients should call {@link BluetoothAdapter#getProfileProxy}, to get the Profile Proxy. Each
36  * public profile implements this interface.
37  */
38 public interface BluetoothProfile {
39 
40     /**
41      * Extra for the connection state intents of the individual profiles.
42      *
43      * <p>This extra represents the current connection state of the profile of the Bluetooth device.
44      */
45     @SuppressLint("ActionValue")
46     String EXTRA_STATE = "android.bluetooth.profile.extra.STATE";
47 
48     /**
49      * Extra for the connection state intents of the individual profiles.
50      *
51      * <p>This extra represents the previous connection state of the profile of the Bluetooth
52      * device.
53      */
54     @SuppressLint("ActionValue")
55     String EXTRA_PREVIOUS_STATE = "android.bluetooth.profile.extra.PREVIOUS_STATE";
56 
57     /** The profile is in disconnected state */
58     int STATE_DISCONNECTED = 0;
59 
60     /** The profile is in connecting state */
61     int STATE_CONNECTING = 1;
62 
63     /** The profile is in connected state */
64     int STATE_CONNECTED = 2;
65 
66     /** The profile is in disconnecting state */
67     int STATE_DISCONNECTING = 3;
68 
69     /** @hide */
70     @IntDef({
71         STATE_DISCONNECTED,
72         STATE_CONNECTING,
73         STATE_CONNECTED,
74         STATE_DISCONNECTING,
75     })
76     @Retention(RetentionPolicy.SOURCE)
77     public @interface BtProfileState {}
78 
79     /** Headset and Handsfree profile */
80     int HEADSET = 1;
81 
82     /** A2DP profile. */
83     int A2DP = 2;
84 
85     /**
86      * Health Profile
87      *
88      * @deprecated Health Device Profile (HDP) and MCAP protocol are no longer used. New apps should
89      *     use Bluetooth Low Energy based solutions such as {@link BluetoothGatt}, {@link
90      *     BluetoothAdapter#listenUsingL2capChannel()}, or {@link
91      *     BluetoothDevice#createL2capChannel(int)}
92      */
93     @Deprecated int HEALTH = 3;
94 
95     /**
96      * HID Host
97      *
98      * @hide
99      */
100     @SystemApi int HID_HOST = 4;
101 
102     /**
103      * PAN Profile
104      *
105      * @hide
106      */
107     @SystemApi int PAN = 5;
108 
109     /**
110      * PBAP
111      *
112      * @hide
113      */
114     @SystemApi int PBAP = 6;
115 
116     /** GATT */
117     int GATT = 7;
118 
119     /** GATT_SERVER */
120     int GATT_SERVER = 8;
121 
122     /**
123      * MAP Profile
124      *
125      * @hide
126      */
127     @SystemApi int MAP = 9;
128 
129     /** SAP Profile */
130     int SAP = 10;
131 
132     /**
133      * A2DP Sink Profile
134      *
135      * @hide
136      */
137     @SystemApi int A2DP_SINK = 11;
138 
139     /**
140      * AVRCP Controller Profile
141      *
142      * @hide
143      */
144     @SystemApi int AVRCP_CONTROLLER = 12;
145 
146     /**
147      * AVRCP Target Profile
148      *
149      * @hide
150      */
151     int AVRCP = 13;
152 
153     /**
154      * Headset Client - HFP HF Role
155      *
156      * @hide
157      */
158     @SystemApi int HEADSET_CLIENT = 16;
159 
160     /**
161      * PBAP Client
162      *
163      * @hide
164      */
165     @SystemApi int PBAP_CLIENT = 17;
166 
167     /**
168      * MAP Messaging Client Equipment (MCE)
169      *
170      * @hide
171      */
172     @SystemApi int MAP_CLIENT = 18;
173 
174     /** HID Device */
175     int HID_DEVICE = 19;
176 
177     /**
178      * Object Push Profile (OPP)
179      *
180      * @hide
181      */
182     @SystemApi int OPP = 20;
183 
184     /** Hearing Aid Device */
185     int HEARING_AID = 21;
186 
187     /** LE Audio Device */
188     int LE_AUDIO = 22;
189 
190     /**
191      * Volume Control profile
192      *
193      * @hide
194      */
195     @SystemApi int VOLUME_CONTROL = 23;
196 
197     /**
198      * @hide Media Control Profile server
199      */
200     int MCP_SERVER = 24;
201 
202     /** Coordinated Set Identification Profile set coordinator */
203     int CSIP_SET_COORDINATOR = 25;
204 
205     /**
206      * LE Audio Broadcast Source
207      *
208      * @hide
209      */
210     @SystemApi int LE_AUDIO_BROADCAST = 26;
211 
212     /**
213      * @hide Telephone Bearer Service from Call Control Profile
214      */
215     int LE_CALL_CONTROL = 27;
216 
217     /*
218      * Hearing Access Profile Client
219      *
220      */
221     int HAP_CLIENT = 28;
222 
223     /**
224      * LE Audio Broadcast Assistant
225      *
226      * @hide
227      */
228     @SystemApi int LE_AUDIO_BROADCAST_ASSISTANT = 29;
229 
230     /**
231      * Battery Service
232      *
233      * @hide
234      */
235     int BATTERY = 30;
236 
237     /**
238      * Gaming Audio Profile
239      *
240      * @hide
241      */
242     int GMAP = 31;
243 
244     /**
245      * Max profile ID. This value should be updated whenever a new profile is added to match the
246      * largest value assigned to a profile.
247      *
248      * @hide
249      */
250     int MAX_PROFILE_ID = 31;
251 
252     /**
253      * Default priority for devices that we try to auto-connect to and allow incoming connections
254      * for the profile
255      *
256      * @hide
257      */
258     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
259     int PRIORITY_AUTO_CONNECT = 1000;
260 
261     /**
262      * Default priority for devices that allow incoming and outgoing connections for the profile
263      *
264      * @hide
265      * @deprecated Replaced with {@link #CONNECTION_POLICY_ALLOWED}
266      */
267     @Deprecated @SystemApi int PRIORITY_ON = 100;
268 
269     /**
270      * Default priority for devices that does not allow incoming connections and outgoing
271      * connections for the profile.
272      *
273      * @hide
274      * @deprecated Replaced with {@link #CONNECTION_POLICY_FORBIDDEN}
275      */
276     @Deprecated @SystemApi int PRIORITY_OFF = 0;
277 
278     /**
279      * Default priority when not set or when the device is unpaired
280      *
281      * @hide
282      */
283     @UnsupportedAppUsage int PRIORITY_UNDEFINED = -1;
284 
285     /** @hide */
286     @IntDef(
287             prefix = "CONNECTION_POLICY_",
288             value = {
289                 CONNECTION_POLICY_ALLOWED,
290                 CONNECTION_POLICY_FORBIDDEN,
291                 CONNECTION_POLICY_UNKNOWN
292             })
293     @Retention(RetentionPolicy.SOURCE)
294     public @interface ConnectionPolicy {}
295 
296     /**
297      * Default connection policy for devices that allow incoming and outgoing connections for the
298      * profile
299      *
300      * @hide
301      */
302     @SystemApi int CONNECTION_POLICY_ALLOWED = 100;
303 
304     /**
305      * Default connection policy for devices that do not allow incoming or outgoing connections for
306      * the profile.
307      *
308      * @hide
309      */
310     @SystemApi int CONNECTION_POLICY_FORBIDDEN = 0;
311 
312     /**
313      * Default connection policy when not set or when the device is unpaired
314      *
315      * @hide
316      */
317     @SystemApi int CONNECTION_POLICY_UNKNOWN = -1;
318 
319     /**
320      * Get connected devices for this specific profile.
321      *
322      * <p>Return the set of devices which are in state {@link #STATE_CONNECTED}
323      *
324      * @return List of devices. The list will be empty on error.
325      */
getConnectedDevices()326     List<BluetoothDevice> getConnectedDevices();
327 
328     /**
329      * Get a list of devices that match any of the given connection states.
330      *
331      * <p>If none of the devices match any of the given states, an empty list will be returned.
332      *
333      * @param states Array of states. States can be one of {@link #STATE_CONNECTED}, {@link
334      *     #STATE_CONNECTING}, {@link #STATE_DISCONNECTED}, {@link #STATE_DISCONNECTING},
335      * @return List of devices. The list will be empty on error.
336      */
getDevicesMatchingConnectionStates(int[] states)337     List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states);
338 
339     /**
340      * Get the current connection state of the profile
341      *
342      * @param device Remote bluetooth device.
343      * @return State of the profile connection. One of {@link #STATE_CONNECTED}, {@link
344      *     #STATE_CONNECTING}, {@link #STATE_DISCONNECTED}, {@link #STATE_DISCONNECTING}
345      */
346     @BtProfileState
getConnectionState(BluetoothDevice device)347     int getConnectionState(BluetoothDevice device);
348 
349     /**
350      * Called by the BluetoothAdapter when the Bluetooth service is connected with a Binder instance
351      * corresponding to the service associated with the profile
352      *
353      * @hide
354      */
onServiceConnected(IBinder service)355     void onServiceConnected(IBinder service);
356 
357     /**
358      * Called by the BluetoothAdapter when the Bluetooth service connection has been lost
359      *
360      * @hide
361      */
onServiceDisconnected()362     void onServiceDisconnected();
363 
364     /**
365      * Get the BluetoothAdapter that created this proxy
366      *
367      * @hide
368      */
getAdapter()369     BluetoothAdapter getAdapter();
370 
371     /**
372      * An interface for notifying BluetoothProfile IPC clients when they have been connected or
373      * disconnected to the service.
374      */
375     public interface ServiceListener {
376         /**
377          * Called to notify the client when the proxy object has been connected to the service.
378          *
379          * @param profile - One of {@link #HEADSET} or {@link #A2DP}
380          * @param proxy - One of {@link BluetoothHeadset} or {@link BluetoothA2dp}
381          */
382         @RequiresNoPermission
onServiceConnected(int profile, BluetoothProfile proxy)383         void onServiceConnected(int profile, BluetoothProfile proxy);
384 
385         /**
386          * Called to notify the client that this proxy object has been disconnected from the
387          * service.
388          *
389          * @param profile - One of {@link #HEADSET} or {@link #A2DP}
390          */
391         @RequiresNoPermission
onServiceDisconnected(int profile)392         void onServiceDisconnected(int profile);
393     }
394 
395     /**
396      * Convert an integer value of connection state into human readable string
397      *
398      * @param connectionState - One of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
399      *     {@link #STATE_CONNECTED}, or {@link #STATE_DISCONNECTED}
400      * @return a string representation of the connection state, STATE_UNKNOWN if the state is not
401      *     defined
402      * @hide
403      */
404     @SystemApi
405     @NonNull
406     @RequiresNoPermission
getConnectionStateName(int connectionState)407     static String getConnectionStateName(int connectionState) {
408         switch (connectionState) {
409             case STATE_DISCONNECTED:
410                 return "STATE_DISCONNECTED";
411             case STATE_CONNECTING:
412                 return "STATE_CONNECTING";
413             case STATE_CONNECTED:
414                 return "STATE_CONNECTED";
415             case STATE_DISCONNECTING:
416                 return "STATE_DISCONNECTING";
417             default:
418                 return "STATE_UNKNOWN";
419         }
420     }
421 
422     /**
423      * Convert an integer value of profile ID into human readable string
424      *
425      * @param profile profile ID
426      * @return profile name as String, UNKNOWN_PROFILE if the profile ID is not defined.
427      * @hide
428      */
429     @SystemApi
430     @NonNull
431     @RequiresNoPermission
getProfileName(int profile)432     static String getProfileName(int profile) {
433         switch (profile) {
434             case HEADSET:
435                 return "HEADSET";
436             case A2DP:
437                 return "A2DP";
438             case HID_HOST:
439                 return "HID_HOST";
440             case PAN:
441                 return "PAN";
442             case PBAP:
443                 return "PBAP";
444             case GATT:
445                 return "GATT";
446             case GATT_SERVER:
447                 return "GATT_SERVER";
448             case MAP:
449                 return "MAP";
450             case SAP:
451                 return "SAP";
452             case A2DP_SINK:
453                 return "A2DP_SINK";
454             case AVRCP_CONTROLLER:
455                 return "AVRCP_CONTROLLER";
456             case AVRCP:
457                 return "AVRCP";
458             case HEADSET_CLIENT:
459                 return "HEADSET_CLIENT";
460             case PBAP_CLIENT:
461                 return "PBAP_CLIENT";
462             case MAP_CLIENT:
463                 return "MAP_CLIENT";
464             case HID_DEVICE:
465                 return "HID_DEVICE";
466             case OPP:
467                 return "OPP";
468             case HEARING_AID:
469                 return "HEARING_AID";
470             case LE_AUDIO:
471                 return "LE_AUDIO";
472             case VOLUME_CONTROL:
473                 return "VOLUME_CONTROL";
474             case MCP_SERVER:
475                 return "MCP_SERVER";
476             case CSIP_SET_COORDINATOR:
477                 return "CSIP_SET_COORDINATOR";
478             case LE_AUDIO_BROADCAST:
479                 return "LE_AUDIO_BROADCAST";
480             case LE_CALL_CONTROL:
481                 return "LE_CALL_CONTROL";
482             case HAP_CLIENT:
483                 return "HAP_CLIENT";
484             case LE_AUDIO_BROADCAST_ASSISTANT:
485                 return "LE_AUDIO_BROADCAST_ASSISTANT";
486             case BATTERY:
487                 return "BATTERY";
488             default:
489                 return "UNKNOWN_PROFILE (" + profile + ")";
490         }
491     }
492 }
493