on my quest to learn Java / Android development, Im works at a lot of roadblocks. Mostly because I really don't know much about threads and the relationships between threads / processes. I am trying to transfer IMU data from an android device to a python application on a computer. Whenever sensor values change, the sensor receiver stores the current values in a variable to access the network handler.
The network handler, in turn, must start on a timer, sending values and the current time stamp with a more or less fixed frequency of 33 Hz (maybe a bit fast? Well, and I'm ready to accept a clock frequency of 10 Hz, but not slower than that ) In any case, when I tested this, I could see on the computer interface that the data almost did not approach a constant pace of 30 per second, but rather appeared in jumps, sometimes they didn’t come for a second, and on the whole the delay accumulated (i.e. e. the later the values, the more they are delayed). I understand that there may be some variability and some delays in the network, but I would like to at least match the pace, at least the right one, i.e. That it does not get worse and worse, the longer Im takes.
Given that the devices are on a shared Wi-Fi network, and Im is capable of transmitting 1080p video streaming without any delay through Wi-Fi, Im pretty sure that the protocol should be able to process a 64 byte string every 30 ms without problems. To eliminate the sensor sensor as the source of the problem, I made a minimal working example that simply sends a line every 30 ms without reading the sensor. I basically got this code from different stackoverflow posts and modified it until it more or less did what I wanted. The problem is that the network interface works in AsynchronousTask, for which I am not sure how to access it after it starts. My theory is that it spends resources opening a new socket for each new data packet, but Im not surehow to open a socket once in the background, and then pass timer values to it and pass it for sending.
, :
package com.jamesdoesntlikejava.motionlearning15;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class SendValuesActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_values);
final Timer timer = new Timer();
TimerTask taskNew = new TimerTask() {
@Override
public void run() {
int counter = 0;
int numsteps = 333;
String params[] = new String[2];
if (counter < numsteps) {
params[0] = "192.168.1.33";
long currentTime = System.currentTimeMillis();
params[1] = Long.toString(currentTime)+"blablabla";
new ServerCommunicationTask().execute(params);
counter++;
} else {
timer.cancel();
timer.purge();
}
}
};
Toast.makeText(this, "Sending Values in 1s...", Toast.LENGTH_SHORT).show();
timer.scheduleAtFixedRate(taskNew,1000,30);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send_values, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
:
package com.jamesdoesntlikejava.motionlearning15;
import android.os.AsyncTask;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ServerCommunicationTask extends AsyncTask<String, Void, String> {
public final static int TCP_SERVER_PORT = 13337;
@Override
protected String doInBackground(String[] params) {
String TCP_SERVER_IP = params[0];
try {
Socket s = new Socket(TCP_SERVER_IP, TCP_SERVER_PORT);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String outMsg = params[1];
out.write(outMsg);
out.flush();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
@Override
protected void onPostExecute(String response) {
}
}
Moto G LTE ( 1- ) android 5.1. , !