Android Open Source - final_project_android Arduino Parser






From Project

Back to project page final_project_android.

License

The source code is released under:

MIT License

If you think the Android project final_project_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.geminnu.hexagon;
/*ww w.  jav a 2 s.  c om*/
import java.io.IOException;
import java.io.StringReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class ArduinoParser  {
  
  private String mMessage;
  
  public ArduinoParser(String message) {
    this.mMessage = message;
  }
  
  public ArduinoMessage ReadDataXML() throws XmlPullParserException, IOException  {
    
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        StringReader sR = new StringReader(mMessage);
        xpp.setInput(sR);
        int eventType = xpp.getEventType();
        
        
        String message_type = null;
        int message_typeFromString = 0;
        String type = null;
        int typeFromString = 0;
        String value = null;
        float valueFromString = 0;
        
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_DOCUMENT) {
            } else if(eventType == XmlPullParser.END_DOCUMENT) {
            } else if(eventType == XmlPullParser.START_TAG) {
              String name = xpp.getName();
          
          if (name.equals("message_type")) {
            message_type = ReadMessageType(xpp, eventType);
            message_typeFromString = Integer.valueOf(message_type);
          } else if (name.equals("type")) {
            type = ReadSensorType(xpp, eventType);
            typeFromString = Integer.valueOf(type);
          } else if (name.equals("value")) {
            value = ReadValue(xpp, eventType);
            valueFromString = Float.valueOf(value);
          } else {
            
          }
            } else if(eventType == XmlPullParser.END_TAG) {
            } else if(eventType == XmlPullParser.TEXT) {
            }
            eventType = xpp.next();
           }
        
        
        return new ArduinoMessage(message_typeFromString, typeFromString, valueFromString);
        
  }
  private String ReadMessageType(XmlPullParser parser, int eventType) throws XmlPullParserException, IOException {
        String msg_type = readText(parser);
        return msg_type;
    }
  
  private String ReadSensorType(XmlPullParser parser, int eventType) throws XmlPullParserException, IOException {
        String sensor = readText(parser);
        return sensor;
    }
  
  private String ReadValue(XmlPullParser parser, int eventType) throws XmlPullParserException, IOException {
        String value = readText(parser);
        return value;
    }
  
  private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
      
    if (parser.next() == XmlPullParser.TEXT) {
          result = parser.getText();
      }
    
      return result;
  }
  
  public ArduinoMessage ReadDataJSON() {
    
        
        String message_type = null;
        int message_typeFromString = 0;
        String type = null;
        int typeFromString = 0;
        String value = null;
        float valueFromString = 0;
        
       if(mMessage != null) {
        try {
      JSONObject jsonObj = new JSONObject(mMessage);
      JSONArray data = jsonObj.getJSONArray("data");
      
      for(int i = 0; i < data.length(); i++) {
        
        JSONObject d = data.getJSONObject(i);
        
        message_type = d.getString("message_type");
        message_typeFromString = Integer.valueOf(message_type);
        type = d.getString("type");
        typeFromString = Integer.valueOf(type);
        value = d.getString("value");
        valueFromString = Float.valueOf(value);
      }
      
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
       }    
       return new ArduinoMessage(message_typeFromString, typeFromString, valueFromString);
  
  }
}




Java Source Code List

com.geminnu.hexagon.Alert.java
com.geminnu.hexagon.ArduinoMessage.java
com.geminnu.hexagon.ArduinoParser.java
com.geminnu.hexagon.ArduinoReceiver.java
com.geminnu.hexagon.ArduinoService.java
com.geminnu.hexagon.ArduinoStatusEventListener.java
com.geminnu.hexagon.ArduinoStatusEvent.java
com.geminnu.hexagon.ArduinoStatusManager.java
com.geminnu.hexagon.ArduinoStatus.java
com.geminnu.hexagon.ArduinoTask.java
com.geminnu.hexagon.ArduinoTransmitter.java
com.geminnu.hexagon.BioSensorEventListener.java
com.geminnu.hexagon.BioSensorEvent.java
com.geminnu.hexagon.BioSensorListenerItem.java
com.geminnu.hexagon.BioSensorManagerService.java
com.geminnu.hexagon.BioSensor.java
com.geminnu.hexagon.Bluetooth.java
com.geminnu.hexagon.CoordinatorActionListener.java
com.geminnu.hexagon.Coordinator.java
com.geminnu.hexagon.MainActivity.java
com.geminnu.hexagon.MessageListener.java
com.geminnu.hexagon.MessageSender.java
com.geminnu.hexagon.MySQLiteHelper.java
com.geminnu.hexagon.Profile.java
com.geminnu.hexagon.Reading.java
com.geminnu.hexagon.Wifi.java