Fix crash in BT manager
This commit is contained in:
parent
40b6952993
commit
3b60a26469
1 changed files with 21 additions and 14 deletions
|
@ -67,20 +67,14 @@ public class BluetoothManager extends BroadcastReceiver {
|
||||||
|
|
||||||
public BluetoothManager() {
|
public BluetoothManager() {
|
||||||
isBluetoothConnected = false;
|
isBluetoothConnected = false;
|
||||||
if (LinphoneService.isReady()) {
|
if (!ensureInit()) {
|
||||||
mContext = LinphoneService.instance().getApplicationContext();
|
|
||||||
mAudioManager = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE));
|
|
||||||
} else {
|
|
||||||
Log.w("BluetoothManager tried to init but LinphoneService not ready yet...");
|
Log.w("BluetoothManager tried to init but LinphoneService not ready yet...");
|
||||||
}
|
}
|
||||||
instance = this;
|
instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initBluetooth() {
|
public void initBluetooth() {
|
||||||
if (mContext == null && LinphoneService.isReady()) {
|
if (!ensureInit()) {
|
||||||
mContext = LinphoneService.instance().getApplicationContext();
|
|
||||||
mAudioManager = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE));
|
|
||||||
} else if (mContext == null) {
|
|
||||||
Log.w("BluetoothManager tried to init bluetooth but LinphoneService not ready yet...");
|
Log.w("BluetoothManager tried to init bluetooth but LinphoneService not ready yet...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -136,12 +130,27 @@ public class BluetoothManager extends BroadcastReceiver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean routeAudioToBluetooth() {
|
private boolean ensureInit() {
|
||||||
if (mBluetoothAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
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 (isBluetoothHeadsetAvailable()) {
|
||||||
if (mAudioManager != null && !mAudioManager.isBluetoothScoOn()) {
|
if (mAudioManager != null && !mAudioManager.isBluetoothScoOn()) {
|
||||||
Log.d("Bluetooth sco off, let's start it");
|
Log.d("Bluetooth sco off, let's start it");
|
||||||
|
@ -190,11 +199,9 @@ public class BluetoothManager extends BroadcastReceiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBluetoothHeadsetAvailable() {
|
public boolean isBluetoothHeadsetAvailable() {
|
||||||
if (mBluetoothAdapter == null) {
|
ensureInit();
|
||||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager.isBluetoothScoAvailableOffCall()) {
|
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager != null && mAudioManager.isBluetoothScoAvailableOffCall()) {
|
||||||
boolean isHeadsetConnected = false;
|
boolean isHeadsetConnected = false;
|
||||||
if (mBluetoothHeadset != null) {
|
if (mBluetoothHeadset != null) {
|
||||||
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
|
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
|
||||||
|
|
Loading…
Reference in a new issue