Skip to content

Commit

Permalink
feat: optional bluetooth for Android #296
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed Feb 2, 2022
1 parent 0e507a1 commit bc36fcb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions speech_to_text/android/src/main/AndroidManifest.xml
@@ -1,3 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csdcorp.speech_to_text">

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
</manifest>
Expand Up @@ -99,6 +99,7 @@ public class SpeechToTextPlugin :
private var debugLogging: Boolean = false
private var alwaysUseStop: Boolean = false
private var intentLookup: Boolean = false
private var noBluetooth: Boolean = false
private var resultSent: Boolean = false
private var speechRecognizer: SpeechRecognizer? = null
private var recognizerIntent: Intent? = null
Expand Down Expand Up @@ -188,6 +189,10 @@ public class SpeechToTextPlugin :
if (null != iOpt) {
intentLookup = iOpt == true
}
var noBtOpt = call.argument<Boolean>("noBluetooth")
if (null != noBtOpt) {
noBluetooth = noBtOpt == true
}
initialize(result)
}
"listen" -> {
Expand Down Expand Up @@ -301,6 +306,7 @@ public class SpeechToTextPlugin :
}

private fun optionallyStartBluetooth() {
if ( noBluetooth ) return
val lbt = bluetoothAdapter
val lpaired = pairedDevices
val lhead = bluetoothHeadset
Expand Down Expand Up @@ -391,6 +397,7 @@ public class SpeechToTextPlugin :
}

private fun optionallyStopBluetooth() {
if ( noBluetooth ) return
val lactive = activeBluetooth
val lbt = bluetoothHeadset
if (null != lactive && null != lbt ) {
Expand Down Expand Up @@ -454,8 +461,13 @@ public class SpeechToTextPlugin :
val localActivity = currentActivity
if (null != localActivity) {
debugLog("Requesting permission")
var requiredPermissions = arrayOf(Manifest.permission.RECORD_AUDIO)
if ( !noBluetooth ) {
requiredPermissions = requiredPermissions.plus(Manifest.permission.BLUETOOTH_CONNECT)
}

ActivityCompat.requestPermissions(localActivity,
arrayOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH_CONNECT), speechToTextPermissionCode)
requiredPermissions, speechToTextPermissionCode)
} else {
debugLog("no permission, no activity, completing")
completeInitialize()
Expand Down Expand Up @@ -502,6 +514,7 @@ public class SpeechToTextPlugin :
}

private fun setupBluetooth() {
if ( noBluetooth ) return
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
pairedDevices = bluetoothAdapter?.getBondedDevices()

Expand Down
21 changes: 21 additions & 0 deletions speech_to_text/lib/speech_to_text.dart
Expand Up @@ -114,15 +114,36 @@ class SpeechToText {
SpeechConfigOption('android', 'alwaysUseStop', true);
static final SpeechConfigOption androidIntentLookup =
SpeechConfigOption('android', 'intentLookup', true);
static final SpeechConfigOption androidNoBluetooth =
SpeechConfigOption('android', 'noBluetooth', true);
static final SpeechConfigOption iosNoBluetooth =
SpeechConfigOption('ios', 'noBluetooth', true);

static final SpeechToText _instance = SpeechToText.withMethodChannel();
bool _initWorked = false;

/// True when any words have been recognized during the current listen session.
bool _recognized = false;

/// True as soon as the platform reports it has started listening which
/// happens some time after the listen method is called.
bool _listening = false;
bool _cancelOnError = false;

/// True if the user has requested to cancel recognition when a permanent
/// error occurs.
bool _partialResults = false;

/// True when the results callback has already been called with a
/// final result.
bool _notifiedFinal = false;

/// True when the internal status callback has been called with the
/// done status. Note that this does not mean the user callback has
/// been called since that is only called after the final result has been
/// seen.
bool _notifiedDone = false;

int _listenStartedAt = 0;
int _lastSpeechEventAt = 0;
Duration? _pauseFor;
Expand Down

0 comments on commit bc36fcb

Please sign in to comment.