The Dictation class lets your Alloy app capture speech from the user and
receive it as text. It drives the system dictation UI and delivers the
transcription to your code through callbacks.
Platform Support: Dictation is available on Emery (Pebble Time 2) and Gabbro (Pebble Time 2 round), and requires a connected phone with a working internet connection.
Import the class and create an instance with onReadable and onError
handlers, then call start() to begin listening:
import Dictation from "pebble/dictation";
let dictation = new Dictation({
onReadable() {
console.log(`Transcription: ${this.read()}`);
},
onError(e) {
console.log(`Dictation error: ${e}`);
}
});
dictation.start();
| Method | Description |
|---|---|
start() |
Open the dictation UI and begin listening. |
stop() |
Stop an in-progress dictation session. |
read() |
Return the transcribed text. Call from onReadable. |
configure(options) |
Change dictation behavior (see Configuration). |
close() |
Release the dictation instance when you're done with it. |
| Event | Description |
|---|---|
onReadable() |
Called when a transcription is ready. Call this.read() to get the text. |
onError(status) |
Called when dictation fails (for example, no connection or no speech detected). status is a numeric error code. |
Pass a byteLength to the constructor to size the transcription buffer, and
use configure() to control the dictation UI:
import Dictation from "pebble/dictation";
let dictation = new Dictation({
byteLength: 512, // maximum transcription size, in bytes
onReadable() {
console.log(`Transcription: ${this.read()}`);
}
});
dictation.configure({
confirm: false, // skip the confirmation screen after dictation
errorDialogs: true // let the system show its own error dialogs
});
dictation.start();
| Option | Description |
|---|---|
confirm |
Whether to show the confirmation screen after dictation (default true). |
errorDialogs |
Whether the system shows its own error UI on failure. |
To keep listening after each transcription, start a new session from within
onReadable. Use setImmediate so the current session finishes cleanly first:
import Dictation from "pebble/dictation";
let dictation = new Dictation({
onReadable() {
console.log(`Transcription: ${this.read()}`);
setImmediate(() => {
console.log("Listening...");
this.start();
});
},
onError(e) {
console.log(`Dictation error: ${e}`);
}
});
console.log("Listening...");
dictation.start();
See the
hellodictation example
for a complete project.