
我尝试使用下面Tudor Luca的答案中提供的代码,但该对象不会写入该文件.我收到一个NotSerializableException.我想保存的对象是一个使用import android.bluetooth.BluetoothDevice导入的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.BluetoothDevice12-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.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)解决方法 无法直接序列化BluetoothDevice类.即使你序列化它,我认为你不能在应用程序关闭后重新使用该对象.相反,我有一个辅助类,它将存储设备的地址.您可以保存设备地址和名称,稍后再读取该信息.然后,您可以执行绑定设备的发现/搜索并获取相应的设备.
这是我通常使用的辅助类
public class BluetoothState implements Serializable { public static final int STATE_NOT_CONNECTED = 1; public static final int STATE_CONNECTED = 1; public static final String filename = "btState.pref"; public static int connectionState = STATE_NOT_CONNECTED; public static String deviceAddress = "00:00:00:00:00:00"; public static String devicename = ""; public static voID setConnectionState(boolean connected,BluetoothDevice device) { if(connected) connectionState = STATE_CONNECTED; else connectionState = STATE_NOT_CONNECTED; if(device != null) { deviceAddress = device.getAddress(); devicename = device.getname(); } } public static voID saveConnectionState(Context cxt) throws IOException { fileOutputStream fos = cxt.openfileOutput(filename,Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(connectionState); oos.writeUTF(deviceAddress); oos.writeUTF(devicename); } public static voID loadConnectionState(Context cxt) throws IOException { fileinputStream fis = cxt.openfileinput(filename); ObjectinputStream ois = new ObjectinputStream(fis); connectionState = ois.readInt(); deviceAddress = ois.readUTF(); devicename = ois.readUTF(); } public static BluetoothDevice getDevice() { BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); if(!btAdapter.isEnabled()) btAdapter.enable(); Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices(); for(BluetoothDevice d : pairedDevices) if(d.getAddress().equalsIgnoreCase(deviceAddress)) return d; return null; }} 总结 以上是内存溢出为你收集整理的android – 在应用程序关闭后保存BluetoothDevice对象全部内容,希望文章能够帮你解决android – 在应用程序关闭后保存BluetoothDevice对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)