Skip to content

Commit

Permalink
feat: stop bt headset on end of speech #210
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed Dec 14, 2021
1 parent 0aba8a2 commit 356b35f
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,29 +308,20 @@ public class SpeechToTextPlugin :
for (tryDevice in lpaired) {
//This loop tries to start VoiceRecognition mode on every paired device until it finds one that works(which will be the currently in use bluetooth headset)
if (lhead.startVoiceRecognition(tryDevice)) {
debugLog("Starting bluetooth voice recognition")
activeBluetooth = tryDevice;
break
}
}
}
}

private fun optionallyStopBluetooth() {
val lactive = activeBluetooth
val lbt = bluetoothHeadset
if (null != lactive && null != lbt ) {
lbt.stopVoiceRecognition(lactive)
activeBluetooth = null
}
}

private fun stopListening(result: Result) {
if (sdkVersionTooLow() || isNotInitialized() || isNotListening()) {
result.success(false)
return
}
debugLog("Stop listening")
optionallyStopBluetooth()
handler.post {
run {
speechRecognizer?.stopListening()
Expand Down Expand Up @@ -393,11 +384,22 @@ public class SpeechToTextPlugin :
else -> SpeechToTextStatus.done.name
}
debugLog("Notify status:" + doneStatus )
optionallyStopBluetooth();
channel?.invokeMethod(SpeechToTextCallbackMethods.notifyStatus.name,
doneStatus )
}
}

private fun optionallyStopBluetooth() {
val lactive = activeBluetooth
val lbt = bluetoothHeadset
if (null != lactive && null != lbt ) {
debugLog("Stopping bluetooth voice recognition")
lbt.stopVoiceRecognition(lactive)
activeBluetooth = null
}
}

private fun updateResults(speechBundle: Bundle?, isFinal: Boolean) {
if (isDuplicateFinal( isFinal )) {
debugLog("Discarding duplicate final")
Expand Down Expand Up @@ -507,11 +509,13 @@ public class SpeechToTextPlugin :
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = proxy as BluetoothHeadset
debugLog("Found a headset: " + bluetoothHeadset.toString())
}
}

override fun onServiceDisconnected(profile: Int) {
if (profile == BluetoothProfile.HEADSET) {
debugLog("Clearing headset: ")
bluetoothHeadset = null
}
}
Expand Down

0 comments on commit 356b35f

Please sign in to comment.