gpt4 book ai didi

android - 在应用程序关闭后保存 BluetoothDevice 对象

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:55 24 4
gpt4 key购买 nike

我想保存我的应用程序连接到的最后一个蓝牙设备。如果以前有蓝牙连接,我根本不想提示用户。他们可以选择连接到新设备,但他们不需要这样做。如果他们选择不选择连接,他们会定期使用该应用程序,然后在需要蓝牙设备时,它会连接到最近的设备。

我已尝试使用下面 Tudor Luca 的 答案中提供的代码,但该对象不会写入文件。我得到一个 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)

最佳答案

无法直接序列化 BluetoothDevice 类。即使您序列化它,我认为您也无法在应用程序关闭后重新使用该对象。相反,我有一个帮助程序类来存储设备的地址。您可以保存设备地址和名称并在以后阅读该信息。然后,您可以对绑定(bind)的设备执行发现/搜索并获取相应的设备。

这是我通常使用的辅助类

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 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13807241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com