Final touch. Reconnecting works almost perfect...

This commit is contained in:
Jimmie Matsson 2018-06-05 17:17:11 +02:00
parent abd19de59c
commit 45e4d8ec05
8 changed files with 345 additions and 394 deletions

6
.idea/gradle.xml generated
View file

@ -7,9 +7,9 @@
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$/../../dixitmobile-common/library" />
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/vxt-engine" />
<option value="/data3/AndroidStudioProjects/dixit/dixitmobile-common/library" />
<option value="/data3/AndroidStudioProjects/dixit/hubone/voiXtreme-engine" />
<option value="/data3/AndroidStudioProjects/dixit/hubone/voiXtreme-engine/vxt-engine" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />

View file

@ -1,7 +1,3 @@
include ':vxt-engine'
include ':library'
if (file('../../dixitmobile-common/library').exists()) {
project(':library').projectDir = new File('../../dixitmobile-common/library')
} else {
project(':library').projectDir = new File('../dixitmobile-common/library')
}
project(':library').projectDir = new File('../../dixitmobile-common/library')

View file

@ -62,6 +62,10 @@ public class AudioSystem {
public AudioSystem(AudioManager audioManager) {
if (true) {
return;
}
try {
mAudioSystem = Class.forName("android.media.AudioSystem");
Log.d(TAG, "AudioSystem constructor 1");
@ -202,6 +206,11 @@ public class AudioSystem {
*/
public void routeSoundWiredHeadset() {
if (true) {
return;
}
Log.v(TAG, "routeSoundWiredHeadset(): " + Build.MODEL);
if (mAudioSystem == null) {
@ -245,6 +254,11 @@ public class AudioSystem {
*/
public void routeSoundSpeakers() {
if (true) {
return;
}
Log.v(TAG, "routeSoundSpeakers(): " + Build.MODEL);
if (mAudioSystem == null) {
@ -307,6 +321,10 @@ public class AudioSystem {
*/
public void routeSoundBluetooth() {
if (true) {
return;
}
Log.v(TAG, "routeSoundBluetooth(): " + Build.MODEL);
Log.i(TAG, "eml: input type send error #16");
@ -328,7 +346,8 @@ public class AudioSystem {
break;
default:
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
// mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(false);
Log.i(TAG, "eml: input type send error #19");
@ -355,6 +374,11 @@ public class AudioSystem {
*/
public void resetToDefaultValues() {
if (true) {
return;
}
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mAudioManager.setSpeakerphoneOn(false);

View file

@ -9,6 +9,7 @@ import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.dixitmobile.common.BroadcastParams;
import com.voixtreme.vocalengine.service.VoiXtremeService;
@ -17,22 +18,29 @@ import com.voixtreme.vxtenginejni.VxtEngineApi;
public class BluetoothSCOManager extends BroadcastReceiver {
private static final String TAG = BluetoothSCOManager.class.getSimpleName();
private static final String BROADCAST_HEADSET_RESPONSE = "voixtreme.vocal.engine.headset.response";
private final AudioManager mAudioManager;
private final AudioSystem mAudioSystem;
private final VoiXtremeService mService;
private final VxtEngineApi mVxtEngineApi;
private final BluetoothAdapter mBTAdapter;
private final Handler mHandler;
private SCOTester mSCOTester;
private SCOStatusInterface mSCOStatusCallback;
private Thread mReconnectSCO;
private Runnable mTerminateRunnable;
private String mDisconnectedMAC; // This device was disconnected
private String mConnectedMAC; // We need to re-connect to this devices
private String mRespondPackage;
private boolean mConnectedSCO;
private boolean mStarted;
private boolean mPaused;
private boolean mResuming;
private boolean mInformConnected;
public interface SCOStatusInterface {
@ -47,12 +55,17 @@ public class BluetoothSCOManager extends BroadcastReceiver {
mAudioSystem = new AudioSystem(mAudioManager);
mVxtEngineApi = engineApi;
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
mHandler = new Handler(Looper.getMainLooper());
mTerminateRunnable = null;
mSCOStatusCallback = null;
mConnectedSCO = false;
mSCOTester = null;
mStarted = false;
mInformConnected = false;
mPaused = false;
mResuming = false;
mRespondPackage = "";
mConnectedMAC = null;
mDisconnectedMAC = null;
mReconnectSCO = null;
@ -95,6 +108,10 @@ public class BluetoothSCOManager extends BroadcastReceiver {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
btStateChanged(state);
}
case BROADCAST_HEADSET_RESPONSE: {
headsetConnectFailed();
}
break;
}
}
@ -102,15 +119,26 @@ public class BluetoothSCOManager extends BroadcastReceiver {
/**
* Remove any active SCO connection.
*
* @param respondPackage The package name to send confirmation info when resumed
*/
public void pause() {
public void pause(String respondPackage) {
if (!mStarted) {
Log.i(TAG, "eml: pause(): " + mStarted + " / " + mPaused);
if (!mStarted || mPaused) {
return;
}
mRespondPackage = (respondPackage == null ? "" : respondPackage);
mVxtEngineApi.nativecommand("<RST>", "313");
stop();
mStarted = true;
mPaused = true;
Log.i(TAG, "eml: pause()");
mService.sendIntentAsrStateChanges(BroadcastParams.ASR_STATE_NO_HEADSET);
mService.setAllowAsrChanges(false);
disconnectHeadset();
}
/**
@ -118,13 +146,29 @@ public class BluetoothSCOManager extends BroadcastReceiver {
*/
public void resume() {
if (!mStarted) {
Log.i(TAG, "eml: resume(): " + mStarted + " / " + mPaused);
if (!(mStarted && mPaused)) {
return;
}
mInformConnected = true;
mPaused = false;
registerReceivers();
mAudioManager.setBluetoothScoOn(true);
connectHeadset("");
mResuming = true;
Log.i(TAG, "eml: resume()");
mTerminateRunnable = new Runnable() {
@Override
public void run() {
stop();
Log.e(TAG, "eml: forceEngineReset: 1: " + mRespondPackage);
forceEngineReset(mRespondPackage);
}
};
mHandler.postDelayed(mTerminateRunnable, 17111);
}
/**
@ -141,7 +185,6 @@ public class BluetoothSCOManager extends BroadcastReceiver {
mStarted = true;
mAudioSystem.routeSoundBluetooth();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.startBluetoothSco();
registerReceivers();
@ -154,10 +197,13 @@ public class BluetoothSCOManager extends BroadcastReceiver {
Log.i(TAG, "eml: stop()");
if (mStarted) {
if (mStarted && !mPaused) {
unregisterReceivers();
}
mPaused = false;
mStarted = false;
if (mReconnectSCO != null) {
mReconnectSCO.interrupt();
mReconnectSCO = null;
@ -165,9 +211,26 @@ public class BluetoothSCOManager extends BroadcastReceiver {
mAudioManager.setBluetoothScoOn(false);
mAudioManager.stopBluetoothSco();
}
mPaused = false;
mStarted = false;
/**
* Are we currently paused?
*/
public boolean isPaused() {
return mPaused;
}
/**
* BT Pairing didn't found a headset to connect to, reset all and force a new calibrate
*/
private void headsetConnectFailed() {
if (BluetoothAdapter.getDefaultAdapter().isEnabled() && !TextUtils.isEmpty(mRespondPackage)) {
stop();
Log.e(TAG, "eml: forceEngineReset: 2: " + mRespondPackage);
forceEngineReset(mRespondPackage);
}
}
private void handleSCOBroadcast(int state, int oldState) {
@ -176,21 +239,21 @@ public class BluetoothSCOManager extends BroadcastReceiver {
switch (state) {
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
Log.i(TAG, "BluetoothSCO - DISconnected: " + state);
Log.i(TAG, "eml: BluetoothSCO - DISconnected: " + state);
scoDisconnected();
break;
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
Log.i(TAG, "BluetoothSCO - connected: " + state);
Log.i(TAG, "eml: BluetoothSCO - connected: " + state);
scoConnected();
break;
case AudioManager.SCO_AUDIO_STATE_CONNECTING:
Log.i(TAG, "BluetoothSCO - connecting: " + state);
Log.i(TAG, "eml: BluetoothSCO - connecting: " + state);
break;
case AudioManager.SCO_AUDIO_STATE_ERROR:
Log.i(TAG, "BluetoothSCO - ERROR: " + state);
Log.i(TAG, "eml: BluetoothSCO - ERROR: " + state);
break;
}
}
@ -212,6 +275,16 @@ public class BluetoothSCOManager extends BroadcastReceiver {
if (mDisconnectedMAC != null) {
mConnectedMAC = mDisconnectedMAC;
mDisconnectedMAC = null;
if (mReconnectSCO != null) {
mReconnectSCO.interrupt();
try {
Thread.sleep(713);
} catch (Exception e) {
// empty by design
}
}
mReconnectSCO = new Thread(new SCOReconnect(true));
mReconnectSCO.start();
}
if (mSCOStatusCallback != null) {
@ -238,12 +311,23 @@ public class BluetoothSCOManager extends BroadcastReceiver {
mReconnectSCO = null;
}
if (mTerminateRunnable != null) {
mHandler.removeCallbacks(mTerminateRunnable);
mTerminateRunnable = null;
}
mConnectedSCO = true;
if (!mStarted || mPaused) {
return;
}
if (mInformConnected && !mRespondPackage.isEmpty()) {
informSCOStatus(true, mRespondPackage);
mRespondPackage = "";
}
mInformConnected = false;
mService.setAllowAsrChanges(true);
mService.sendIntentAsrStateChanges(BroadcastParams.ASR_STATE_NOT_SPEAKING);
@ -263,58 +347,87 @@ public class BluetoothSCOManager extends BroadcastReceiver {
private class SCOReconnect implements Runnable {
private int counter;
private boolean disconnected;
/**
*
* @param disconnected true if we received a ACTION_ACL_DISCONNECTED (different way of check)
*/
SCOReconnect(boolean disconnected) {
this.disconnected = disconnected;
}
@SuppressWarnings("ConstantConditions")
@Override
public void run() {
counter = 0;
while (!mConnectedSCO) {
Log.i(TAG, "eml: mAudioManager.startBluetoothSco() loop");
mAudioManager.startBluetoothSco();
while (!mConnectedSCO && mStarted && !mPaused) {
if (disconnected) {
Log.i(TAG, "eml: connectHeadset() loop");
connectHeadset("");
try {
Thread.sleep(1013);
} catch (InterruptedException e) {
break;
}
if (counter % 3 == 0) {
mAudioManager.stopBluetoothSco();
try {
Thread.sleep(313);
Thread.sleep(13513);
} catch (InterruptedException e) {
break;
}
}
else if (counter % 10 == 0) {
mReconnectSCO = null;
if (mBTAdapter.getBondedDevices().isEmpty()) {
mReconnectSCO = null;
break;
} else {
disconnectHeadset();
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
connectHeadset("");
}
}, 713);
}
} else {
Log.i(TAG, "eml: mAudioManager.startBluetoothSco() loop");
mAudioManager.startBluetoothSco();
try {
Thread.sleep(1513);
Thread.sleep(1013);
} catch (InterruptedException e) {
break;
}
}
counter++;
if (counter % 3 == 0) {
mAudioManager.stopBluetoothSco();
try {
Thread.sleep(313);
} catch (InterruptedException e) {
break;
}
}
else if (counter % 13 == 0) {
if (mReconnectSCO != null) {
mReconnectSCO.interrupt();
mReconnectSCO = null;
}
if (mBTAdapter.getBondedDevices().isEmpty()) {
break;
} else {
reConnectHeadset();
}
try {
Thread.sleep(2513);
} catch (InterruptedException e) {
break;
}
}
counter++;
}
}
}
private void reConnectHeadset() {
disconnectHeadset();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
connectHeadset("");
}
}, 713);
}
}
/**
@ -330,15 +443,24 @@ public class BluetoothSCOManager extends BroadcastReceiver {
Log.i(TAG, "eml: mPaused : " + mPaused);
Log.i(TAG, "eml: MACs : " + mConnectedMAC + " <--> " + btDevice.getAddress());
if ((mStarted || mPaused) && mConnectedMAC != null && mConnectedMAC.equals(btDevice.getAddress())) {
if (mResuming
|| ((mStarted || mPaused) && mConnectedMAC != null && mConnectedMAC.equals(btDevice.getAddress()))) {
Log.i(TAG, "eml: request connect to headset !!! ");
connectHeadset(mConnectedMAC);
// Log.i(TAG, "eml: request connect to headset !!! ");
// connectHeadset(mConnectedMAC);
Log.i(TAG, "eml: resuming: " + mResuming);
mResuming = false;
if (mReconnectSCO != null) {
mReconnectSCO.interrupt();
try {
Thread.sleep(313);
} catch (Exception e) {
// empty by design
}
}
mReconnectSCO = new Thread(new SCOReconnect());
mReconnectSCO = new Thread(new SCOReconnect(false));
mReconnectSCO.start();
}
@ -366,6 +488,29 @@ public class BluetoothSCOManager extends BroadcastReceiver {
mConnectedMAC = btDevice.getAddress();
mDisconnectedMAC = null;
}
stop();
if (mReconnectSCO != null) {
mReconnectSCO.interrupt();
try {
Thread.sleep(713);
} catch (Exception e) {
// empty by design
}
} else {
try {
Thread.sleep(1713);
} catch (Exception e) {
// empty by design
}
}
start();
// mReconnectSCO = new Thread(new SCOReconnect(true));
// mReconnectSCO.start();
} else {
Log.w(TAG, "eml: NOT started, ignoring device");
mDisconnectedMAC = null;
@ -373,6 +518,35 @@ public class BluetoothSCOManager extends BroadcastReceiver {
}
}
/**
* Send information letting listening app know SCO was just (re)connected
*/
public void informSCOStatus(boolean connected, String respondPackage) {
Intent intent = new Intent(respondPackage);
intent.putExtra(BroadcastParams.EXTRAS_ACTION, BroadcastParams.COMMAND_BLUETOOTH_RESUMED);
intent.putExtra(BroadcastParams.EXTRAS_DATA, connected);
// This parameter is only necessary for TellNext Emulator
intent.putExtra(BroadcastParams.EXTRAS_RESPONSE_TYPE, "calibrate");
mService.sendBroadcast(intent);
}
/**
* Headset was not connected, force a close and reset by calling app
*/
public void forceEngineReset(String respondPackage) {
Intent intent = new Intent(respondPackage);
intent.putExtra(BroadcastParams.EXTRAS_ACTION, BroadcastParams.COMMAND_SERVICE_CONFIGURED);
intent.putExtra(BroadcastParams.EXTRAS_DATA, false);
intent.putExtra(BroadcastParams.EXTRAS_STATUS, false);
// This parameter is only necessary for TellNext Emulator
intent.putExtra(BroadcastParams.EXTRAS_RESPONSE_TYPE, "calibrate");
mService.sendBroadcast(intent);
}
/**
* Disconnect (not unpair) any headset connected by Bluetooth
*/
@ -403,10 +577,12 @@ public class BluetoothSCOManager extends BroadcastReceiver {
*/
private void registerReceivers() {
IntentFilter filter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
IntentFilter filter = new IntentFilter();
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BROADCAST_HEADSET_RESPONSE);
mService.registerReceiver(this, filter);
}
@ -457,6 +633,7 @@ public class BluetoothSCOManager extends BroadcastReceiver {
if (mConnectedSCO) {
mSCOStatusCallback.connected();
} else {
BluetoothSCOManager.this.stop();
mSCOStatusCallback.disconnected();
}
@ -482,19 +659,31 @@ public class BluetoothSCOManager extends BroadcastReceiver {
switch (state) {
case BluetoothAdapter.STATE_ON:
Log.w(TAG, "BluetoothAdapter.STATE_ON");
Log.w(TAG, "eml: BluetoothAdapter.STATE_ON");
if (mStarted && !mPaused) {
mService.sendIntentAsrStateChanges(BroadcastParams.ASR_STATE_NO_HEADSET, true);
mService.setAllowAsrChanges(false);
mReconnectSCO = new Thread(new SCOReconnect());
if (mReconnectSCO != null) {
mReconnectSCO.interrupt();
}
connectHeadset("");
try {
Thread.sleep(313);
} catch (InterruptedException e) {
// empty by design
}
mReconnectSCO = new Thread(new SCOReconnect(false));
mReconnectSCO.start();
}
break;
case BluetoothAdapter.STATE_OFF:
Log.w(TAG, "BluetoothAdapter.STATE_OFF");
Log.w(TAG, "eml: BluetoothAdapter.STATE_OFF");
if (mStarted && !mPaused) {
mService.sendIntentAsrStateChanges(BroadcastParams.ASR_STATE_NO_BLUETOOTH, true);

View file

@ -1,204 +0,0 @@
package com.voixtreme.vocalengine.receivers;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;
import com.voixtreme.vocalengine.events.UIEventBluetoothSCOStatus;
import com.voixtreme.vocalengine.service.VoiXtremeService;
import org.greenrobot.eventbus.EventBus;
import java.lang.reflect.Method;
/**
* Created by jima and CZor 17-Aug-16.
*/
public class BluetoothSCOReceiver extends BroadcastReceiver {
private static final String TAG = "BluetoothSCOReceiver";
public static final int MAX_RETRIES = 10;
private VoiXtremeService mService;
private boolean mIgnoreNotifications;
private BluetoothAdapter mBTAdapter;
private boolean mTryAgain;
private int mRetryCount;
private int mMaxRetries;
private int mCurrentScoState;
public BluetoothSCOReceiver(VoiXtremeService service) {
Log.e(TAG, "new BluetoothSCOReceiver()");
mService = service;
mIgnoreNotifications = true;
mTryAgain = false;
mRetryCount = 0;
mMaxRetries = MAX_RETRIES;
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
mCurrentScoState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED;
}
/**
* Set to false when using Bluetooth as input
*/
public void setIgnoreNotifications(boolean ignore) {
Log.e(TAG, "********************************");
Log.v(TAG, "ignore: " + ignore);
Log.e(TAG, "********************************");
mIgnoreNotifications = ignore;
}
public void setTryAgain(boolean tryAgain) {
mTryAgain = tryAgain;
mRetryCount = 0;
}
public void setTryAgain(int maxRetries) {
mTryAgain = true;
mRetryCount = 0;
mMaxRetries = maxRetries;
}
// Methods
@Override
public void onReceive(final Context context, Intent intent) {
Log.w(TAG, "onReceive(): " + mService + " / " + mIgnoreNotifications);
if (mService == null || mIgnoreNotifications) {
Log.e(TAG, "==========================");
Log.e(TAG, "IGNORING BT: " + mIgnoreNotifications + " / " + mService.toString());
Log.e(TAG, "==========================");
return;
}
if (intent.getAction().equals(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
int oldState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, -1);
Log.i(TAG, "BluetoothSCO - new/old State: " + state + " / " + oldState);
EventBus.getDefault().post(new UIEventBluetoothSCOStatus(state));
mCurrentScoState = state;
switch (state) {
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
Log.i(TAG, "BluetoothSCO - DISconnected: " + state);
Log.e(TAG, "setBluetoothAsrErrorState(true);");
// mService.setBluetoothAsrErrorState(true);
if (mTryAgain && mBTAdapter.isEnabled()) {
mTryAgain = false;
if (mRetryCount++ < mMaxRetries) {
Log.v(TAG, "trying to connect again to BTSco(): " + mRetryCount + " / " + mMaxRetries);
mService.newHandler().postDelayed(new Runnable() {
@Override
public void run() {
switch (Build.MODEL) {
case VoiXtremeService.MODEL_ZEBRA_WT6000:
case VoiXtremeService.MODEL_ZEBRA_TC8000:
mService.getAudioManager().startBluetoothSco();
break;
default:
try {
AudioManager audioManager = mService.getAudioManager();
Method method = audioManager.getClass().getMethod("startBluetoothScoVirtualCall", (Class[]) null);
method.invoke(audioManager, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}, 500);
} else {
Log.e(TAG, "ending retries... failure...");
}
}
break;
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
Log.i(TAG, "BluetoothSCO - connected: " + state);
mTryAgain = false;
mRetryCount = 0;
Log.e(TAG, "setBluetoothAsrErrorState(false);");
// mService.setBluetoothAsrErrorState(false);
break;
case AudioManager.SCO_AUDIO_STATE_CONNECTING:
Log.i(TAG, "BluetoothSCO - connecting: " + state);
mTryAgain = true;
break;
case AudioManager.SCO_AUDIO_STATE_ERROR:
Log.i(TAG, "BluetoothSCO - ERROR: " + state);
break;
}
} else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
// BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.v(TAG, "BluetoothDevice.ACTION_ACL_CONNECTED");
/*
* TODO: If we are currently using BT and Sco connection state isn't CONNECTED we assume the
* device connected was a headset. We need to add a better check for this.
*/
if (mService.isUsingBluetooth() && mCurrentScoState != AudioManager.SCO_AUDIO_STATE_CONNECTED) {
mIgnoreNotifications = false;
// mService.setBluetoothAsrErrorState(false);
mService.getAudioManager().setMode(AudioManager.MODE_IN_COMMUNICATION);
switch (Build.MODEL) {
case VoiXtremeService.MODEL_ZEBRA_WT6000:
case VoiXtremeService.MODEL_ZEBRA_TC8000:
mService.getAudioManager().startBluetoothSco();
break;
default:
try {
AudioManager audioManager = mService.getAudioManager();
Method method = audioManager.getClass().getMethod("startBluetoothScoVirtualCall", (Class[]) null);
method.invoke(audioManager, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
} else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.v(TAG, "BluetoothDevice.ACTION_ACL_DISCONNECTED");
} else if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)) {
case BluetoothAdapter.STATE_ON:
Log.e(TAG, "BluetoothAdapter.STATE_ON");
break;
case BluetoothAdapter.STATE_OFF:
Log.e(TAG, "BluetoothAdapter.STATE_OFF");
mService.getAudioManager().stopBluetoothSco();
break;
}
}
}
}

View file

@ -44,12 +44,19 @@ public class VoiXtremeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (intent == null || intent.getAction() == null) {
return;
}
if ( action.equals(BroadcastParams.INTENT_ACTION_VOIXTREME)) // Received from Emulator
{
if (intent.getExtras() == null) {
Log.e(TAG, "eml: no EXTRA found");
return;
}
// Received from Emulator
if (intent.getAction().equals(BroadcastParams.INTENT_ACTION_VOIXTREME)) {
if (commandManager == null) {
// TODO: exception
Log.e(TAG, "eml: commandManager not initialized");
return;
}
@ -61,8 +68,9 @@ public class VoiXtremeReceiver extends BroadcastReceiver {
if (packageName == null || !packageName.equals("dummy")) {
pkgManager.setIntentPackage( packageName);
}
asrStateManager.setIntentPackage( packageName);
Log.i(TAG, "Intent PACKAGE =" + pkgManager.intentPackage);
Log.i(TAG, "Intent PACKAGE: " + pkgManager.intentPackage);
}
// Following received command IN INTENT
@ -172,6 +180,16 @@ public class VoiXtremeReceiver extends BroadcastReceiver {
Log.i(TAG, "Intent COMMAND_PING");
break;
case BroadcastParams.COMMAND_PAUSE_ENGINE:
Log.i(TAG, "Intent COMMAND_PAUSE_ENGINE");
mHandler.sendMessage( Message.obtain( mHandler, command));
break;
case BroadcastParams.COMMAND_RESUME_ENGINE:
Log.i(TAG, "Intent COMMAND_RESUME_ENGINE");
mHandler.sendMessage( Message.obtain( mHandler, command));
break;
default:
// TODO: exception
Log.e(TAG, "Intent ERROR command=" + command);

View file

@ -66,7 +66,7 @@ public class VoiXtremeService extends Service
// Engines ASR and TTS -----------------------------------------
// The VoiXtreme API
private VxtEngineApi mVxtEngineApi= null;
private VxtEngineApi mVxtEngineApi = null;
// Tts Vocalizer Engine: the text to speech engine.
private VocalizerEngine ttsEngine = null;
@ -111,7 +111,6 @@ public class VoiXtremeService extends Service
private AudioManager mAudioManager;
private AudioSystem mAudioSystem;
private boolean mIsHeadsetConnected;
private boolean mStopBluetoothSco;
private boolean mAllowAsrChanges;
private BluetoothSCOManager mSCOManager;
@ -180,27 +179,18 @@ public class VoiXtremeService extends Service
mInputType = BroadcastParams.INPUT_TYPE_OPEN;
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioSystem = new AudioSystem(mAudioManager);
mStopBluetoothSco = false;
mAllowAsrChanges = true;
mIsHeadsetConnected = mAudioManager.isWiredHeadsetOn();
mSCOManager = new BluetoothSCOManager(this, mVxtEngineApi);
// Register our Bluetooth receiver
// mBluetoothSCOReceiver = new BluetoothSCOReceiver(this);
// IntentFilter filter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
// filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
// filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
// filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
// registerReceiver(mBluetoothSCOReceiver, filter);
// The initial TTS CURRENT SAY values
mSay.mSayTtsId= 0;
mSay.mSayAsrMode= 0;
mSay.mSayTtsMode= 0;
mSay.mSayPhrase= "";
mSay.mSayTtsId = 0;
mSay.mSayAsrMode = 0;
mSay.mSayTtsMode = 0;
mSay.mSayPhrase = "";
// The VoiXtreme engine
mVxtEngineApi = new VxtEngineApi(this);
mSCOManager = new BluetoothSCOManager(this, mVxtEngineApi);
mVxtPkgManager = new VxtPkgManager();
@ -229,17 +219,21 @@ public class VoiXtremeService extends Service
registerReceiver(mVoiXtremeReceiver, filter);
}
//=========================================================================
// Service Overrides / start command
/**
* Make sure the service is created with foreground privileges
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
super.onStartCommand(intent, flags, startId);
startForeground(313, buildForegroundNotification());
return START_STICKY; // We want to live forever
}
/**
* Create our foreground Notification object necessary
*/
private Notification buildForegroundNotification() {
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
@ -252,8 +246,9 @@ public class VoiXtremeService extends Service
return(b.build());
}
//=========================================================================
// Service Overrides / destroy service
/**
* Stop service
*/
@Override
public void onDestroy() {
@ -290,13 +285,15 @@ public class VoiXtremeService extends Service
// Voixtreme callbacks to implement tts features
//=========================================================================
//=========================================================================
// This method is called from Vxt to set the loaded ASR language
/**
* This method is called from Vxt to set the loaded ASR language
*
* @param asrLanguageConfigured Configured language
*/
public void ttsEngineSetLanguage(String asrLanguageConfigured) {
Log.i( TAG, "ttsEngineSetLanguage LanguageConfigured=" + asrLanguageConfigured );
mAsrLanguageConfigured= asrLanguageConfigured;
mAsrLanguageConfigured = asrLanguageConfigured;
}
//=========================================================================
@ -1098,6 +1095,14 @@ public class VoiXtremeService extends Service
}
break;
case BroadcastParams.COMMAND_PAUSE_ENGINE:
mSCOManager.pause(mVxtPkgManager.intentPackage);
break;
case BroadcastParams.COMMAND_RESUME_ENGINE:
mSCOManager.resume();
break;
// (From native) Make it run the TTS
case 99:
Log.i(TAG, "update COMMAND_TTS Phrase=" + mVxtTtsManager.sayPhrase );
@ -1189,7 +1194,6 @@ public class VoiXtremeService extends Service
case BroadcastParams.INPUT_TYPE_NFC:
Log.i(TAG, "eml: input type send error #4");
mStopBluetoothSco = false;
setHeadsetErrorState(false);
mInputType = BroadcastParams.INPUT_TYPE_NFC;
@ -1198,18 +1202,15 @@ public class VoiXtremeService extends Service
Log.v(TAG, "sendInputConnectionStatus6 - false");
sendInputConnectionStatus(false);
} else {
enableInputTypeBluetooth();
Log.i(TAG, "eml: input type send error #5.1");
mSCOManager.testSCOStatus(mSCOTestCallback);
}
break;
case BroadcastParams.INPUT_TYPE_BLUETOOTH:
Log.i(TAG, "eml: input type send error #5");
// mStopBluetoothSco = false;
// mBluetoothSCOReceiver.setTryAgain(3);
// setHeadsetErrorState(false);
// setBluetoothAsrErrorState(false);
mInputType = BroadcastParams.INPUT_TYPE_BLUETOOTH;
Log.i(TAG, "eml: input type send error #6");
setHeadsetErrorState(false);
if (!BluetoothSCOManager.isBluetoothEnabled()) {
Log.i(TAG, "eml: input type send error #7");
@ -1269,55 +1270,11 @@ public class VoiXtremeService extends Service
Intent intentOut = new Intent( mVxtPkgManager.intentPackage);
intentOut.putExtra(BroadcastParams.EXTRAS_ACTION, BroadcastParams.COMMAND_SERVICE_CONFIGURED);
intentOut.putExtra(BroadcastParams.EXTRAS_DATA, mIsConfigured);
intentOut.putExtra(BroadcastParams.EXTRAS_STATUS, mSCOManager.isPaused());
sendBroadcast(intentOut);
}
}
/**
* Capture input from bluetooth headset by enabling BluetoothSco
*/
public void enableInputTypeBluetooth() {
Log.i(TAG, "eml: input type send error #9");
switch (Build.MODEL) {
case MODEL_ZEBRA_WT6000:
case MODEL_ZEBRA_TC8000:
Log.i(TAG, "eml: input type send error #10");
// Log.e(TAG, "ee: startBluetoothSco() 4");
// mAudioManager.startBluetoothSco();
// mBluetoothSCOReceiver.setTryAgain(true);
break;
default:
Log.i(TAG, "eml: input type send error #11");
// try {
// AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Method method = audioManager.getClass().getMethod("startBluetoothScoVirtualCall", (Class[]) null);
// method.invoke(audioManager, (Object[]) null);
// } catch (Exception e) {
// e.printStackTrace();
// }
break;
}
mAudioSystem.routeSoundBluetooth();
if (!mStopBluetoothSco) {
Log.i(TAG, "eml: input type send error #12");
if (!mAudioManager.isBluetoothScoOn()) {
Log.i(TAG, "eml: input type send error #3");
mAudioManager.setBluetoothScoOn(true);
}
mAudioManager.startBluetoothSco();
Log.i(TAG, "eml: input type send error #14");
Log.e(TAG, "==================");
Log.e(TAG, "starting SCO");
Log.e(TAG, "==================");
mStopBluetoothSco = true;
}
}
/**
* OPEN, WIRED or BLUETOOTH
*
@ -1328,14 +1285,6 @@ public class VoiXtremeService extends Service
return mInputType;
}
/**
* Get the Android AudioManager service
*/
public AudioManager getAudioManager() {
return mAudioManager;
}
/**
* Get the our own AudioSystem manager
*/
@ -1389,11 +1338,6 @@ public class VoiXtremeService extends Service
mIsHeadsetConnected = headsetConnected;
}
public Handler newHandler() {
return new Handler();
}
// CZ add log from TTs engine
public void nativeLog(int type, String tag, String message) {

View file

@ -1,33 +1,23 @@
package com.voixtreme.vocalengine.service;
//=============================================================================
import com.dixitmobile.common.BroadcastParams;
import static com.voixtreme.vocalengine.service.VxtPkgManager.BROADCAST_SEND_INTENT_COMMAND;
/**
* Created by c_zorrilla on 25/11/2016.
* This class holds the ASR engine state
*/
public class VxtAsrStateManager
{
// The command is always COMMAND_STATE
public static final int command = BroadcastParams.COMMAND_STATE;
public class VxtAsrStateManager {
// The package to send the response
public String intentPackage= BROADCAST_SEND_INTENT_COMMAND;
public String intentPackage = "void";
// 0=NOL No Listen-red, 1=NOS (NO Speech), 2=BOS (Begin Of Speech), EOR=End Of Recognition-green.
public int mState=0;
private int mState=0;
// The energy of input sound 0-100
public int mEnergy=0;
// Id if this sequence (counter)
public int mId=0;
private int mId=0;
public boolean mRegisteredForState= false;
@ -35,33 +25,27 @@ public class VxtAsrStateManager
//=========================================================================
// to emulator or calibrate state
public void setState( int state)
{
public void setState( int state) {
mState= state;
++mId;
}
//=========================================================================
// Set the registration state from intent intent.register
public void setRegisterState(int register)
{
if( (register== 1) || (register== 3) )
mRegisteredForState= true;
else
mRegisteredForState= false;
if( (register== 2) || (register== 3) )
mRegisteredEnergy= true;
else
mRegisteredEnergy= false;
public void setRegisterState(int register) {
mRegisteredEnergy = register == 2 || register == 3;
mRegisteredForState = register == 1 || register == 3;
}
//=========================================================================
// For package
public void setIntentPackage( String intentPackage_)
{
/**
* Package to respond to when sending broadcasts
*
* @param intentPackage_ Interested package
*/
public void setIntentPackage( String intentPackage_) {
this.intentPackage= intentPackage_;
}
}