0

Is it possible to save BluetoothSocket object using shared preference in android . I got the reference from this How Android SharedPreferences save/store object ,and saved the object using this reference. My problem is that i can not retrieve the object as BluetoothSocket.

This is my code for saving

void saveBluetoothDevice(String connectingDevice)
{
    SharedPreferences.Editor editor = getSharedPreferences(Prefer_Bluetooth_name, MODE_PRIVATE).edit();
    Gson gson = new Gson();
    editor.putString("name", connectingDevice);
    String json = gson.toJson(mmDevice);
    editor.putString("MyObject", json);
    //editor.putInt("idName", 12);
    editor.apply();
}`  

For retrieving:

 String getBluetoothName()
{
    String name = "";
    SharedPreferences prefs = getSharedPreferences(Prefer_Bluetooth_name, MODE_PRIVATE);
    name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
    Gson gson = new Gson();
    String json = prefs.getString("MyObject", "");
    mmSocket= gson.fromJson(json, BluetoothSocket);
    return name;
}

But its not Ok.Please help me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
happy_coding
  • 1,064
  • 1
  • 13
  • 24
  • Well what is exactly the problem? What is not ok? – greenapps Jan 31 '18 at 08:44
  • `mmSocket= gson.fromJson(json, BluetoothSocket);` ?? What is `BluetoothSocket` ? What is `mmSocket` ? You are suppost to post complete code. – greenapps Jan 31 '18 at 08:46
  • `void saveBluetoothDevice(String connectingDevice)` ?? What is `connectingDevice` ? If you want to save a socket then your code should start with a socket of course. – greenapps Jan 31 '18 at 08:47
  • `String json = gson.toJson(mmDevice);` ? What is `mmDevice` ? Declare all used variables first. Can you tell us what the value of `json` is? Please tell. Its a string. So it will contain a text. Which text? – greenapps Jan 31 '18 at 08:49
  • @greenapps mmDevice is the object of BluetoothSocket – happy_coding Jan 31 '18 at 09:05
  • Yes i can imagine that. But please take the time to post decent code. And react to all comments please. And give all the requested info. Why didnt you give it? And tell which problem you actually have. – greenapps Jan 31 '18 at 09:07
  • Why to you want to save the socket? You wanna keep the connection? Or what? – greenapps Jan 31 '18 at 09:09
  • `void saveBluetoothDevice(String connectingDevice)` That is a strange function as the parameter is a string. You should make a function more like `void saveBluetoothDevice(BluetoothSocket Device)`. Start with a socket. I asked you that before. – greenapps Jan 31 '18 at 09:12
  • @g mmDevice is object of android.bluetooth.BluetoothSocket – happy_coding Jan 31 '18 at 09:20
  • @greenapps In my app one activity for listing all Bluetooth paired device for selecting. And in another activity i prefer to sent data to the selected Bluetooth device. – happy_coding Jan 31 '18 at 09:25
  • Ok. Nice to know. But then answer the comments now and supply all the requested info. – greenapps Jan 31 '18 at 09:28
  • `But its not Ok.Please help me.` Terrible! You still did not tell what is not ok exactly. We try to help but you are not reacting on helpfull comments. Why so reluctant? – greenapps Jan 31 '18 at 09:30

1 Answers1

0

Have you tried using mmSocket= gson.fromJson(json, BluetoothSocket.class);.
Please put log for value of String json.

String getBluetoothName()
{
    String name = "";
    SharedPreferences prefs = getSharedPreferences(Prefer_Bluetooth_name, MODE_PRIVATE);
    name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
    Gson gson = new Gson();
    String json = prefs.getString("MyObject", "");
    mmSocket= gson.fromJson(json, BluetoothSocket.class);    <-- This one
    return name;
}
Kasim Rangwala
  • 1,765
  • 2
  • 23
  • 44