我有一个类似于Google蓝牙示例的代码,具有以下类别:
private class ConnectedThread extends Thread implements Serializable {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.available();
if(bytes != 0) {
buffer = new byte[1024];
SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
bytes = mmInStream.available(); // how many bytes are ready to be read?
bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget(); // Send the obtained bytes to the UI activity
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(String input) {
byte[] bytes = input.getBytes(); //converts entered String into bytes
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
我有一个ConnectedThread
mConnectedThread
,我想从MainActivity传递到另一个活动。两个活动中的变量名称相同。
我使用startController.putExtra("BLUETOOTH_CONNECTED_THREAD",mConnectedThread);
但是,我收到此错误,指出类型不兼容。
我尝试使用
将其强制转换为类 mConnectedThread = (ConnectedThread) getIntent().getSerializableExtra("BLUETOOTH_CONNECTED_THREAD");
,并且至少可以编译。
这是我在发送对象的活动中的代码:
Intent startController = new Intent(MainActivity.this,ControllerActivity.class);
startController.putExtra("BLUETOOTH_CONNECTED_THREAD",mConnectedThread);
startActivity(startController);
编辑-这是我的错误日志:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = in.justrobotics.jrbluetoothcontrol.MainActivity$ConnectedThread)
at android.os.Parcel.writeSerializable(Parcel.java:1527)
at android.os.Parcel.writeValue(Parcel.java:1475)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1423)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8697)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3159)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1520)
at android.app.Activity.startActivityForResult(Activity.java:4402)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
at android.app.Activity.startActivityForResult(Activity.java:4360)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at android.app.Activity.startActivity(Activity.java:4699)
at android.app.Activity.startActivity(Activity.java:4667)
at in.justrobotics.jrbluetoothcontrol.MainActivity$3.onClick(MainActivity.java:110)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6239)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Caused by: java.io.NotSerializableException: android.bluetooth.BluetoothInputStream
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at android.os.Parcel.writeSerializable(Parcel.java:1522)
... 25 more
但这会导致运行时错误。
所以,现在,我有mConnectedThread = getIntent().getSerializableExtra("BLUETOOTH_CONNECTED_THREAD");
在它下面有一条红色的波浪线。
该如何解决?相反,如何反序列化对象? 另外,如果我可以使用Parcelable解决问题,那么我很乐意收到同样的解决方案。
This是我完整的源代码。
答案 0 :(得分:0)
在我看来,您的线程必须位于活动之外。这意味着您需要使用Singleton,Manager或类似的工具来处理您的线程。