Fixed bluetooth UI not shown if bluetooth adapter is turned on after app started

This commit is contained in:
Sylvain Berfini 2019-12-02 13:19:19 +01:00
parent 25d9af1c18
commit d20379a484
2 changed files with 105 additions and 76 deletions

View file

@ -517,19 +517,9 @@ public class AndroidAudioManager {
}.start(); }.start();
} }
private void startBluetooth() { public void bluetoothAdapterStateChanged() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
Log.i("[Audio Manager] [Bluetooth] Adapter found");
if (mAudioManager.isBluetoothScoAvailableOffCall()) {
Log.i("[Audio Manager] [Bluetooth] SCO available off call, continue");
} else {
Log.w("[Audio Manager] [Bluetooth] SCO not available off call !");
}
if (mBluetoothAdapter.isEnabled()) { if (mBluetoothAdapter.isEnabled()) {
Log.i("[Audio Manager] [Bluetooth] Adapter enabled"); Log.i("[Audio Manager] [Bluetooth] Adapter enabled");
mBluetoothReceiver = new BluetoothReceiver();
mIsBluetoothHeadsetConnected = false; mIsBluetoothHeadsetConnected = false;
mIsBluetoothHeadsetScoConnected = false; mIsBluetoothHeadsetScoConnected = false;
@ -548,21 +538,18 @@ public class AndroidAudioManager {
bluetoothHeadetConnectionChanged(true); bluetoothHeadetConnectionChanged(true);
} }
Log.i( Log.i("[Audio Manager] [Bluetooth] Registering bluetooth receiver");
"[Audio Manager] [Bluetooth] Registering bluetooth receiver");
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
filter.addAction( filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
filter.addAction( filter.addAction(
BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT); BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
Intent sticky = Intent sticky =
mContext.registerReceiver(mBluetoothReceiver, filter); mContext.registerReceiver(mBluetoothReceiver, filter);
Log.i( Log.i("[Audio Manager] [Bluetooth] Bluetooth receiver registered");
"[Audio Manager] [Bluetooth] Bluetooth receiver registered");
int state = int state =
sticky.getIntExtra( sticky.getIntExtra(
AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.EXTRA_SCO_AUDIO_STATE,
@ -591,8 +578,7 @@ public class AndroidAudioManager {
public void onServiceDisconnected(int profile) { public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) { if (profile == BluetoothProfile.HEADSET) {
Log.i( Log.i("[Audio Manager] [Bluetooth] HEADSET profile disconnected");
"[Audio Manager] [Bluetooth] HEADSET profile disconnected");
mBluetoothHeadset = null; mBluetoothHeadset = null;
mIsBluetoothHeadsetConnected = false; mIsBluetoothHeadsetConnected = false;
mIsBluetoothHeadsetScoConnected = false; mIsBluetoothHeadsetScoConnected = false;
@ -602,8 +588,27 @@ public class AndroidAudioManager {
mBluetoothAdapter.getProfileProxy( mBluetoothAdapter.getProfileProxy(
mContext, bluetoothServiceListener, BluetoothProfile.HEADSET); mContext, bluetoothServiceListener, BluetoothProfile.HEADSET);
} else {
Log.w("[Audio Manager] [Bluetooth] Adapter disabled");
} }
} }
private void startBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
Log.i("[Audio Manager] [Bluetooth] Adapter found");
if (mAudioManager.isBluetoothScoAvailableOffCall()) {
Log.i("[Audio Manager] [Bluetooth] SCO available off call, continue");
} else {
Log.w("[Audio Manager] [Bluetooth] SCO not available off call !");
}
mBluetoothReceiver = new BluetoothReceiver();
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
mContext.registerReceiver(mBluetoothReceiver, filter);
bluetoothAdapterStateChanged();
}
} }
// HEADSET // HEADSET

View file

@ -19,6 +19,7 @@
*/ */
package org.linphone.receivers; package org.linphone.receivers;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -38,7 +39,30 @@ public class BluetoothReceiver extends BroadcastReceiver {
String action = intent.getAction(); String action = intent.getAction();
Log.i("[Bluetooth] Bluetooth broadcast received"); Log.i("[Bluetooth] Bluetooth broadcast received");
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
Log.w("[Bluetooth] Adapter has been turned off");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.w("[Bluetooth] Adapter is being turned off");
break;
case BluetoothAdapter.STATE_ON:
Log.i("[Bluetooth] Adapter has been turned on");
LinphoneManager.getAudioManager().bluetoothAdapterStateChanged();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.i("[Bluetooth] Adapter is being turned on");
break;
case BluetoothAdapter.ERROR:
Log.e("[Bluetooth] Adapter is in error state !");
break;
default:
Log.w("[Bluetooth] Unknown adapter state: ", state);
break;
}
} else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
int state = int state =
intent.getIntExtra( intent.getIntExtra(
BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED); BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);