我可以在主要活动中发送数据和读取数据。我完全沟通。当我切换另一个活动时,我可以发送数据,但我无法读取数据。有什么问题?
private final ExecutorService mExecutor = Executors
.newSingleThreadExecutor();
private SerialInputOutputManager mSerialIoManager;
private final SerialInputOutputManager.Listener mListener = new SerialInputOutputManager.Listener() {
@Override
public void onRunError(Exception e) {
Log.d(TAG, "Runner stopped.");
}
@Override
public void onNewData(final byte[] data) {
Ozel_giris.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mListener == null) {
Context context = getApplicationContext();
CharSequence text = "Error!!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
// Read
else if (data.length == 8) {
Context context = getApplicationContext();
CharSequence text = "Read!!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
} else {
}
}
});
}
};
答案 0 :(得分:0)
试试这个
主要活动的onDestroy():
protected voidonDestroy(){
super.onDestroy();
if (mSerialIoManager!= null) {
Log.i("Device", "Stopping io manager ..");
mSerialIoManager.stop();
mSerialIoManager= null;
}
}
您必须在下一个活动
重新初始化mSerialIoManager
或者您可以创建一个初始化并使用SerialInputOutputManager mSerialIoManager
的新类,您必须在主活动中初始化它并继续在应用程序中使用它,然后在应用关闭时关闭/停止它。