Android voice commands and Spark (Particle) Cores

How to communicate with your Spark (Particle) core with voice commands

Similar projects worth following
How to communicate with your Spark (Particle) core with voice commands

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 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;

TextView Speech;

Dialog match_text_dialog;

ListView textlist;

ArrayList<String> matches_text;

Next we define an onTouchListener for the audio button

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {


public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:


// Start action ...

Log.i(TAG, "Button pressed");

if (isConnected()) {

Intent intent = new Intent(




startActivityForResult(intent, REQUEST_CODE);

} else {


"Plese Connect to Internet", Toast.LENGTH_LONG)




case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_OUTSIDE:


// Stop action ...

Log.i(TAG, "Button pressed done");




case MotionEvent.ACTION_POINTER_UP:


case MotionEvent.ACTION_MOVE:



return true;


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() {

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo net = cm.getActiveNetworkInfo();

if (net != null && net.isAvailable() && net.isConnected()) {

return true;

} else {

return false;



and then, of course, we need something to handle the results from the activity of speaking:


protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

match_text_dialog = new Dialog(MainActivity.this);


match_text_dialog.setTitle("Select Matching Text");

textlist = (ListView) match_text_dialog.findViewById(;

matches_text = data


ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, matches_text);


textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {


public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {


String workingText = matches_text.get(position);




super.onActivityResult(requestCode, resultCode, data);


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) {

RequestQueue queue = Volley.newRequestQueue(getContext());

StringRequest myReq = new StringRequest(http://Method.POST, url,

createMyReqSuccessListener(), createMyReqErrorListener()) {

protected Map<String, String> getParams()

throws {

Map<String, String> params = new HashMap<String, String>();


"your spark core access token here");

if (state.equals("on")) {

params.put("params", "r1,HIGH"...

Read more »

  • 1 × Particle Core Spark Core has been renamed to Particle
  • 1 × Spark Relay Shield
  • 1 × An appliance
  • 1 × An understanding of the risks of high voltage!
  • 1 × An Android device

View all 6 components

  • 1
    Step 1

    Wire up the relay and the appliance to the relay shield. You will also need a 9V battery to power the core.

View all instructions

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates