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