I hooked up a Spark relay shield to a popcorn machine, but wanted to take it to the next level, and create a voice recognition command system for it. This was actually surprisingly simple to do! See the video in the links first…
I used the stock standard relay shield example code from the spark.io/docs and then went on and flashed it to my Spark Core.
On the Android side, I won't bore you with starting a new project etc. but I will post the code that does the magic.
First we need a few properties to hold the data we need.
Button Start;
|
Next we define an onTouchListener for the audio button
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
|
You will notice from the above, that we fire off an Intent to the RecognizerIntent (android library) to do the hard work!
Since this is an online service that needs to talk to Google, we also put in a connection manager
public boolean isConnected() {
|
and then, of course, we need something to handle the results from the activity of speaking:
@Override
|
We use an List to display the text matches on screen, as a way of "training" your phone. It should get better at recognizing you soon though, and then you can disable the List and just go with the first option and hope for the best!
I then created a switch statement to help with parsing the keywords/phrases and set up a simple request function using Android Volley
public void sendRequest(String url, final String state) {
|