英文:
Gson doesn't serialyse mac adresses
问题
我想保存蓝牙设备的Mac地址,所以我使用Gson进行序列化,将其转换为String类型,但没有任何返回。
Gson gson = new Gson();
Log.d("my device", String.valueOf(myBluetoothDevice)); //93:39:1B:04:00:71
String dev = gson.toJson(myBluetoothDevice, BluetoothDevice.class);
Log.d("my device after", dev); //{}
谢谢帮助
英文:
I want to save the Mac Adress of a Bluetooth device, so I serialize it with Gson to convert it into a String type but it returns nothing.
Gson gson = new Gson();
Log.d("my device", String.valueOf(myBluetoothDevice)); //93:39:1B:04:00:71
String dev = gson.toJson(myBluetoothDevice, BluetoothDevice.class);
Log.d("my device after", dev); //{}
Thanks for help
答案1
得分: 2
获取BluetoothDevice的地址,请调用getAddress
方法:
String address = myBluetoothDevice.getAddress();
// address = 00:11:22:AA:BB:CC
如果您想将字符串转换为JSON表示形式的字符串,基本上只需添加引号,那么您可以使用gson.toJson()
:
String addressInJson = gson.toJson(myBluetoothDevice.getAddress());
// addressInJson = "00:11:22:AA:BB:CC"
英文:
To get the address of a BluetoothDevice, call the getAddress
method:
String address = myBluetoothDevice.getAddress();
// address = 00:11:22:AA:BB:CC
If you want to convert the String into a JSON representation of the string, which basically just adds quotes, then you can use gson.toJson()
:
String addressInJson = gson.toJson(myBluetoothDevice.getAddress());
// addressInJson = "00:11:22:AA:BB:CC"
专注分享java语言的经验与见解,让所有开发者获益!
评论