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 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 @SystemApi 105 int HID_HOST = 4; 106 107 /** 108 * PAN Profile 109 * 110 * @hide 111 */ 112 @SystemApi 113 int PAN = 5; 114 115 /** 116 * PBAP 117 * 118 * @hide 119 */ 120 @SystemApi 121 int PBAP = 6; 122 123 /** 124 * GATT 125 */ 126 int GATT = 7; 127 128 /** 129 * GATT_SERVER 130 */ 131 int GATT_SERVER = 8; 132 133 /** 134 * MAP Profile 135 * 136 * @hide 137 */ 138 @SystemApi 139 int MAP = 9; 140 141 /* 142 * SAP Profile 143 * @hide 144 */ 145 int SAP = 10; 146 147 /** 148 * A2DP Sink Profile 149 * 150 * @hide 151 */ 152 @SystemApi 153 int A2DP_SINK = 11; 154 155 /** 156 * AVRCP Controller Profile 157 * 158 * @hide 159 */ 160 @SystemApi 161 int AVRCP_CONTROLLER = 12; 162 163 /** 164 * AVRCP Target Profile 165 * 166 * @hide 167 */ 168 int AVRCP = 13; 169 170 /** 171 * Headset Client - HFP HF Role 172 * 173 * @hide 174 */ 175 @SystemApi 176 int HEADSET_CLIENT = 16; 177 178 /** 179 * PBAP Client 180 * 181 * @hide 182 */ 183 @SystemApi 184 int PBAP_CLIENT = 17; 185 186 /** 187 * MAP Messaging Client Equipment (MCE) 188 * 189 * @hide 190 */ 191 @SystemApi 192 int MAP_CLIENT = 18; 193 194 /** 195 * HID Device 196 */ 197 int HID_DEVICE = 19; 198 199 /** 200 * Object Push Profile (OPP) 201 * 202 * @hide 203 */ 204 @SystemApi 205 int OPP = 20; 206 207 /** 208 * Hearing Aid Device 209 * 210 */ 211 int HEARING_AID = 21; 212 213 /** 214 * LE Audio Device 215 * 216 */ 217 int LE_AUDIO = 22; 218 219 /** 220 * Volume Control profile 221 * 222 * @hide 223 */ 224 @SystemApi 225 int VOLUME_CONTROL = 23; 226 227 /** 228 * @hide 229 * Media Control Profile server 230 * 231 */ 232 int MCP_SERVER = 24; 233 234 /** 235 * Coordinated Set Identification Profile set coordinator 236 * 237 */ 238 int CSIP_SET_COORDINATOR = 25; 239 240 /** 241 * LE Audio Broadcast Source 242 * 243 * @hide 244 */ 245 @SystemApi 246 int LE_AUDIO_BROADCAST = 26; 247 248 /** 249 * @hide 250 * Telephone Bearer Service from Call Control Profile 251 * 252 */ 253 int LE_CALL_CONTROL = 27; 254 255 /* 256 * Hearing Access Profile Client 257 * 258 */ 259 int HAP_CLIENT = 28; 260 261 /** 262 * LE Audio Broadcast Assistant 263 * 264 * @hide 265 */ 266 @SystemApi 267 int LE_AUDIO_BROADCAST_ASSISTANT = 29; 268 269 /** 270 * Battery Service 271 * 272 * @hide 273 */ 274 int BATTERY = 30; 275 276 /** 277 * Max profile ID. This value should be updated whenever a new profile is added to match 278 * the largest value assigned to a profile. 279 * 280 * @hide 281 */ 282 int MAX_PROFILE_ID = 30; 283 284 /** 285 * Default priority for devices that we try to auto-connect to and 286 * and allow incoming connections for the profile 287 * 288 * @hide 289 **/ 290 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 291 int PRIORITY_AUTO_CONNECT = 1000; 292 293 /** 294 * Default priority for devices that allow incoming 295 * and outgoing connections for the profile 296 * 297 * @hide 298 * @deprecated Replaced with {@link #CONNECTION_POLICY_ALLOWED} 299 **/ 300 @Deprecated 301 @SystemApi 302 int PRIORITY_ON = 100; 303 304 /** 305 * Default priority for devices that does not allow incoming 306 * connections and outgoing connections for the profile. 307 * 308 * @hide 309 * @deprecated Replaced with {@link #CONNECTION_POLICY_FORBIDDEN} 310 **/ 311 @Deprecated 312 @SystemApi 313 int PRIORITY_OFF = 0; 314 315 /** 316 * Default priority when not set or when the device is unpaired 317 * 318 * @hide 319 */ 320 @UnsupportedAppUsage 321 int PRIORITY_UNDEFINED = -1; 322 323 /** @hide */ 324 @IntDef(prefix = "CONNECTION_POLICY_", value = {CONNECTION_POLICY_ALLOWED, 325 CONNECTION_POLICY_FORBIDDEN, CONNECTION_POLICY_UNKNOWN}) 326 @Retention(RetentionPolicy.SOURCE) 327 public @interface ConnectionPolicy{} 328 329 /** 330 * Default connection policy for devices that allow incoming and outgoing connections 331 * for the profile 332 * 333 * @hide 334 **/ 335 @SystemApi 336 int CONNECTION_POLICY_ALLOWED = 100; 337 338 /** 339 * Default connection policy for devices that do not allow incoming or outgoing connections 340 * for the profile. 341 * 342 * @hide 343 **/ 344 @SystemApi 345 int CONNECTION_POLICY_FORBIDDEN = 0; 346 347 /** 348 * Default connection policy when not set or when the device is unpaired 349 * 350 * @hide 351 */ 352 @SystemApi 353 int CONNECTION_POLICY_UNKNOWN = -1; 354 355 /** 356 * Get connected devices for this specific profile. 357 * 358 * <p> Return the set of devices which are in state {@link #STATE_CONNECTED} 359 * 360 * @return List of devices. The list will be empty on error. 361 */ getConnectedDevices()362 public List<BluetoothDevice> getConnectedDevices(); 363 364 /** 365 * Get a list of devices that match any of the given connection 366 * states. 367 * 368 * <p> If none of the devices match any of the given states, 369 * an empty list will be returned. 370 * 371 * @param states Array of states. States can be one of {@link #STATE_CONNECTED}, {@link 372 * #STATE_CONNECTING}, {@link #STATE_DISCONNECTED}, {@link #STATE_DISCONNECTING}, 373 * @return List of devices. The list will be empty on error. 374 */ getDevicesMatchingConnectionStates(int[] states)375 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states); 376 377 /** 378 * Get the current connection state of the profile 379 * 380 * @param device Remote bluetooth device. 381 * @return State of the profile connection. One of {@link #STATE_CONNECTED}, {@link 382 * #STATE_CONNECTING}, {@link #STATE_DISCONNECTED}, {@link #STATE_DISCONNECTING} 383 */ getConnectionState(BluetoothDevice device)384 @BtProfileState int getConnectionState(BluetoothDevice device); 385 386 /** 387 * An interface for notifying BluetoothProfile IPC clients when they have 388 * been connected or disconnected to the service. 389 */ 390 public interface ServiceListener { 391 /** 392 * Called to notify the client when the proxy object has been 393 * connected to the service. 394 * 395 * @param profile - One of {@link #HEADSET} or {@link #A2DP} 396 * @param proxy - One of {@link BluetoothHeadset} or {@link BluetoothA2dp} 397 */ 398 @RequiresNoPermission onServiceConnected(int profile, BluetoothProfile proxy)399 public void onServiceConnected(int profile, BluetoothProfile proxy); 400 401 /** 402 * Called to notify the client that this proxy object has been 403 * disconnected from the service. 404 * 405 * @param profile - One of {@link #HEADSET} or {@link #A2DP} 406 */ 407 @RequiresNoPermission onServiceDisconnected(int profile)408 public void onServiceDisconnected(int profile); 409 } 410 411 /** 412 * Convert an integer value of connection state into human readable string 413 * 414 * @param connectionState - One of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, 415 * {@link #STATE_CONNECTED}, or {@link #STATE_DISCONNECTED} 416 * @return a string representation of the connection state, STATE_UNKNOWN if the state 417 * is not defined 418 * @hide 419 */ 420 @SystemApi 421 @NonNull 422 @RequiresNoPermission getConnectionStateName(int connectionState)423 static String getConnectionStateName(int connectionState) { 424 switch (connectionState) { 425 case STATE_DISCONNECTED: 426 return "STATE_DISCONNECTED"; 427 case STATE_CONNECTING: 428 return "STATE_CONNECTING"; 429 case STATE_CONNECTED: 430 return "STATE_CONNECTED"; 431 case STATE_DISCONNECTING: 432 return "STATE_DISCONNECTING"; 433 default: 434 return "STATE_UNKNOWN"; 435 } 436 } 437 438 /** 439 * Convert an integer value of profile ID into human readable string 440 * 441 * @param profile profile ID 442 * @return profile name as String, UNKOWN_PROFILE if the profile ID is not defined. 443 * @hide 444 */ 445 @SystemApi 446 @NonNull 447 @RequiresNoPermission getProfileName(int profile)448 static String getProfileName(int profile) { 449 switch(profile) { 450 case HEADSET: 451 return "HEADSET"; 452 case A2DP: 453 return "A2DP"; 454 case HID_HOST: 455 return "HID_HOST"; 456 case PAN: 457 return "PAN"; 458 case PBAP: 459 return "PBAP"; 460 case GATT: 461 return "GATT"; 462 case GATT_SERVER: 463 return "GATT_SERVER"; 464 case MAP: 465 return "MAP"; 466 case SAP: 467 return "SAP"; 468 case A2DP_SINK: 469 return "A2DP_SINK"; 470 case AVRCP_CONTROLLER: 471 return "AVRCP_CONTROLLER"; 472 case AVRCP: 473 return "AVRCP"; 474 case HEADSET_CLIENT: 475 return "HEADSET_CLIENT"; 476 case PBAP_CLIENT: 477 return "PBAP_CLIENT"; 478 case MAP_CLIENT: 479 return "MAP_CLIENT"; 480 case HID_DEVICE: 481 return "HID_DEVICE"; 482 case OPP: 483 return "OPP"; 484 case HEARING_AID: 485 return "HEARING_AID"; 486 case LE_AUDIO: 487 return "LE_AUDIO"; 488 case VOLUME_CONTROL: 489 return "VOLUME_CONTROL"; 490 case MCP_SERVER: 491 return "MCP_SERVER"; 492 case CSIP_SET_COORDINATOR: 493 return "CSIP_SET_COORDINATOR"; 494 case LE_AUDIO_BROADCAST: 495 return "LE_AUDIO_BROADCAST"; 496 case LE_CALL_CONTROL: 497 return "LE_CALL_CONTROL"; 498 case HAP_CLIENT: 499 return "HAP_CLIENT"; 500 case LE_AUDIO_BROADCAST_ASSISTANT: 501 return "LE_AUDIO_BROADCAST_ASSISTANT"; 502 case BATTERY: 503 return "BATTERY"; 504 default: 505 return "UNKNOWN_PROFILE"; 506 } 507 } 508 } 509