VoiceControlActivity.java
Go to the documentation of this file.
00001 /*
00002 copyright 2014 UNL Nimbus Lab 
00003 
00004   Licensed under the Apache License, Version 2.0 (the "License");
00005      you may not use this file except in compliance with the License.
00006     You may obtain a copy of the License at
00007   
00008         http://www.apache.org/licenses/LICENSE-2.0
00009   
00010     Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013    See the License for the specific language governing permissions and
00014   limitations under the License.
00015 */
00016 package edu.nimbus.glass.robotvoicecontrol;
00017 
00018 import java.util.ArrayList;
00019 
00020 import org.json.JSONArray;
00021 import org.json.JSONException;
00022 import org.json.JSONObject;
00023 
00024 import com.google.android.glass.app.Card;
00025 
00026 
00027 
00028 import de.tavendo.autobahn.WebSocketConnection;
00029 import de.tavendo.autobahn.WebSocketHandler;
00030  
00031 
00032 import android.os.Bundle;
00033 import android.app.Activity;
00034 import android.speech.RecognizerIntent;
00035 import android.util.Log;
00036 import android.view.View;
00037 
00043 public class VoiceControlActivity extends Activity {
00044         
00046         public final static String HOST_ADDRESS = "ws://10.214.32.10:9090";
00047     @Override
00048     protected void onCreate(Bundle savedInstanceState) {
00049         super.onCreate(savedInstanceState);
00050     }
00051 
00052     @Override
00053     protected void onResume(){
00054         super.onResume();
00055         
00056         //Get the voice results and send them to the server.
00057         String command = "";
00058         ArrayList<String> voiceResults = getIntent().getExtras()
00059                 .getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
00060         
00061         for(int i=0;i< voiceResults.size(); i++){
00062                 Log.d("Voice Results", voiceResults.get(i));
00063                 command = voiceResults.get(i);
00064         }
00065         Card card1 = new Card(getBaseContext());
00066         card1.setText("Sending Command:\n" + command);
00067         // Don't call this if you're using TimelineManager
00068         View card1View = card1.toView();
00069         sendMessage(command);
00070         setContentView(card1View);
00071     }
00072    
00077     private void sendMessage(String command) {
00078 
00079                 final String url = HOST_ADDRESS;
00080                 Log.d("SENDING REQUEST", command);
00081                 final WebSocketConnection mConnection = new WebSocketConnection();
00082                 final String to_send = command;
00083 
00084                 try{
00085                         Log.d("Socket", "Atempting connection");
00086                         mConnection.connect(url, new WebSocketHandler(){
00087 
00088                                 boolean message_received = false;
00089                                 @Override
00090                                 public void onOpen() {
00091                                         //send the request
00092                                         JSONObject jsonRequest = new JSONObject();
00093 
00094 
00095                                         try {
00096                                                 
00097                                                 //Create the object and call it correctly using the API for the rosbridge server.
00098                                                 jsonRequest.put("op", "call_service");
00099                                                 jsonRequest.put("service", "/glass_voice_command");
00100                                                 
00101                                                 JSONObject args = new JSONObject();
00102                                                 args.put("command", to_send);
00103                                                 
00104                                                 jsonRequest.put("args", args);
00105                                                 Log.d("SENDING COMMAND", jsonRequest.toString());
00106 
00107                                                 mConnection.sendTextMessage(jsonRequest.toString());
00108 
00109 
00110                                         } catch(Exception e) {
00111 
00112                                         }
00113                                         Log.d("Main Connection", "Status: Connected to " + url);
00114 
00115                                 }
00116 
00120                                 @Override
00121                                 public void onTextMessage(String payload) {
00122                                         Log.d("Main Payload", payload);
00123                                         String result_string = "Falure";
00124                                         try {
00125                                                 JSONObject res = new JSONObject(payload);
00126                                                 if(res.getBoolean("result") == false){
00127                                                         result_string = "Service Falure";
00128                                                 }else{
00129                                                         result_string = res.getJSONObject("values").getString("result");
00130                                                 }
00131                                         } catch (JSONException e) {
00132                                         }
00133                                         message_received = true;
00134                                         //We got a message back from the server so lets create the cards for selection.
00135                                         Card card1 = new Card(getBaseContext());
00136                                 card1.setText("Done :\n" + result_string);
00137                                 // Don't call this if you're using TimelineManager
00138                                 View card1View = card1.toView();
00139                                 setContentView(card1View);
00140                                         mConnection.disconnect();                                       
00141                                         finish();
00142                                         
00143                                 }
00144 
00148                                 @Override
00149                                 public void onClose(int code, String reason) {
00150                                         Log.d("WEBSOCKET CLOSE", code + "");
00151                                         //If connection is lost change the view.
00152                                         if (code == 3 && !message_received){
00153                                                 Card card1 = new Card(getBaseContext());
00154                                         card1.setText("No Web Connection");
00155                                                 View card1View = card1.toView();
00156                                         setContentView(card1View);
00157                                                 
00158                                         }
00159                                 }
00160                                 
00161                         });
00162                         
00163                 }catch (Exception e){
00164                         e.printStackTrace();
00165                 }
00166         }
00167     
00168 }


ros_glass_tools
Author(s):
autogenerated on Thu Aug 27 2015 14:47:21