Gson不会序列化Mac地址

问题描述

我想保存蓝牙设备的Mac Adress,因此我将其与Gson序列化以将其转换为String类型,但不返回任何内容

Gson gson = new Gson();
Log.d("my device",String.valueOf(myBlu@R_404_6440@othDevice));   //93:39:1B:04:00:71
String dev = gson.toJson(myBlu@R_404_6440@othDevice,Blu@R_404[email protected]);
Log.d("my device after",dev);   //{}

感谢帮助

解决方法

要获取BluetoothDevice的地址,请调用getAddress方法:

String address = myBluetoothDevice.getAddress();
// address = 00:11:22:AA:BB:CC

如果您要将String转换为该字符串的JSON表示形式(基本上只添加引号),则可以使用gson.toJson()

String addressInJson = gson.toJson(myBluetoothDevice.getAddress());
// addressInJson = "00:11:22:AA:BB:CC"