Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Paul Scott
Published © CC BY-SA

Using Android voice API with Spark Cores

Relays and Spark cores with voice commands

BeginnerFull instructions provided1,702
Using Android voice API with Spark Cores

Things used in this project

Hardware components

Spark Core
Particle Spark Core
×1
Spark - Spark Relay Shield
×1
Android device
Android device
×1

Story

Read more

Code

code.txt

Java
private Response.Listener<String> createMyReqSuccessListener() {

	return new Response.Listener<String>() {

		@Override

		public void onResponse(String response) {

			Log.i(TAG, response);

		}

	};

}



private Response.ErrorListener createMyReqErrorListener() {

	return new Response.ErrorListener() {

		@Override

		public void onErrorResponse(VolleyError error) {

			Log.i(TAG, error.getMessage());

		}

	};

}

code.txt

Java
public void sendRequest(String url, final String state) {

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

	StringRequest myReq = new StringRequest(Method.POST, url,

			createMyReqSuccessListener(), createMyReqErrorListener()) {



		protected Map<String, String> getParams()

				throws com.android.volley.AuthFailureError {

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

			params.put("access_token",

					"your spark core access token here");

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

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

			} else {

				params.put("params", "r1,LOW");

			}

			return params;

		};

	};



	queue.add(myReq);

}

code.txt

Java
@Override

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.setContentView(R.layout.dialog_frag);

		match_text_dialog.setTitle("Select Matching Text");

		textlist = (ListView) match_text_dialog.findViewById(R.id.list);

		matches_text = data

			.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

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

				android.R.layout.simple_list_item_1, matches_text);

		textlist.setAdapter(adapter);

		textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {



			@Override

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

   				int position, long id) {

    			match_text_dialog.hide();

				String workingText = matches_text.get(position);

				

			}

		});

		match_text_dialog.show();

	}

	super.onActivityResult(requestCode, resultCode, data);

}

code.txt

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

	}

}

code.txt

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

    @Override

    public boolean onTouch(View v, MotionEvent event) {

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



        case MotionEvent.ACTION_DOWN:

	    v.setPressed(true);

	    // Start action ...

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

	    if (isConnected()) {

	        Intent intent = new Intent(

		RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

		RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

		startActivityForResult(intent, REQUEST_CODE);

	    } else {

		Toast.makeText(getApplicationContext(),

		"Plese Connect to Internet", Toast.LENGTH_LONG)

		.show();

	    }

	break;

	case MotionEvent.ACTION_UP:

	case MotionEvent.ACTION_OUTSIDE:

		v.setPressed(false);

		// Stop action ...

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

		break;

	case MotionEvent.ACTION_POINTER_DOWN:

		break;

	case MotionEvent.ACTION_POINTER_UP:

		break;

	case MotionEvent.ACTION_MOVE:

		break;

	}



	return true;

}

code.txt

Java
Button Start;

TextView Speech;

Dialog match_text_dialog;

ListView textlist;

ArrayList<String> matches_text;

Credits

Paul Scott
5 projects • 32 followers

Comments