Fixed bluetooth UI not shown if bluetooth adapter is turned on after app started
This commit is contained in:
parent
25d9af1c18
commit
d20379a484
2 changed files with 105 additions and 76 deletions
|
@ -517,6 +517,82 @@ public class AndroidAudioManager {
|
|||
}.start();
|
||||
}
|
||||
|
||||
public void bluetoothAdapterStateChanged() {
|
||||
if (mBluetoothAdapter.isEnabled()) {
|
||||
Log.i("[Audio Manager] [Bluetooth] Adapter enabled");
|
||||
mIsBluetoothHeadsetConnected = false;
|
||||
mIsBluetoothHeadsetScoConnected = false;
|
||||
|
||||
BluetoothProfile.ServiceListener bluetoothServiceListener =
|
||||
new BluetoothProfile.ServiceListener() {
|
||||
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
||||
if (profile == BluetoothProfile.HEADSET) {
|
||||
Log.i("[Audio Manager] [Bluetooth] HEADSET profile connected");
|
||||
mBluetoothHeadset = (BluetoothHeadset) proxy;
|
||||
|
||||
List<BluetoothDevice> devices =
|
||||
mBluetoothHeadset.getConnectedDevices();
|
||||
if (devices.size() > 0) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] A device is already connected");
|
||||
bluetoothHeadetConnectionChanged(true);
|
||||
}
|
||||
|
||||
Log.i("[Audio Manager] [Bluetooth] Registering bluetooth receiver");
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
|
||||
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
|
||||
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
|
||||
filter.addAction(
|
||||
BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
|
||||
|
||||
Intent sticky =
|
||||
mContext.registerReceiver(mBluetoothReceiver, filter);
|
||||
Log.i("[Audio Manager] [Bluetooth] Bluetooth receiver registered");
|
||||
int state =
|
||||
sticky.getIntExtra(
|
||||
AudioManager.EXTRA_SCO_AUDIO_STATE,
|
||||
AudioManager.SCO_AUDIO_STATE_DISCONNECTED);
|
||||
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO connected");
|
||||
bluetoothHeadetScoConnectionChanged(true);
|
||||
} else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO disconnected");
|
||||
bluetoothHeadetScoConnectionChanged(false);
|
||||
} else if (state == AudioManager.SCO_AUDIO_STATE_CONNECTING) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO connecting");
|
||||
} else if (state == AudioManager.SCO_AUDIO_STATE_ERROR) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO connection error");
|
||||
} else {
|
||||
Log.w(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset unknown SCO state changed: "
|
||||
+ state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onServiceDisconnected(int profile) {
|
||||
if (profile == BluetoothProfile.HEADSET) {
|
||||
Log.i("[Audio Manager] [Bluetooth] HEADSET profile disconnected");
|
||||
mBluetoothHeadset = null;
|
||||
mIsBluetoothHeadsetConnected = false;
|
||||
mIsBluetoothHeadsetScoConnected = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mBluetoothAdapter.getProfileProxy(
|
||||
mContext, bluetoothServiceListener, BluetoothProfile.HEADSET);
|
||||
} else {
|
||||
Log.w("[Audio Manager] [Bluetooth] Adapter disabled");
|
||||
}
|
||||
}
|
||||
|
||||
private void startBluetooth() {
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (mBluetoothAdapter != null) {
|
||||
|
@ -527,82 +603,11 @@ public class AndroidAudioManager {
|
|||
Log.w("[Audio Manager] [Bluetooth] SCO not available off call !");
|
||||
}
|
||||
|
||||
if (mBluetoothAdapter.isEnabled()) {
|
||||
Log.i("[Audio Manager] [Bluetooth] Adapter enabled");
|
||||
mBluetoothReceiver = new BluetoothReceiver();
|
||||
mIsBluetoothHeadsetConnected = false;
|
||||
mIsBluetoothHeadsetScoConnected = false;
|
||||
mBluetoothReceiver = new BluetoothReceiver();
|
||||
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
|
||||
mContext.registerReceiver(mBluetoothReceiver, filter);
|
||||
|
||||
BluetoothProfile.ServiceListener bluetoothServiceListener =
|
||||
new BluetoothProfile.ServiceListener() {
|
||||
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
||||
if (profile == BluetoothProfile.HEADSET) {
|
||||
Log.i("[Audio Manager] [Bluetooth] HEADSET profile connected");
|
||||
mBluetoothHeadset = (BluetoothHeadset) proxy;
|
||||
|
||||
List<BluetoothDevice> devices =
|
||||
mBluetoothHeadset.getConnectedDevices();
|
||||
if (devices.size() > 0) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] A device is already connected");
|
||||
bluetoothHeadetConnectionChanged(true);
|
||||
}
|
||||
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Registering bluetooth receiver");
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
|
||||
filter.addAction(
|
||||
BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
|
||||
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
|
||||
filter.addAction(
|
||||
BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
|
||||
|
||||
Intent sticky =
|
||||
mContext.registerReceiver(mBluetoothReceiver, filter);
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth receiver registered");
|
||||
int state =
|
||||
sticky.getIntExtra(
|
||||
AudioManager.EXTRA_SCO_AUDIO_STATE,
|
||||
AudioManager.SCO_AUDIO_STATE_DISCONNECTED);
|
||||
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO connected");
|
||||
bluetoothHeadetScoConnectionChanged(true);
|
||||
} else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO disconnected");
|
||||
bluetoothHeadetScoConnectionChanged(false);
|
||||
} else if (state == AudioManager.SCO_AUDIO_STATE_CONNECTING) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO connecting");
|
||||
} else if (state == AudioManager.SCO_AUDIO_STATE_ERROR) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset SCO connection error");
|
||||
} else {
|
||||
Log.w(
|
||||
"[Audio Manager] [Bluetooth] Bluetooth headset unknown SCO state changed: "
|
||||
+ state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onServiceDisconnected(int profile) {
|
||||
if (profile == BluetoothProfile.HEADSET) {
|
||||
Log.i(
|
||||
"[Audio Manager] [Bluetooth] HEADSET profile disconnected");
|
||||
mBluetoothHeadset = null;
|
||||
mIsBluetoothHeadsetConnected = false;
|
||||
mIsBluetoothHeadsetScoConnected = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mBluetoothAdapter.getProfileProxy(
|
||||
mContext, bluetoothServiceListener, BluetoothProfile.HEADSET);
|
||||
}
|
||||
bluetoothAdapterStateChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
package org.linphone.receivers;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothHeadset;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
|
@ -38,7 +39,30 @@ public class BluetoothReceiver extends BroadcastReceiver {
|
|||
String action = intent.getAction();
|
||||
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 =
|
||||
intent.getIntExtra(
|
||||
BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
|
||||
|
|
Loading…
Reference in a new issue