Bluetooth support for android 2.2+ added
This commit is contained in:
parent
468a422da8
commit
b5d1e09580
2 changed files with 105 additions and 49 deletions
|
@ -2,39 +2,68 @@ package org.linphone;
|
||||||
|
|
||||||
import org.linphone.mediastream.Log;
|
import org.linphone.mediastream.Log;
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.annotation.TargetApi;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.media.AudioManager;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
public class BluetoothManager extends BroadcastReceiver {
|
public class BluetoothManager extends BroadcastReceiver {
|
||||||
public void onReceive(Context context, Intent intent) {
|
@SuppressWarnings("deprecation")
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
boolean routeToBT = context.getResources().getBoolean(R.bool.route_audio_to_bluetooth_if_available);
|
boolean routeToBT = context.getResources().getBoolean(R.bool.route_audio_to_bluetooth_if_available);
|
||||||
if (!routeToBT)
|
if (!routeToBT)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
LinphoneManager lm = LinphoneManager.getInstance();
|
LinphoneManager lm = LinphoneManager.getInstance();
|
||||||
|
|
||||||
|
String actionScoConnected = AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED;
|
||||||
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
|
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
|
||||||
Log.e("Bluetooth Received Event" + " ACTION_ACL_DISCONNECTED" );
|
Log.e("Bluetooth Received Event" + " ACTION_ACL_DISCONNECTED" );
|
||||||
|
|
||||||
if (lm != null) {
|
if (lm != null) {
|
||||||
lm.uninitBluetooth();
|
lm.isBluetoothScoConnected = false;
|
||||||
|
lm.scoDisconnected();
|
||||||
lm.routeAudioToReceiver();
|
lm.routeAudioToReceiver();
|
||||||
}
|
}
|
||||||
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
|
}
|
||||||
|
else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
|
||||||
Log.e("Bluetooth Received Event" + " ACTION_ACL_CONNECTED" );
|
Log.e("Bluetooth Received Event" + " ACTION_ACL_CONNECTED" );
|
||||||
|
|
||||||
if (lm != null) {
|
if (lm != null) {
|
||||||
lm.routeToBluetoothIfAvailable();
|
lm.isBluetoothScoConnected = true;
|
||||||
|
lm.scoConnected();
|
||||||
}
|
}
|
||||||
} else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
|
}
|
||||||
Log.e("Bluetooth state changed!");
|
else if (actionScoConnected.equals(action)) {
|
||||||
if (lm != null) {
|
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, 0);
|
||||||
|
Log.e("Bluetooth sco state changed : " + state);
|
||||||
|
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
|
||||||
|
if (lm != null) {
|
||||||
|
lm.isBluetoothScoConnected = true;
|
||||||
|
lm.scoConnected();
|
||||||
|
}
|
||||||
|
} else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
|
||||||
|
if (lm != null) {
|
||||||
|
lm.isBluetoothScoConnected = false;
|
||||||
|
lm.scoDisconnected();
|
||||||
|
lm.routeAudioToReceiver();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Using real value instead of constant because not available before sdk 11
|
||||||
|
else if ("android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED".equals(action)) { //BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED
|
||||||
|
int currentConnState = intent.getIntExtra("android.bluetooth.adapter.extra.CONNECTION_STATE", //BluetoothAdapter.EXTRA_CONNECTION_STATE
|
||||||
|
0); //BluetoothAdapter.STATE_DISCONNECTED
|
||||||
|
Log.e("Bluetooth state changed: " + currentConnState);
|
||||||
|
if (lm != null && currentConnState == 2) { //BluetoothAdapter.STATE_CONNECTED
|
||||||
|
lm.isBluetoothScoConnected = true;
|
||||||
lm.startBluetooth();
|
lm.startBluetooth();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,6 +150,8 @@ public final class LinphoneManager implements LinphoneCoreListener {
|
||||||
private BluetoothAdapter mBluetoothAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
private BluetoothHeadset mBluetoothHeadset;
|
private BluetoothHeadset mBluetoothHeadset;
|
||||||
private BluetoothProfile.ServiceListener mProfileListener;
|
private BluetoothProfile.ServiceListener mProfileListener;
|
||||||
|
private BroadcastReceiver bluetoothReiceiver = new BluetoothManager();
|
||||||
|
public boolean isBluetoothScoConnected;
|
||||||
|
|
||||||
private static List<LinphoneSimpleListener> simpleListeners = new ArrayList<LinphoneSimpleListener>();
|
private static List<LinphoneSimpleListener> simpleListeners = new ArrayList<LinphoneSimpleListener>();
|
||||||
public static void addListener(LinphoneSimpleListener listener) {
|
public static void addListener(LinphoneSimpleListener listener) {
|
||||||
|
@ -204,14 +206,14 @@ public final class LinphoneManager implements LinphoneCoreListener {
|
||||||
boolean routeToBT = mServiceContext.getResources().getBoolean(R.bool.route_audio_to_bluetooth_if_available);
|
boolean routeToBT = mServiceContext.getResources().getBoolean(R.bool.route_audio_to_bluetooth_if_available);
|
||||||
if (!routeToBT || (routeToBT && !routeToBluetoothIfAvailable())) {
|
if (!routeToBT || (routeToBT && !routeToBluetoothIfAvailable())) {
|
||||||
mLc.enableSpeaker(false);
|
mLc.enableSpeaker(false);
|
||||||
uninitBluetooth();
|
scoDisconnected();
|
||||||
} else {
|
} else {
|
||||||
Log.d("Routing audio to bluetooth headset");
|
Log.d("Routing audio to bluetooth headset");
|
||||||
routeToBluetoothEnabled = true;
|
routeToBluetoothEnabled = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mLc.enableSpeaker(true);
|
mLc.enableSpeaker(true);
|
||||||
uninitBluetooth();
|
scoDisconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (LinphoneOnAudioChangedListener listener : getSimpleListeners(LinphoneOnAudioChangedListener.class)) {
|
for (LinphoneOnAudioChangedListener listener : getSimpleListeners(LinphoneOnAudioChangedListener.class)) {
|
||||||
|
@ -243,27 +245,40 @@ public final class LinphoneManager implements LinphoneCoreListener {
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
public void startBluetooth() {
|
public void startBluetooth() {
|
||||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30) && mBluetoothAdapter.isEnabled()) {
|
if (mBluetoothAdapter.isEnabled()) {
|
||||||
mProfileListener = new BluetoothProfile.ServiceListener() {
|
if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30)) {
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
mProfileListener = new BluetoothProfile.ServiceListener() {
|
||||||
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
if (profile == BluetoothProfile.HEADSET) {
|
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
||||||
mBluetoothHeadset = (BluetoothHeadset) proxy;
|
if (profile == BluetoothProfile.HEADSET) {
|
||||||
Log.d("Bluetooth headset connected");
|
mBluetoothHeadset = (BluetoothHeadset) proxy;
|
||||||
routeToBluetoothIfAvailable();
|
Log.d("Bluetooth headset connected");
|
||||||
}
|
routeToBluetoothIfAvailable();
|
||||||
}
|
}
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
}
|
||||||
public void onServiceDisconnected(int profile) {
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
if (profile == BluetoothProfile.HEADSET) {
|
public void onServiceDisconnected(int profile) {
|
||||||
mBluetoothHeadset = null;
|
if (profile == BluetoothProfile.HEADSET) {
|
||||||
Log.d("Bluetooth headset disconnected");
|
mBluetoothHeadset = null;
|
||||||
routeAudioToReceiver();
|
Log.d("Bluetooth headset disconnected");
|
||||||
}
|
routeAudioToReceiver();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
mBluetoothAdapter.getProfileProxy(mServiceContext, mProfileListener, BluetoothProfile.HEADSET);
|
};
|
||||||
} else {
|
mBluetoothAdapter.getProfileProxy(mServiceContext, mProfileListener, BluetoothProfile.HEADSET);
|
||||||
|
} else {
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
String actionScoConnected = AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED;
|
||||||
|
Intent currentValue = mServiceContext.registerReceiver(bluetoothReiceiver, new IntentFilter(actionScoConnected));
|
||||||
|
int state = currentValue == null ? 0 : currentValue.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, 0);
|
||||||
|
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
|
||||||
|
isBluetoothScoConnected = true;
|
||||||
|
scoConnected();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
scoDisconnected();
|
||||||
routeAudioToReceiver();
|
routeAudioToReceiver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,30 +286,42 @@ public final class LinphoneManager implements LinphoneCoreListener {
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
public boolean routeToBluetoothIfAvailable() {
|
public boolean routeToBluetoothIfAvailable() {
|
||||||
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30) && mBluetoothAdapter.isEnabled() && mAudioManager.isBluetoothScoAvailableOffCall()) {
|
if (mBluetoothAdapter.isEnabled() && mAudioManager.isBluetoothScoAvailableOffCall()) {
|
||||||
mAudioManager.setBluetoothScoOn(true);
|
mAudioManager.setBluetoothScoOn(true);
|
||||||
mAudioManager.startBluetoothSco();
|
mAudioManager.startBluetoothSco();
|
||||||
|
|
||||||
boolean connected = false;
|
|
||||||
if (mBluetoothHeadset != null) {
|
|
||||||
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
|
|
||||||
for (final BluetoothDevice dev : devices) {
|
|
||||||
connected |= mBluetoothHeadset.getConnectionState(dev) == BluetoothHeadset.STATE_CONNECTED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!connected) {
|
if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30)) {
|
||||||
Log.d("No bluetooth device available");
|
boolean connected = false;
|
||||||
uninitBluetooth();
|
if (mBluetoothHeadset != null) {
|
||||||
|
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
|
||||||
|
for (final BluetoothDevice dev : devices) {
|
||||||
|
connected |= mBluetoothHeadset.getConnectionState(dev) == BluetoothHeadset.STATE_CONNECTED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!connected) {
|
||||||
|
Log.d("No bluetooth device available");
|
||||||
|
scoDisconnected();
|
||||||
|
}
|
||||||
|
return connected;
|
||||||
|
} else {
|
||||||
|
return isBluetoothScoConnected;
|
||||||
}
|
}
|
||||||
return connected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
public void scoConnected() {
|
||||||
public void uninitBluetooth() {
|
Log.e("Bluetooth sco connected!");
|
||||||
if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30) && mAudioManager != null) {
|
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||||
|
routeToBluetoothIfAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scoDisconnected() {
|
||||||
|
Log.e("Bluetooth sco disconnected!");
|
||||||
|
if (mAudioManager != null) {
|
||||||
|
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||||
mAudioManager.stopBluetoothSco();
|
mAudioManager.stopBluetoothSco();
|
||||||
mAudioManager.setBluetoothScoOn(false);
|
mAudioManager.setBluetoothScoOn(false);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue