В настоящее время я работаю над своим проектом FYP, который представляет собой умную комнату с использованием Android на основе Arduino Ethe rnet щита. я могу контролировать некоторые приборы в комнате с помощью этого приложения Android. У меня есть способы, чтобы управлять комнатным прибором или голосовой командой или переключением режима. Режим переключения работает нормально, но голосовая команда не работает вообще. Я попытался запустить приложение на телефоне Android, но оно не дает никакой функции, но режим переключения работает нормально. Код голосовой команды
package com.example.user.iSmartHome;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class voicemode extends Activity implements OnClickListener {
private SpeechRecognizer sr;
private static final String TAG ="voicemode";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popvoice);
ImageButton speakButton = (ImageButton) findViewById(R.id.btn_speak);
speakButton.setOnClickListener(this);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
}
//making class for the speech recognition.
class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {}
public void onBeginningOfSpeech() {}
public void onRmsChanged(float rmsdB) {}
public void onBufferReceived(byte[] buffer) {}
public void onEndOfSpeech() {}
public void onError(int error)
{
if(error != 7) {
Log.d(TAG, "error " + error);}
}
public void onResults(Bundle results) {
String str = "";
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(data.size() != 0) {
for (int i = 0; i < data.size(); i++) {
Log.d(TAG, "result " + data.get(i));
str += data.get(i);
str += " ";
}
str = str.toLowerCase();
Toast.makeText(getApplicationContext(), data.get(0).toString(), Toast.LENGTH_SHORT).show();
String message = "message=" + str;
new Background_get().execute(message);}
}
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "onPartialResults");
}
public void onEvent(int eventType, Bundle params)
{
Log.d(TAG, "onEvent " + eventType);
}
}
//create onClick function which activates when you press the button.
public void onClick(View v) {
if (v.getId() == R.id.btn_speak)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
sr.startListening(intent);
}
}
private class Background_get extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL("http://192.168.1.14?" + params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
result.append(inputLine).append("\n");
in.close();
connection.disconnect();
return result.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}