title: how to send sensor values over udp
body: <p>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.</p>
  
  <ol>
  <li>I can send a udp packet if sensors are unregistered.</li>
  <li>I can display sensors if I don’t use udp to send data.</li>
  <li>I can’t do both simultaneously!</li>
  </ol>
  
  <p>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: </p>
  
  <pre><code>    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 &lt;String, Integer, Long&gt; {
  
      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() {
      }
  }
  </code></pre>