cómo enviar valores de sensor a través de udp

I am a total noob. My goal is to send sensor data (gyro acc magnet) over udp. On the other side, matlab is going to receive the data.

  1. I can send a udp packet if sensors are unregistered.
  2. I can display sensors if I don’t use udp to send data.
  3. I can’t do both simultaneously!

This code is supposed to read accelerometer and display it, meanwhile it tries to send a predefined udp packet every 2000 ms asynchronously. But it fails to run! Any suggestions? My code is like:

    public class udp_sensors_matlab extends Activity implements SensorEventListener{
/** Called when the activity is first created. */
SensorManager sensorManager = null;

//for accelerometer values
TextView outputX;
TextView outputY;
TextView outputZ;

String messageStr="test udp";
String ip;
udpOut task;
int server_port = 12345;
DatagramSocket s = null;
int msg_length=messageStr.length();
byte[] message = messageStr.getBytes();
InetAddress local = null;
DatagramPacket p=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);     
    //just some textviews, for data output
    outputX = (TextView) findViewById(R.id.TextView01);
    outputY = (TextView) findViewById(R.id.TextView02);
    outputZ = (TextView) findViewById(R.id.TextView03);
    try {
        s = new DatagramSocket();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        local = InetAddress.getByName("81.31.187.32");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    task = new udpOut();
    task.execute("g");
    timer= new Timer();
    timerTask=new TimerTask(){    @Override
    public void run() {
        // TODO Auto-generated method stub
        task = new udpOut();
        task.execute("g");
    }};
    timer.scheduleAtFixedRate(timerTask, 2000, 2000);
}
@Override
protected void onResume() {
   super.onResume();
   sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
   super.onStop();
   sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
}
synchronized public void onSensorChanged(SensorEvent event) {

        switch (event.sensor.getType()){
            case Sensor.TYPE_ACCELEROMETER:
                outputX.setText("\tx:"+Float.toString(event.values[0]));
                outputY.setText("\ty:"+Float.toString(event.values[1]));
                outputZ.setText("\tz:"+Float.toString(event.values[2]));
            break;
    }
 }
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {} 

class udpOut extends AsyncTask <String, Integer, Long> {

    protected Long doInBackground(String... messageStr) {
        p = new DatagramPacket(message, msg_length,local,server_port);
        try {
            s.send(p);
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    protected void onProgressUpdate() {
    }
}

preguntado el 10 de marzo de 12 a las 14:03

Hi, wondering if you were able to fix your code? I am doing a same thing, it could be nice if you could share how you fixed it ;Thanks buddy :) -

1 Respuestas

I would suggest stepping through your code with the debugger to see if that gives you more specific information on what is/isn't happening. Are you getting any exceptions when you try to do both simulataneously?

También deberías usar AlarmManager en lugar de un TimerTask. @CommonsWare has a lot of good info on AlarmManager, Por ejemplo: Android: cómo utilizar AlarmManager

Here is one tutorial for using the Eclipse debugger: http://www.vogella.de/articles/EclipseDebugging/article.html.

In my experience with Android, it works best to try and set a breakpoint before the exception and step through from there. Otherwise, it will be difficult to decipher the exception if you just enter the debugger when it occurs.

contestado el 23 de mayo de 17 a las 11:05

Thank you for your replay. Is there any tutorial on how to debug using eclipse and monitor exeptions? (I have fair amount of experience with matlab but Shame on me, I don’t know how to debug in eclipse.) - misalu2001

Added a link above and there are lots more if you Google for them. Also added some extra advice. - Eric Levine

@elevine wondering if you were able to fix your code? I am doing a same thing, it could be nice if you could share how you fixed it :) Thanks buddy - tina j

@TinaJasmin It wasn't my code, misalu2001 asked the question. - Eric Levine

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.