From 3b60a264696759b47bb0316a01c60e1ed44cac48 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 12 Dec 2014 09:40:28 +0100 Subject: [PATCH] Fix crash in BT manager --- src/org/linphone/BluetoothManager.java | 35 +++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/org/linphone/BluetoothManager.java b/src/org/linphone/BluetoothManager.java index 7a3de4307..dd9c15a5c 100644 --- a/src/org/linphone/BluetoothManager.java +++ b/src/org/linphone/BluetoothManager.java @@ -67,20 +67,14 @@ public class BluetoothManager extends BroadcastReceiver { public BluetoothManager() { isBluetoothConnected = false; - if (LinphoneService.isReady()) { - mContext = LinphoneService.instance().getApplicationContext(); - mAudioManager = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)); - } else { + if (!ensureInit()) { Log.w("BluetoothManager tried to init but LinphoneService not ready yet..."); } instance = this; } public void initBluetooth() { - if (mContext == null && LinphoneService.isReady()) { - mContext = LinphoneService.instance().getApplicationContext(); - mAudioManager = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)); - } else if (mContext == null) { + if (!ensureInit()) { Log.w("BluetoothManager tried to init bluetooth but LinphoneService not ready yet..."); return; } @@ -136,12 +130,27 @@ public class BluetoothManager extends BroadcastReceiver { } } - public boolean routeAudioToBluetooth() { + private boolean ensureInit() { if (mBluetoothAdapter == null) { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } + if (mContext == null) { + if (LinphoneService.isReady()) { + mContext = LinphoneService.instance().getApplicationContext(); + } else { + return false; + } + } + if (mContext != null && mAudioManager == null) { + mAudioManager = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)); + } + return true; + } + + public boolean routeAudioToBluetooth() { + ensureInit(); - if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager.isBluetoothScoAvailableOffCall()) { + if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager != null && mAudioManager.isBluetoothScoAvailableOffCall()) { if (isBluetoothHeadsetAvailable()) { if (mAudioManager != null && !mAudioManager.isBluetoothScoOn()) { Log.d("Bluetooth sco off, let's start it"); @@ -190,11 +199,9 @@ public class BluetoothManager extends BroadcastReceiver { } public boolean isBluetoothHeadsetAvailable() { - if (mBluetoothAdapter == null) { - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - } + ensureInit(); - if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager.isBluetoothScoAvailableOffCall()) { + if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager != null && mAudioManager.isBluetoothScoAvailableOffCall()) { boolean isHeadsetConnected = false; if (mBluetoothHeadset != null) { List devices = mBluetoothHeadset.getConnectedDevices();