如何将 BluetoothDevice 对象存储到 Flutter 应用程序中的共享首选项

发布于 2025-01-11 10:48:44 字数 257 浏览 0 评论 0原文

我正在使用 flutter_blue 包来获取蓝牙设备。连接后,我尝试在本地存储设备,以便即使蓝牙设备处于非活动状态也可以看到设备信息。

我发现的问题是我似乎无法保存BluetoothDevice对象及其设备信息,因为共享首选项不支持对象类型Bluetoothdevice并且没有任何toJsonfromJson< /code> 包中提供的方法。我听有人说要连载,但我还是很困惑。有人可以帮助我吗,因为我不明白该怎么做。

I am using flutter_blue package to get the Bluetooth device. After connection i am trying to store the device locally so that the device info can be seen even if the Bluetooth device is inactive.

the problem I am finding is I cant seem to save the BluetoothDevice object along with its device info since shared preferences doesn't support object type Bluetoothdevice and that there isn't any toJson or fromJson methods provided in the package. I have heard some people say to serialize it but I am still confused. Could someone please help me since I don't understand what to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2025-01-18 10:48:44

BluetoothDevice 对象看起来不像您通常缓存的信息,因为 BLE 设备的可识别功能通常由于各种隐私设置而被屏蔽。

不过,如果您想将其存储在 SharedPreferences 中。尝试创建一个包含 BluetoothDevice 类字段的映射,并使用 SharedPreferences 来存储它。我似乎记得从映射到 JSON 字符串的序列化是由库内部完成的。

我个人使用 扩展方法 来扩展类的功能由第三方库定义。您可以使用扩展来定义您提到的 toJsonfromJson 类。

The BluetoothDevice object does not look like information you'd usually cache, since the identifiable features for a BLE Device are usually masked due to various privacy settings.

Still, if you want to store it in the SharedPreferences. Try making a map containing the fields from the class BluetoothDevice and using SharedPreferences to store that. I seem to remember that the serialization from a map to a JSON string is done internally by the library.

I personally use Extension methods to extend the functionality on classes defined by third-party libraries. You can use an extension to define the toJson and fromJson classes that you mention.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文