Skip to main content

Voice Service Integration

 Objective

Integrate voice commands in your Android applications using the voice service of the Almer Arc.

  Interaction between your App and Service

The application and the service will communicate using broadcasts. In the following examples you will find the functionalities of the voice service alongside code samples to fully acknowledge how to integrate the voice commands in your app.

1. Registering voice commands

Voice commands can be registered by sending a list to the Voice Service using an intent:

companion object {
// Intention to add new voice commands
private const val ACTION_OVERRIDE = "com.almer.voiceassistant.action.OVERRIDE_COMMANDS"

// List of commands to be added
private const val EXTRA_COMMANDS = "com.almer.voiceassistant.extra.COMMANDS"

// Package name of the app that sends the voice commands
private const val EXTRA_SOURCE = "com.almer.voiceassistant.extra.SOURCE_PACKAGE"
}

private fun sendCommands() {
val voiceCommands = arrayOf(
"Turn Camera on",
"Turn Camera off"
)

val intent = Intent(ACTION_OVERRIDE)
intent.putExtra(EXTRA_SOURCE, packageName)
intent.putExtra(EXTRA_COMMANDS, voiceCommands)

sendBroadcast(intent)
}

2. Reacting to voice command triggers

  • Applications can listen for when the speech recognizer has detected a spoken voice command (e.g "Enter text <...>") by using a BroadcastReceiver:
companion object {
// Action for the broadcast receiver intent filter: voice service detected a command
private const val ACTION_SPEECH_EVENT = "com.almer.voiceassistant.action.SPEECH_EVENT"

// The detected voice command
private const val EXTRA_COMMAND = "com.almer.voiceassistant.extra.COMMAND"
}

private val speechBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == ACTION_SPEECH_EVENT) {
val command = intent.getStringExtra(EXTRA_COMMAND)
}
}
}

3. Restore normal services

When your application has finished with the speech recogniser it should send a message to restore default behaviour, using the RESTORE_COMMANDS intent.

companion object {
// Resets the voice service to default commands
private const val ACTION_RESTORE = "com.almer.voiceassistant.action.RESTORE_COMMANDS"
}

private fun restoreVoiceCommands() {
val intent = Intent(ACTION_RESTORE)
sendBroadcast(intent)
}

4. API

IntentDescription
com.almer.voiceassistant.action.OVERRIDE_COMMANDSUsed to take control of the speech recogniser
com.almer.voiceassistant.action.RESTORE_COMMANDSUsed to reset the speech recogniser to its default behavior
com.almer.voiceassistant.action.SPEECH_EVENTSent from the Voice Service to identify spoken voice commands
ExtraData TypeDescription
com.almer.voiceassistant.extra.COMMANDSString[]Used to pass in a list of voice commands to the speech recogniser
com.almer.voiceassistant.extra.SOURCE_PACKAGEStringUsed to pass the application's package name to the speech recogniser
com.almer.voiceassistant.extra.COMMANDStringPassed from the speech recogniser when a voice command is spoken. Identifies the command spoken by the user