1

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() {
    }
}
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
misalu2001
  • 73
  • 3
  • 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 :) – Tina J Jun 19 '14 at 15:18

1 Answers1

1

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?

You should also use AlarmManager instead of a TimerTask. @CommonsWare has a lot of good info on AlarmManager, for example: Android: How to use 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.

Community
  • 1
  • 1
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • 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 Mar 10 '12 at 16:18
  • Added a link above and there are lots more if you Google for them. Also added some extra advice. – Eric Levine Mar 10 '12 at 17:05
  • @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 Jun 19 '14 at 13:55
  • @TinaJasmin It wasn't my code, misalu2001 asked the question. – Eric Levine Jun 19 '14 at 14:46