我使用Android手机作为使用Arduino微控制器的机器人的遥控器。该应用程序按预期工作,并将x和y轴数据从加速度计发送到Arduino机器人。
问题是应用程序有时会冻结(通常在几分钟后)。发送到机器人的x和y轴数据也显示在Android手机的屏幕上,我可以看到x和y轴数据没有变化。如果我只是将Android连接到Arduino机器人而不打开电机的电源,它会保持连接并一次发送数小时数而不会冻结。
只有当我使用手机作为遥控器并打开电机时,Android似乎才会崩溃。对我来说毫无意义的是电机(EMI)可能会干扰Android应用程序。 Arduino将没有数据发送回Android。有什么想法吗?
package com.example.shiel.sonicartrc;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
public class SendData extends AppCompatActivity implements SensorEventListener {
Button btnOn, btnOff, btnDis;
float x, y;
String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
//MOTION STARTS
//private static final String TAG = ledControl.class.getSimpleName();
private SensorManager mSensorManager;
private Sensor mAccelerometer;
TextView tv, tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent newint = getIntent();
address = newint.getStringExtra(BTdevices.EXTRA_ADDRESS);
//receive the address of the bluetooth device
//view of the ledControl
setContentView(R.layout.activity_send_data);
//call the widgets
btnOn = (Button) findViewById(R.id.button2);
btnOff = (Button) findViewById(R.id.button3);
btnDis = (Button) findViewById(R.id.button4);
//MOTION START
//get the sensor service
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//get the accelerometer sensor
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
tv = (TextView) findViewById(R.id.xval);
tv1 = (TextView) findViewById(R.id.yval);
Log.d("TAG", "My debug-message4");
//MOTION END
new ConnectBT().execute(); //Call the class to connect
//commands to be sent to bluetooth
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
turnOnLed(); //method to turn on
}
});
btnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
turnOffLed(); //method to turn off
}
});
btnDis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Disconnect(); //close connection
}
});
}
private void Disconnect() {
if (btSocket != null) {
//If the btSocket is busy
try {
btSocket.close(); //close connection
} catch (IOException e) {
msg("Error");
}
}
finish(); //return to the first layout
}
private void turnOffLed() {
if (btSocket != null) {
try {
btSocket.getOutputStream().write("<a>".getBytes());
} catch (IOException e) {
msg("Error");
}
}
}
private void turnOnLed() {
if (btSocket != null) {
try {
btSocket.getOutputStream().write("<b>".getBytes());
} catch (IOException e) {
msg("Error");
}
}
}
private void sendMotor(String cmdSendLR) {
if (btSocket != null) {
try {
btSocket.getOutputStream().write(cmdSendLR.getBytes());
} catch (IOException e) {
msg("Error");
} finally{
btSocket.close();
}
}
}
// fast way to call Toast
private void msg(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
private class ConnectBT extends AsyncTask<Void, Void, Void> {
// UI thread
private boolean ConnectSuccess = true; //if it's here, it's almost connected
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(SendData.this, "Connecting", "Please wait"); //show a progress dialog
}
@Override
protected Void doInBackground(Void... devices) {
//while the progress dialog is shown, the connection is done in background
try {
if (btSocket == null || !isBtConnected) {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
//get the mobile bluetooth device
BluetoothDevice dispositive = myBluetooth.getRemoteDevice(address);
//connects to the device's address and checks if it's available
btSocket = dispositive.createInsecureRfcommSocketToServiceRecord(myUUID);
//create a RFCOMM (SPP) connection
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();//start connection
}
} catch (IOException e) {
ConnectSuccess = false;//if the try failed, you can check the exception here
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//after the doInBackground, it checks if everything went fine
super.onPostExecute(result);
if (!ConnectSuccess) {
msg("Connection Failed");
finish();
} else {
msg("Connected");
isBtConnected = true;
}
progress.dismiss();
}
}
//MOTION STARTS
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
WindowManager windowMgr = (WindowManager) this.getSystemService(WINDOW_SERVICE);
int rotationIndex = windowMgr.getDefaultDisplay().getRotation();
if (rotationIndex == 1 || rotationIndex == 3) {
// detect 90 or 270 degree rotation
x = -event.values[1];
y = event.values[0];
} else {
x = event.values[0]; //Force x axis in m s-2
y = event.values[1]; //Force y axis in m s-2
}
sendEventValues();
}
public void sendEventValues() {
tv.setText("X axis" + "\t\t" + x);
tv1.setText("Y axis" + "\t\t" + y);
if (isBtConnected) {
int xx = Math.round(x);
int yy = Math.round(y);
sendMotor("<" + xx + "," + yy + ">");
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
//MOTION ENDS
}
答案 0 :(得分:0)
我觉得你没有关闭你的插座。您需要打开您的套接字,发送数据然后关闭它。
try {
...
}finally{
try {
mSocket.close();
} catch (IOException closeException) { }
只是澄清一下。如果您没有关闭套接字,程序将泄漏文件描述符。这不是直接的,这就是为什么你会在一段时间后看到它冻结的原因。