'How to Store a BluetoothDevice Object to Shared Preferences in my Flutter App

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman