MagicBluetoothManager.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Original code:
  21. *
  22. *
  23. * Copyright 2016 The WebRTC Project Authors. All rights reserved.
  24. *
  25. * Use of this source code is governed by a BSD-style license
  26. * that can be found in the LICENSE file in the root of the source
  27. * tree. An additional intellectual property rights grant can be found
  28. * in the file PATENTS. All contributing project authors may
  29. * be found in the AUTHORS file in the root of the source tree.
  30. */
  31. package com.nextcloud.talk.webrtc;
  32. import android.annotation.SuppressLint;
  33. import android.bluetooth.BluetoothAdapter;
  34. import android.bluetooth.BluetoothDevice;
  35. import android.bluetooth.BluetoothHeadset;
  36. import android.bluetooth.BluetoothProfile;
  37. import android.content.BroadcastReceiver;
  38. import android.content.Context;
  39. import android.content.Intent;
  40. import android.content.IntentFilter;
  41. import android.content.pm.PackageManager;
  42. import android.media.AudioManager;
  43. import android.os.Build;
  44. import android.os.Handler;
  45. import android.os.Looper;
  46. import android.os.Process;
  47. import android.util.Log;
  48. import org.webrtc.ThreadUtils;
  49. import java.util.List;
  50. import java.util.Set;
  51. public class MagicBluetoothManager {
  52. private static final String TAG = "MagicBluetoothManager";
  53. // Timeout interval for starting or stopping audio to a Bluetooth SCO device.
  54. private static final int BLUETOOTH_SCO_TIMEOUT_MS = 4000;
  55. // Maximum number of SCO connection attempts.
  56. private static final int MAX_SCO_CONNECTION_ATTEMPTS = 2;
  57. private final Context apprtcContext;
  58. private final MagicAudioManager apprtcAudioManager;
  59. private final AudioManager audioManager;
  60. private final Handler handler;
  61. private final BluetoothProfile.ServiceListener bluetoothServiceListener;
  62. private final BroadcastReceiver bluetoothHeadsetReceiver;
  63. int scoConnectionAttempts;
  64. private State bluetoothState;
  65. private BluetoothAdapter bluetoothAdapter;
  66. private BluetoothHeadset bluetoothHeadset;
  67. private BluetoothDevice bluetoothDevice;
  68. // Runs when the Bluetooth timeout expires. We use that timeout after calling
  69. // startScoAudio() or stopScoAudio() because we're not guaranteed to get a
  70. // callback after those calls.
  71. private final Runnable bluetoothTimeoutRunnable = new Runnable() {
  72. @Override
  73. public void run() {
  74. bluetoothTimeout();
  75. }
  76. };
  77. protected MagicBluetoothManager(Context context, MagicAudioManager audioManager) {
  78. Log.d(TAG, "ctor");
  79. ThreadUtils.checkIsOnMainThread();
  80. apprtcContext = context;
  81. apprtcAudioManager = audioManager;
  82. this.audioManager = getAudioManager(context);
  83. bluetoothState = State.UNINITIALIZED;
  84. bluetoothServiceListener = new BluetoothServiceListener();
  85. bluetoothHeadsetReceiver = new BluetoothHeadsetBroadcastReceiver();
  86. handler = new Handler(Looper.getMainLooper());
  87. }
  88. /**
  89. * Construction.
  90. */
  91. static MagicBluetoothManager create(Context context, MagicAudioManager audioManager) {
  92. return new MagicBluetoothManager(context, audioManager);
  93. }
  94. /**
  95. * Returns the internal state.
  96. */
  97. public State getState() {
  98. ThreadUtils.checkIsOnMainThread();
  99. return bluetoothState;
  100. }
  101. ;
  102. /**
  103. * Activates components required to detect Bluetooth devices and to enable
  104. * BT SCO (audio is routed via BT SCO) for the headset profile. The end
  105. * state will be HEADSET_UNAVAILABLE but a state machine has started which
  106. * will start a state change sequence where the final outcome depends on
  107. * if/when the BT headset is enabled.
  108. * Example of state change sequence when start() is called while BT device
  109. * is connected and enabled:
  110. * UNINITIALIZED --> HEADSET_UNAVAILABLE --> HEADSET_AVAILABLE -->
  111. * SCO_CONNECTING --> SCO_CONNECTED <==> audio is now routed via BT SCO.
  112. * Note that the MagicAudioManager is also involved in driving this state
  113. * change.
  114. */
  115. public void start() {
  116. ThreadUtils.checkIsOnMainThread();
  117. Log.d(TAG, "start");
  118. if (!hasPermission(apprtcContext, android.Manifest.permission.BLUETOOTH)) {
  119. Log.w(TAG, "Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission");
  120. return;
  121. }
  122. if (bluetoothState != State.UNINITIALIZED) {
  123. Log.w(TAG, "Invalid BT state");
  124. return;
  125. }
  126. bluetoothHeadset = null;
  127. bluetoothDevice = null;
  128. scoConnectionAttempts = 0;
  129. // Get a handle to the default local Bluetooth adapter.
  130. bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  131. if (bluetoothAdapter == null) {
  132. Log.w(TAG, "Device does not support Bluetooth");
  133. return;
  134. }
  135. // Ensure that the device supports use of BT SCO audio for off call use cases.
  136. if (!audioManager.isBluetoothScoAvailableOffCall()) {
  137. Log.e(TAG, "Bluetooth SCO audio is not available off call");
  138. return;
  139. }
  140. logBluetoothAdapterInfo(bluetoothAdapter);
  141. // Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
  142. // Hands-Free) proxy object and install a listener.
  143. if (!getBluetoothProfileProxy(
  144. apprtcContext, bluetoothServiceListener, BluetoothProfile.HEADSET)) {
  145. Log.e(TAG, "BluetoothAdapter.getProfileProxy(HEADSET) failed");
  146. return;
  147. }
  148. // Register receivers for BluetoothHeadset change notifications.
  149. IntentFilter bluetoothHeadsetFilter = new IntentFilter();
  150. // Register receiver for change in connection state of the Headset profile.
  151. bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
  152. // Register receiver for change in audio connection state of the Headset profile.
  153. bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
  154. registerReceiver(bluetoothHeadsetReceiver, bluetoothHeadsetFilter);
  155. Log.d(TAG, "HEADSET profile state: "
  156. + stateToString(bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)));
  157. Log.d(TAG, "Bluetooth proxy for headset profile has started");
  158. bluetoothState = State.HEADSET_UNAVAILABLE;
  159. Log.d(TAG, "start done: BT state=" + bluetoothState);
  160. }
  161. /**
  162. * Stops and closes all components related to Bluetooth audio.
  163. */
  164. public void stop() {
  165. ThreadUtils.checkIsOnMainThread();
  166. Log.d(TAG, "stop: BT state=" + bluetoothState);
  167. if (bluetoothAdapter == null) {
  168. return;
  169. }
  170. // Stop BT SCO connection with remote device if needed.
  171. stopScoAudio();
  172. // Close down remaining BT resources.
  173. if (bluetoothState == State.UNINITIALIZED) {
  174. return;
  175. }
  176. unregisterReceiver(bluetoothHeadsetReceiver);
  177. cancelTimer();
  178. if (bluetoothHeadset != null) {
  179. bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
  180. bluetoothHeadset = null;
  181. }
  182. bluetoothAdapter = null;
  183. bluetoothDevice = null;
  184. bluetoothState = State.UNINITIALIZED;
  185. Log.d(TAG, "stop done: BT state=" + bluetoothState);
  186. }
  187. /**
  188. * Starts Bluetooth SCO connection with remote device.
  189. * Note that the phone application always has the priority on the usage of the SCO connection
  190. * for telephony. If this method is called while the phone is in call it will be ignored.
  191. * Similarly, if a call is received or sent while an application is using the SCO connection,
  192. * the connection will be lost for the application and NOT returned automatically when the call
  193. * ends. Also note that: up to and including API version JELLY_BEAN_MR1, this method initiates a
  194. * virtual voice call to the Bluetooth headset. After API version JELLY_BEAN_MR2 only a raw SCO
  195. * audio connection is established.
  196. * TODO(henrika): should we add support for virtual voice call to BT headset also for JBMR2 and
  197. * higher. It might be required to initiates a virtual voice call since many devices do not
  198. * accept SCO audio without a "call".
  199. */
  200. public boolean startScoAudio() {
  201. ThreadUtils.checkIsOnMainThread();
  202. Log.d(TAG, "startSco: BT state=" + bluetoothState + ", "
  203. + "attempts: " + scoConnectionAttempts + ", "
  204. + "SCO is on: " + isScoOn());
  205. if (scoConnectionAttempts >= MAX_SCO_CONNECTION_ATTEMPTS) {
  206. Log.e(TAG, "BT SCO connection fails - no more attempts");
  207. return false;
  208. }
  209. if (bluetoothState != State.HEADSET_AVAILABLE) {
  210. Log.e(TAG, "BT SCO connection fails - no headset available");
  211. return false;
  212. }
  213. // Start BT SCO channel and wait for ACTION_AUDIO_STATE_CHANGED.
  214. Log.d(TAG, "Starting Bluetooth SCO and waits for ACTION_AUDIO_STATE_CHANGED...");
  215. // The SCO connection establishment can take several seconds, hence we cannot rely on the
  216. // connection to be available when the method returns but instead register to receive the
  217. // intent ACTION_SCO_AUDIO_STATE_UPDATED and wait for the state to be SCO_AUDIO_STATE_CONNECTED.
  218. bluetoothState = State.SCO_CONNECTING;
  219. audioManager.startBluetoothSco();
  220. audioManager.setBluetoothScoOn(true);
  221. scoConnectionAttempts++;
  222. startTimer();
  223. Log.d(TAG, "startScoAudio done: BT state=" + bluetoothState + ", "
  224. + "SCO is on: " + isScoOn());
  225. return true;
  226. }
  227. /**
  228. * Stops Bluetooth SCO connection with remote device.
  229. */
  230. public void stopScoAudio() {
  231. ThreadUtils.checkIsOnMainThread();
  232. Log.d(TAG, "stopScoAudio: BT state=" + bluetoothState + ", "
  233. + "SCO is on: " + isScoOn());
  234. if (bluetoothState != State.SCO_CONNECTING && bluetoothState != State.SCO_CONNECTED) {
  235. return;
  236. }
  237. cancelTimer();
  238. audioManager.stopBluetoothSco();
  239. audioManager.setBluetoothScoOn(false);
  240. bluetoothState = State.SCO_DISCONNECTING;
  241. Log.d(TAG, "stopScoAudio done: BT state=" + bluetoothState + ", "
  242. + "SCO is on: " + isScoOn());
  243. }
  244. /**
  245. * Use the BluetoothHeadset proxy object (controls the Bluetooth Headset
  246. * Service via IPC) to update the list of connected devices for the HEADSET
  247. * profile. The internal state will change to HEADSET_UNAVAILABLE or to
  248. * HEADSET_AVAILABLE and |bluetoothDevice| will be mapped to the connected
  249. * device if available.
  250. */
  251. public void updateDevice() {
  252. if (bluetoothState == State.UNINITIALIZED || bluetoothHeadset == null) {
  253. return;
  254. }
  255. Log.d(TAG, "updateDevice");
  256. // Get connected devices for the headset profile. Returns the set of
  257. // devices which are in state STATE_CONNECTED. The BluetoothDevice class
  258. // is just a thin wrapper for a Bluetooth hardware address.
  259. List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
  260. if (devices.isEmpty()) {
  261. bluetoothDevice = null;
  262. bluetoothState = State.HEADSET_UNAVAILABLE;
  263. Log.d(TAG, "No connected bluetooth headset");
  264. } else {
  265. // Always use first device in list. Android only supports one device.
  266. bluetoothDevice = devices.get(0);
  267. bluetoothState = State.HEADSET_AVAILABLE;
  268. Log.d(TAG, "Connected bluetooth headset: "
  269. + "name=" + bluetoothDevice.getName() + ", "
  270. + "state=" + stateToString(bluetoothHeadset.getConnectionState(bluetoothDevice))
  271. + ", SCO audio=" + bluetoothHeadset.isAudioConnected(bluetoothDevice));
  272. }
  273. Log.d(TAG, "updateDevice done: BT state=" + bluetoothState);
  274. }
  275. /**
  276. * Stubs for test mocks.
  277. */
  278. protected AudioManager getAudioManager(Context context) {
  279. return (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  280. }
  281. protected void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
  282. apprtcContext.registerReceiver(receiver, filter);
  283. }
  284. protected void unregisterReceiver(BroadcastReceiver receiver) {
  285. apprtcContext.unregisterReceiver(receiver);
  286. }
  287. protected boolean getBluetoothProfileProxy(
  288. Context context, BluetoothProfile.ServiceListener listener, int profile) {
  289. return bluetoothAdapter.getProfileProxy(context, listener, profile);
  290. }
  291. protected boolean hasPermission(Context context, String permission) {
  292. return apprtcContext.checkPermission(permission, Process.myPid(), Process.myUid())
  293. == PackageManager.PERMISSION_GRANTED;
  294. }
  295. /**
  296. * Logs the state of the local Bluetooth adapter.
  297. */
  298. @SuppressLint("HardwareIds")
  299. protected void logBluetoothAdapterInfo(BluetoothAdapter localAdapter) {
  300. Log.d(TAG, "BluetoothAdapter: "
  301. + "enabled=" + localAdapter.isEnabled() + ", "
  302. + "state=" + stateToString(localAdapter.getState()) + ", "
  303. + "name=" + localAdapter.getName());
  304. // Log the set of BluetoothDevice objects that are bonded (paired) to the local adapter.
  305. Set<BluetoothDevice> pairedDevices = localAdapter.getBondedDevices();
  306. if (!pairedDevices.isEmpty()) {
  307. Log.d(TAG, "paired devices:");
  308. for (BluetoothDevice device : pairedDevices) {
  309. Log.d(TAG, " name=" + device.getName() + ", address=" + device.getAddress());
  310. }
  311. }
  312. }
  313. /**
  314. * Ensures that the audio manager updates its list of available audio devices.
  315. */
  316. private void updateAudioDeviceState() {
  317. ThreadUtils.checkIsOnMainThread();
  318. Log.d(TAG, "updateAudioDeviceState");
  319. apprtcAudioManager.updateAudioDeviceState();
  320. }
  321. /**
  322. * Starts timer which times out after BLUETOOTH_SCO_TIMEOUT_MS milliseconds.
  323. */
  324. private void startTimer() {
  325. ThreadUtils.checkIsOnMainThread();
  326. Log.d(TAG, "startTimer");
  327. handler.postDelayed(bluetoothTimeoutRunnable, BLUETOOTH_SCO_TIMEOUT_MS);
  328. }
  329. /**
  330. * Cancels any outstanding timer tasks.
  331. */
  332. private void cancelTimer() {
  333. ThreadUtils.checkIsOnMainThread();
  334. Log.d(TAG, "cancelTimer");
  335. handler.removeCallbacks(bluetoothTimeoutRunnable);
  336. }
  337. /**
  338. * Called when start of the BT SCO channel takes too long time. Usually
  339. * happens when the BT device has been turned on during an ongoing call.
  340. */
  341. private void bluetoothTimeout() {
  342. ThreadUtils.checkIsOnMainThread();
  343. if (bluetoothState == State.UNINITIALIZED || bluetoothHeadset == null) {
  344. return;
  345. }
  346. Log.d(TAG, "bluetoothTimeout: BT state=" + bluetoothState + ", "
  347. + "attempts: " + scoConnectionAttempts + ", "
  348. + "SCO is on: " + isScoOn());
  349. if (bluetoothState != State.SCO_CONNECTING) {
  350. return;
  351. }
  352. // Bluetooth SCO should be connecting; check the latest result.
  353. boolean scoConnected = false;
  354. List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
  355. if (devices.size() > 0) {
  356. bluetoothDevice = devices.get(0);
  357. if (bluetoothHeadset.isAudioConnected(bluetoothDevice)) {
  358. Log.d(TAG, "SCO connected with " + bluetoothDevice.getName());
  359. scoConnected = true;
  360. } else {
  361. Log.d(TAG, "SCO is not connected with " + bluetoothDevice.getName());
  362. }
  363. }
  364. if (scoConnected) {
  365. // We thought BT had timed out, but it's actually on; updating state.
  366. bluetoothState = State.SCO_CONNECTED;
  367. scoConnectionAttempts = 0;
  368. } else {
  369. // Give up and "cancel" our request by calling stopBluetoothSco().
  370. Log.w(TAG, "BT failed to connect after timeout");
  371. stopScoAudio();
  372. }
  373. updateAudioDeviceState();
  374. Log.d(TAG, "bluetoothTimeout done: BT state=" + bluetoothState);
  375. }
  376. /**
  377. * Checks whether audio uses Bluetooth SCO.
  378. */
  379. private boolean isScoOn() {
  380. return audioManager.isBluetoothScoOn();
  381. }
  382. /**
  383. * Converts BluetoothAdapter states into local string representations.
  384. */
  385. private String stateToString(int state) {
  386. switch (state) {
  387. case BluetoothAdapter.STATE_DISCONNECTED:
  388. return "DISCONNECTED";
  389. case BluetoothAdapter.STATE_CONNECTED:
  390. return "CONNECTED";
  391. case BluetoothAdapter.STATE_CONNECTING:
  392. return "CONNECTING";
  393. case BluetoothAdapter.STATE_DISCONNECTING:
  394. return "DISCONNECTING";
  395. case BluetoothAdapter.STATE_OFF:
  396. return "OFF";
  397. case BluetoothAdapter.STATE_ON:
  398. return "ON";
  399. case BluetoothAdapter.STATE_TURNING_OFF:
  400. // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
  401. // attempt graceful disconnection of any remote links.
  402. return "TURNING_OFF";
  403. case BluetoothAdapter.STATE_TURNING_ON:
  404. // Indicates the local Bluetooth adapter is turning on. However local clients should wait
  405. // for STATE_ON before attempting to use the adapter.
  406. return "TURNING_ON";
  407. default:
  408. return "INVALID";
  409. }
  410. }
  411. // Bluetooth connection state.
  412. public enum State {
  413. // Bluetooth is not available; no adapter or Bluetooth is off.
  414. UNINITIALIZED,
  415. // Bluetooth error happened when trying to start Bluetooth.
  416. ERROR,
  417. // Bluetooth proxy object for the Headset profile exists, but no connected headset devices,
  418. // SCO is not started or disconnected.
  419. HEADSET_UNAVAILABLE,
  420. // Bluetooth proxy object for the Headset profile connected, connected Bluetooth headset
  421. // present, but SCO is not started or disconnected.
  422. HEADSET_AVAILABLE,
  423. // Bluetooth audio SCO connection with remote device is closing.
  424. SCO_DISCONNECTING,
  425. // Bluetooth audio SCO connection with remote device is initiated.
  426. SCO_CONNECTING,
  427. // Bluetooth audio SCO connection with remote device is established.
  428. SCO_CONNECTED
  429. }
  430. /**
  431. * Implementation of an interface that notifies BluetoothProfile IPC clients when they have been
  432. * connected to or disconnected from the service.
  433. */
  434. private class BluetoothServiceListener implements BluetoothProfile.ServiceListener {
  435. @Override
  436. // Called to notify the client when the proxy object has been connected to the service.
  437. // Once we have the profile proxy object, we can use it to monitor the state of the
  438. // connection and perform other operations that are relevant to the headset profile.
  439. public void onServiceConnected(int profile, BluetoothProfile proxy) {
  440. if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
  441. return;
  442. }
  443. Log.d(TAG, "BluetoothServiceListener.onServiceConnected: BT state=" + bluetoothState);
  444. // Android only supports one connected Bluetooth Headset at a time.
  445. bluetoothHeadset = (BluetoothHeadset) proxy;
  446. updateAudioDeviceState();
  447. Log.d(TAG, "onServiceConnected done: BT state=" + bluetoothState);
  448. }
  449. @Override
  450. /** Notifies the client when the proxy object has been disconnected from the service. */
  451. public void onServiceDisconnected(int profile) {
  452. if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
  453. return;
  454. }
  455. Log.d(TAG, "BluetoothServiceListener.onServiceDisconnected: BT state=" + bluetoothState);
  456. stopScoAudio();
  457. bluetoothHeadset = null;
  458. bluetoothDevice = null;
  459. bluetoothState = State.HEADSET_UNAVAILABLE;
  460. updateAudioDeviceState();
  461. Log.d(TAG, "onServiceDisconnected done: BT state=" + bluetoothState);
  462. }
  463. }
  464. // Intent broadcast receiver which handles changes in Bluetooth device availability.
  465. // Detects headset changes and Bluetooth SCO state changes.
  466. private class BluetoothHeadsetBroadcastReceiver extends BroadcastReceiver {
  467. @Override
  468. public void onReceive(Context context, Intent intent) {
  469. if (bluetoothState == State.UNINITIALIZED) {
  470. return;
  471. }
  472. final String action = intent.getAction();
  473. // Change in connection state of the Headset profile. Note that the
  474. // change does not tell us anything about whether we're streaming
  475. // audio to BT over SCO. Typically received when user turns on a BT
  476. // headset while audio is active using another audio device.
  477. if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
  478. final int state =
  479. intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
  480. Log.d(TAG, "BluetoothHeadsetBroadcastReceiver.onReceive: "
  481. + "a=ACTION_CONNECTION_STATE_CHANGED, "
  482. + "s=" + stateToString(state) + ", "
  483. + "sb=" + isInitialStickyBroadcast() + ", "
  484. + "BT state: " + bluetoothState);
  485. if (state == BluetoothHeadset.STATE_CONNECTED) {
  486. scoConnectionAttempts = 0;
  487. updateAudioDeviceState();
  488. } else if (state == BluetoothHeadset.STATE_CONNECTING) {
  489. // No action needed.
  490. } else if (state == BluetoothHeadset.STATE_DISCONNECTING) {
  491. // No action needed.
  492. } else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
  493. // Bluetooth is probably powered off during the call.
  494. stopScoAudio();
  495. updateAudioDeviceState();
  496. }
  497. // Change in the audio (SCO) connection state of the Headset profile.
  498. // Typically received after call to startScoAudio() has finalized.
  499. } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
  500. final int state = intent.getIntExtra(
  501. BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
  502. Log.d(TAG, "BluetoothHeadsetBroadcastReceiver.onReceive: "
  503. + "a=ACTION_AUDIO_STATE_CHANGED, "
  504. + "s=" + stateToString(state) + ", "
  505. + "sb=" + isInitialStickyBroadcast() + ", "
  506. + "BT state: " + bluetoothState);
  507. if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
  508. cancelTimer();
  509. if (bluetoothState == State.SCO_CONNECTING) {
  510. Log.d(TAG, "+++ Bluetooth audio SCO is now connected");
  511. bluetoothState = State.SCO_CONNECTED;
  512. scoConnectionAttempts = 0;
  513. updateAudioDeviceState();
  514. } else {
  515. Log.w(TAG, "Unexpected state BluetoothHeadset.STATE_AUDIO_CONNECTED");
  516. }
  517. } else if (state == BluetoothHeadset.STATE_AUDIO_CONNECTING) {
  518. Log.d(TAG, "+++ Bluetooth audio SCO is now connecting...");
  519. } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
  520. Log.d(TAG, "+++ Bluetooth audio SCO is now disconnected");
  521. if (isInitialStickyBroadcast()) {
  522. Log.d(TAG, "Ignore STATE_AUDIO_DISCONNECTED initial sticky broadcast.");
  523. return;
  524. }
  525. updateAudioDeviceState();
  526. }
  527. }
  528. Log.d(TAG, "onReceive done: BT state=" + bluetoothState);
  529. }
  530. }
  531. }