我一直在尝试在我的应用程序中创建一个包含蓝牙 RFID 扫描仪的功能,它与我的设备配对,并且我可以让它正常工作。
我可以接收文本并将其记录在控制台中,当我编译 Activity 时,一切正常,棒读取代码,然后将文本附加到 EditText 中,但是如果我返回并再次输入 Activity ,我可以在日志中看到代码,但文本不会转到 Edittext。
我尝试了很多不同的方法,但似乎没有任何效果:/
这是我的代码:
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> bondedSet = mBluetoothAdapter.getBondedDevices();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
}
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Please enable your BT and re-run this program.", Toast.LENGTH_LONG).show();
finish();
}
if (mBluetoothAdapter.isEnabled()) {
if(bondedSet.size() == 1){
for(BluetoothDevice device : bondedSet){
address = device.getAddress();
Log.d("bt:", address);
}
}
}
String address = "00:A0:96:2A:0A:1B";
out = (EditText) findViewById(R.id.output);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Log.d(TAG, device.getName() + " connected");
myConnection = new ConnectThread(device);
myConnection.start();
}
private class ConnectThread extends Thread {
private final BluetoothSocket mySocket;
Message msg;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.d(TAG, "CONNECTION IN THREAD DIDNT WORK");
}
mySocket = tmp;
}
Handler uiThreadHandler = new Handler() {
public void handleMessage(Message msg) {
out = (EditText) findViewById(R.id.output);
Object o = msg.obj;
out.append(o.toString().trim());
Log.d("handler", o.toString());
}
};
public void run() {
out = (EditText) findViewById(R.id.output);
Log.d(TAG, "STARTING TO CONNECT THE SOCKET");
setName("My Connection Thread");
InputStream inStream = null;
boolean run = false;
mBluetoothAdapter.cancelDiscovery();
try {
mySocket.connect();
run = true;
} catch (IOException e) {
Log.d(TAG, this.getName() + ": CONN DIDNT WORK, Try closing socket");
try {
mySocket.close();
Log.d(TAG, this.getName() + ": CLOSED SOCKET");
} catch (IOException e1) {
Log.d(TAG, this.getName() + ": COULD CLOSE SOCKET", e1);
this.destroy();
}
run = false;
}
synchronized (BluetoothActivity.this) {
myConnection = null;
}
byte[] buffer = new byte[1024];
int bytes;
// handle Connection
try {
inStream = mySocket.getInputStream();
while (run) {
try {
bytes = inStream.read(buffer);
readMessage = new String(buffer, 0, bytes);
msg = uiThreadHandler.obtainMessage();
msg.obj = readMessage;
uiThreadHandler.sendMessage(msg);
Log.d(TAG, "Received: " + readMessage);
} catch (IOException e3) {
Log.d(TAG, "disconnected");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
最佳答案
我的猜测是这与Thread
本身有关。当您第一次启动 Activity
时,您还可以在 Thread
上调用 .start()
,这样就可以正常工作。
问题是当您离开Activity
并再次打开它时。在这种情况下,将调用 onStop()
或 onPause()
之一(根据具体情况),并调用 onRestart()
或 onResume()
随后将分别调用。
现在技巧来了:与此同时,所有这些过程,您的Thread
仍在运行。当您显示代码时,它没有停止/暂停,并且一直在运行。所以基本上我的建议是,您在 Activity
的 onCreate()
方法中执行的操作也应该在 onPause()
中执行和 onStop()
事件,以及我的另一个提示,它位于您的 ConnectThread(BluetoothDevice device)
方法中的某个位置。
要了解如何继续,我首先在您的 Activity
中定义 onStop()
和 onPause()
方法,然后查看哪个方法被触发,记录每个属性以查看其值/状态,这样您就可以调试失败的内容。
There's Activity 生命周期图。
关于java - android Java - 当 Activity 重新启动时,Textview 不附加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21700958/