Android 我做错了什么?将Bluetooth设备的ArrayList保存到SharedReferences

Android 我做错了什么?将Bluetooth设备的ArrayList保存到SharedReferences,android,bluetooth,sharedpreferences,android-bluetooth,android-sharedpreferences,Android,Bluetooth,Sharedpreferences,Android Bluetooth,Android Sharedpreferences,我目前正在编写一个android应用程序来控制蓝牙设备。我试图使用Gson和Json类将ArrayList保存到SharedReferences,但当我尝试在手机上运行这段代码时,我无法获取每个设备的名称,但可以获取它们的地址。我在Android监视器日志(使用Android Studio)中看到此错误: 03-28 16:12:51.70617640-17640/?E/Bluetooth设备:未启用BT。 无法获取远程设备名称 我使用自定义类来管理设备列表,以下是相关代码: public cl

我目前正在编写一个android应用程序来控制蓝牙设备。我试图使用Gson和Json类将
ArrayList
保存到
SharedReferences
,但当我尝试在手机上运行这段代码时,我无法获取每个设备的名称,但可以获取它们的地址。我在Android监视器日志(使用Android Studio)中看到此错误:

03-28 16:12:51.70617640-17640/?E/Bluetooth设备:未启用BT。 无法获取远程设备名称

我使用自定义类来管理设备列表,以下是相关代码:

public class DeviceList {

private ArrayList<BluetoothDevice> mDeviceList_bt;
private ArrayList<String> mDeviceList_s;

private ArrayAdapter mAdapter;

private String PREFS_NAME;

SharedPreferences mPrefs;
SharedPreferences.Editor mEditor;

private int i = 0;

public DeviceList(Context c, String Prefs_Name){

    this.PREFS_NAME = Prefs_Name;

    mDeviceList_bt = new ArrayList<BluetoothDevice>();
    mDeviceList_s = new ArrayList<String>();

    mPrefs = PreferenceManager.getDefaultSharedPreferences(c);
    mEditor = mPrefs.edit();

    mAdapter = new ArrayAdapter(c, android.R.layout.simple_list_item_1, mDeviceList_s);
}

public void readFromSharedPrefs(){

    Gson gson = new Gson();
    String json = mPrefs.getString(PREFS_NAME, null);
    Type type = new TypeToken<ArrayList<BluetoothDevice>>() {}.getType();
    mDeviceList_bt = gson.fromJson(json, type);

    for (BluetoothDevice device : mDeviceList_bt) {
        mDeviceList_s.add(device.getName() + "\n" + device.getAddress());
    }

    mAdapter.notifyDataSetChanged();
}

public void saveToSharedPrefs(){
    Gson gson = new Gson();
    String json = gson.toJson(mDeviceList_bt); // myObject - instance of MyObject
    mEditor.putString(PREFS_NAME, json);
    mEditor.commit();
}
我从
SharedReferences
中阅读后得到:

null
8C:DE:52:FA:96:0A

null
00:3E:01:00:47:4E

null
8C:DE:52:FA:B5:F0
知道我做错了什么吗


提前谢谢你

设备名称未保存在Bluetooth设备中。而是将名称缓存在BluetoothService中:

/**
 * Get the friendly Bluetooth name of the remote device.
 *
 * <p>The local adapter will automatically retrieve remote names when
 * performing a device scan, and will cache them. This method just returns
 * the name for this device from the cache.
 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
 *
 * @return the Bluetooth name, or null if there was a problem.
 */
    public String getName() {
    if (sService == null) {
        Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
        return null;
    }
    try {
        return sService.getRemoteName(this);
    } catch (RemoteException e) {Log.e(TAG, "", e);}
    return null;
}
以获得正确初始化的设备


请注意,您可能会遇到问题,因为iOS和Android设备可能会在一段时间后更改其MAC地址。

谢谢,请多多帮助!
/**
 * Get the friendly Bluetooth name of the remote device.
 *
 * <p>The local adapter will automatically retrieve remote names when
 * performing a device scan, and will cache them. This method just returns
 * the name for this device from the cache.
 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
 *
 * @return the Bluetooth name, or null if there was a problem.
 */
    public String getName() {
    if (sService == null) {
        Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
        return null;
    }
    try {
        return sService.getRemoteName(this);
    } catch (RemoteException e) {Log.e(TAG, "", e);}
    return null;
}
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);