我正在尝试创建一个读取传感器(陀螺仪)原始数据的应用程序,并使用套接字将其实时发送到其他设备。 我已经通过在AsyncTask中使用Java Socket类实现了双方之间的简单通信,并且它可以很好地处理虚拟数据。
我不确定如何将传感器部件的整个读取原始数据与插座部件集成。
点击一下按钮就会触发传感器读取的开始,从那一刻开始,应该每隔10ms进行一次采样,并通过插座发送采样数据。
我应该使用另一个asyncTask从传感器读取数据吗?如何在执行时将传感器采样的数据发送到套接字的AsyncTask?这种缝合的最佳做法是什么?
这里是我已经hava的代码(只有套接字处理,没有传感器) MainActivity:
public class MainActivity extends AppCompatActivity {
Button btnConnect, btnCloseSocket;
EditText etIP;
EditText etPort;
boolean isConnected = false;
ConnectToServerAsyncTask connectToServerAsyncTask = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConnect = (Button) findViewById(R.id.btn_connect);
btnCloseSocket = (Button) findViewById(R.id.btn_closeSocket);
}
public void socketOperationRequest(View view) {
if (!isConnected) {
connectToServerAsyncTask = new ConnectToServerAsyncTask(MainActivity.this);
}
switch (view.getId()) {
case R.id.btn_connect:
{
isConnected = true;
etIP = (EditText) findViewById(R.id.et_ip);
etPort = (EditText) findViewById(R.id.et_port);
String IP = etIP.getText().toString();
String port = etPort.getText().toString();
connectToServerAsyncTask.execute(IP, port);
btnConnect.setEnabled(false);
btnCloseSocket.setEnabled(true);
break;
}
case R.id.btn_closeSocket:
{
isConnected = false;
connectToServerAsyncTask.cancel(true);
btnCloseSocket.setEnabled(false);
btnConnect.setEnabled(true);
break;
}
}
}
}
的AsyncTask:
public class ConnectToServerAsyncTask extends AsyncTask<String,String,String> {
Context context;
Socket socket;
OutputStream os;
//boolean running = true;
public ConnectToServerAsyncTask(Context ctx) {
this.context = ctx;
}
@Override
protected String doInBackground(String... strings) {
String IP = strings[0];
String port = strings[1];
InputStream is = null;
int data = 1;
try {
InetAddress inetIP = InetAddress.getByName(IP);
socket = new Socket(inetIP, Integer.parseInt(port));
os = socket.getOutputStream();
publishProgress("test");
while(!this.isCancelled()){
os.write(data);
Thread.sleep(2);
}
} catch (UnknownHostException e) {
publishProgress("Exception");
e.printStackTrace();
} catch (IOException e) {
publishProgress("Exception2");
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
String message = values[0];
Toast.makeText(this.context, message, Toast.LENGTH_LONG).show();
}
@Override
protected void onCancelled() {
super.onCancelled();
try {
os.flush();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
抱歉我的英语不好, 谢谢
答案 0 :(得分:1)
首先,您应该在SensorManager
中找到您的传感器。例如(用这个替换你的os.write
循环,然后像你一样添加一个无限的睡眠循环):
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor gyro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(new MySensorListener(), gyro, SensorManager.SENSOR_DELAY_FASTEST);
然后你应该实现监听器,从它的onSensorChanged
方法写入os。
确保在关闭流之前取消注册该侦听器(unregisterListener
)。
有关详细信息,请参阅Android文档:https://developer.android.com/reference/android/hardware/SensorManager.html