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