0
我从谷歌服用了大量代码后创建了一个应用程序。将它们放在一起后,应用程序似乎运行安静。当我启动应用程序时,它请求蓝牙访问。一旦提供,它会显示可用的蓝牙连接列表。点击后,它连接到Arduino板(HC-05模块)并发送命令。一切都很好,直到现在。如何清除关闭的Android应用程序
现在,当我使用Back键关闭应用程序时,应用程序仍然保持连接打开状态。在Arduino上,While(Serial1.available)返回true。
当我使用断开按钮断开连接时,它将关闭连接,并再次连接到电路板不会发送任何命令,而只是连接状态已连接。
为了使它工作,我必须重新安装使用android studio的应用程序,然后打开连接,它完美的工作。
我的疑惑是:在关闭应用程序之前是否有必须清除的东西?由于应用程序使用片段,我不知道如何清除所有的缓存,堆栈或其他任何东西。我已经添加了下面的代码来检查。
请帮忙。
public class MainActivityFragment extends Fragment {
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static BluetoothAdapter mAdapter = null;
private static OutputStream outStream = null;
private int message = 0;
private int aButton = 1;
private int bButton = 1;
private int cButton = 1;
private int dButton = 1;
private int eButton = 1;
private int fButton = 1;
private int gyButton = 0;
private float xcoord = 0;
private float ycoord = 0;
private int f1;
private int s2;
private int t3;
private int f4;
private int CS;
private int case_ = 0;
private int xPower = 0; // processed x-y power
private int yPower = 0;
private float initX = 0; // initial touch position
private float initY = 0;
private float origX = 0; // joystick button locations
private float origY = 0;
private long data_ = 0;
private double joyPower = 0;
private double joyAngle = 0;
private static BluetoothSocket mSocket = null;
private static Timer timer;
private final Handler handler = new Handler();
private static BluetoothDevice device;
private ImageView btButton;
private ImageView disconnectButton;
private static TimerTask sendMessage;
private static boolean isSchedule = false;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final TextView serO = (TextView) rootView.findViewById(R.id.serialOut);
final Vibrator vibr = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
timer = new Timer();
sendMessage = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
if (outStream != null) {
try {
message = 100;
outStream.write(message);
} catch (Exception e) {
}
}
}
});
}
};
// Get Bluetooth adapter
mAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
// Set aButton = 1 when A is pressed
final ImageButton a = (ImageButton) rootView.findViewById(R.id.a);
//Button a = (Button) rootView.findViewById(R.id.a);
a.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
aButton = 0;
//Toast.makeText(getActivity().getBaseContext(), "A Action Down" + aButton, Toast.LENGTH_LONG).show();
Vibrator vibr = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
vibr.vibrate(50);
a.setImageResource(R.drawable.white_bg);
return true;
case MotionEvent.ACTION_UP:
aButton = 1;
a.setImageResource(R.drawable.trans_bg);
return true;
}
return false;
}
});
private void checkBTState() {
if (mAdapter == null) {
Toast.makeText(getActivity().getApplicationContext(), "Bluetooth Not Supported", Toast.LENGTH_SHORT).show();
} else {
if (!mAdapter.isEnabled()) {
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
}
}
}
private static BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if (Build.VERSION.SDK_INT >= 10) {
try {
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[]{UUID.class});
return (BluetoothSocket) m.invoke(device, MY_UUID);
} catch (Exception e) {
}
}
return device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
}
public static void connect(String address, Context context) {
try {
device = mAdapter.getRemoteDevice(address);
} catch (Exception e) {
Toast.makeText(context, "Invalid Address", Toast.LENGTH_LONG).show();
}
try {
mSocket = createBluetoothSocket(device);
} catch (Exception e) {
}
mAdapter.cancelDiscovery();
try {
// try connecting to socket
mSocket.connect();
} catch (Exception e) {
try {
mSocket.close();
} catch (Exception e1) {
Log.e("E4", "Socket Connection Exception");
}
}
try {
outStream = mSocket.getOutputStream();
if (!isSchedule) {
timer.schedule(sendMessage, 0, 500);
isSchedule = true;
} else {
Log.e("Timer", "Timer already schedule");
}
Toast.makeText(context, "Connection Established", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
}
}
public static boolean isConnected() {
// Check if the phone has already connected to bluetooth device
return device != null;
}
private void disconnectDevice() {
if (device != null) {
device = null;
try {
outStream.close();
} catch (Exception e) {
}
outStream = null;
mSocket = null;
Toast.makeText(getActivity(), "Bluetooth Device Disconnected", Toast.LENGTH_SHORT).show();
}
}
}
是。我没有检查Activity Lifecycle的链接。但正如我之前所确认的那样,我不知道该如何放置,因为我所做的一切都是在没有android背景知识的情况下搞乱了代码。顺便说一句,我应该在这段代码中添加onStop()?也不知道Java。我认为这是通过将不同代码填充在一起可以学习的一种方式。如果您可以通过某种方式来添加该功能,将会有所帮助。 – Praveen
[onStop()](https://developer.android.com/reference/android/app/Activity.html#onStop())是一种您可以在活动中覆盖的方法,就像您重写onCreate –
u意思是:'我不知道该把什么放在哪里' –