private void persistCandidateDiscoveredDevices() { // TODO I don't think the MULTI_PROCESS flag is necessary SharedPreferences.Editor editor = getContext().getSharedPreferences( DeviceManager.KNOWN_BLUETOOTH_DEVICE_PREFERENCES, Context.MODE_MULTI_PROCESS).edit(); Set<String> candidates = new HashSet<String>(); for(Map.Entry<String, String> device : mDiscoveredDevices.entrySet()) { if(device.getValue().toUpperCase().startsWith( BluetoothVehicleInterface.DEVICE_NAME_PREFIX)) { candidates.add(device.getKey()); } } SupportSettingsUtils.putStringSet(editor, DeviceManager.KNOWN_BLUETOOTH_DEVICE_PREF_KEY, candidates); editor.commit(); } }
/** * * Store a set in SharedPreferences, using the built-in putStringSet method * if available and falling back to a comma separated String if not. * * * Note that this is a simple approach that won't work if the values in the * set contain commas. * * @param editor the editor to store the set in. * @param key the key for the preference. * @param value the value to store with the key. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static void putStringSet(SharedPreferences.Editor editor, String key, Set<String> value) { if(supportsStringSet()) { editor.putStringSet(key, value); } else { editor.putString(key, Joiner.on(",").join(value)); } } }
public Set<BluetoothDevice> getCandidateDevices() { Set<BluetoothDevice> candidates = new HashSet<>(); for (BluetoothDevice device : getPairedDevices()) { if (device.getName().startsWith( BluetoothVehicleInterface.DEVICE_NAME_PREFIX)) { candidates.add(device); } } SharedPreferences preferences = mContext.getSharedPreferences(KNOWN_BLUETOOTH_DEVICE_PREFERENCES, Context.MODE_MULTI_PROCESS); Set<String> detectedDevices = SupportSettingsUtils.getStringSet( preferences, KNOWN_BLUETOOTH_DEVICE_PREF_KEY, new HashSet<String>()); for (String address : detectedDevices) { if (BluetoothAdapter.checkBluetoothAddress(address)) { candidates.add(getDefaultAdapter().getRemoteDevice(address)); } } for (BluetoothDevice candidate : candidates) { Log.d(TAG, "Found previously discovered or paired OpenXC BT VI " + candidate.getAddress()); } return candidates; }
String key, Set<String> defaultValue) { Set<String> result = defaultValue; if(supportsStringSet()) { result = preferences.getStringSet(key, defaultValue); } else {