我想保存我的应用程序连接到的最后一个蓝牙设备。如果以前有蓝牙连接,我根本不想提示用户。他们可以选择连接到新设备,但他们不需要这样做。如果他们选择不选择连接,他们会定期使用该应用程序,然后当需要蓝牙设备时,它将连接到最近的设备。
我尝试使用Tudor Luca's
下面答案中提供的代码,但对象不会写入文件。我得到一个NotSerializableException
. 我要保存的对象是一个BluetoothDevice
用import android.bluetooth.BluetoothDevice
.
这是我试图使蓝牙设备可序列化的方法:
import java.io.Serializable;
import android.bluetooth.BluetoothDevice;
public class SerializableObjects implements Serializable {
private BluetoothDevice device;
public SerializableObjects( BluetoothDevice device ) {
this.device = device;
}
public BluetoothDevice getDevice() {
return this.device;
}
}
LogCat 返回:
12-11 17:46:24.032: W/System.err(24641): java.io.NotSerializableException: android.bluetooth.BluetoothDevice
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
12-11 17:46:24.032: W/System.err(24641): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
12-11 17:46:24.032: W/System.err(24641): at my.eti.commander.LocalObjects.writeObjectToFile(LocalObjects.java:29)
12-11 17:46:24.032: W/System.err(24641): at my.eti.commander.MainMenu$1.handleMessage(MainMenu.java:460)
12-11 17:46:24.032: W/System.err(24641): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 17:46:24.036: W/System.err(24641): at android.os.Looper.loop(Looper.java:130)
12-11 17:46:24.036: W/System.err(24641): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-11 17:46:24.036: W/System.err(24641): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 17:46:24.036: W/System.err(24641): at java.lang.reflect.Method.invoke(Method.java:507)
12-11 17:46:24.036: W/System.err(24641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-11 17:46:24.036: W/System.err(24641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-11 17:46:24.036: W/System.err(24641): at dalvik.system.NativeStart.main(Native Method)