Skip to content

Commit

Permalink
Store device password in SharedPreferences.
Browse files Browse the repository at this point in the history
This way, the scale has to be paired only once.
  • Loading branch information
maksverver committed Oct 7, 2018
1 parent 827dbce commit ce615c5
Showing 1 changed file with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;

import com.health.openscale.R;
Expand Down Expand Up @@ -73,21 +75,32 @@ public class BluetoothTrisaBodyAnalyze extends BluetoothCommunication {
*/
private static final int BROADCAST_ID = 0;

/** Hardware address (i.e., Bluetooth mac) of the connected device. */
@Nullable
private String hwAddress;

/**
* Device password as a 32-bit integer, or {@code null} if the device password is unknown.
* Prefix for {@link SharedPreferences} keys that store device passwords.
*
* <p>TODO: store this is in a database.</p>
* @see #loadDevicePassword
* @see #saveDevicePassword
*/
private static final String SHARED_PREFERENCES_PASSWORD_KEY_PREFIX =
"trisa_body_analyze_password_for_device_";

/**
* ASCII string that identifies the connected device (i.e. the hex-encoded Bluetooth MAC
* address). Used in shared preference keys to store per-device settings.
*/
@Nullable
private String deviceId;

/** Device password as a 32-bit integer, or {@code null} if the device password is unknown. */
@Nullable
private static Integer password;

/**
* Indicates whether we are pairing. If this is {@code true} then we have written the
* set-broadcast-id command, and should disconnect after the write succeeds.
*
* @see #onPasswordReceived
* @see #nextBluetoothCmd
*/
private boolean pairing = false;

Expand All @@ -103,8 +116,9 @@ public String driverName() {
@Override
public void connect(String hwAddress) {
Timber.i("connect(\"%s\")", hwAddress);
this.hwAddress = hwAddress;
super.connect(hwAddress);
this.deviceId = hwAddress;
this.password = loadDevicePassword(context, hwAddress);
}

@Override
Expand Down Expand Up @@ -205,12 +219,13 @@ private void onPasswordReceived(byte[] data) {
Timber.e("Password data too short");
return;
}
int newPassword = getInt32(data, 1);
if (password != null && password != newPassword) {
Timber.w("Replacing old password '%08x'", password);
password = getInt32(data, 1);
if (deviceId == null) {
Timber.e("Can't save password: device id not set!");
} else {
Timber.i("Saving password '%08x' for device id '%s'", password, deviceId);
saveDevicePassword(context, deviceId, password);
}
Timber.i("Storing password '%08x'", newPassword);
password = newPassword;

sendMessage(R.string.trisa_scale_pairing_succeeded, null);

Expand Down Expand Up @@ -312,4 +327,27 @@ private static double getBase10Float(byte[] data, int offset) {
int exponent = data[offset + 3]; // note: byte is signed.
return mantissa * Math.pow(10, exponent);
}

private static String getDevicePasswordKey(String deviceId) {
return SHARED_PREFERENCES_PASSWORD_KEY_PREFIX + deviceId;
}

@Nullable
private static Integer loadDevicePassword(Context context, String deviceId) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = getDevicePasswordKey(deviceId);
try {
// Strictly speaking, there is a race condition between the calls to contains() and
// getInt(), but it's not a problem because we never delete passwords.
return prefs.contains(key) ? Integer.valueOf(prefs.getInt(key, 0)) : null;
} catch (ClassCastException e) {
Timber.e(e, "Password preference value is not an integer.");
return null;
}
}

private static void saveDevicePassword(Context context, String deviceId, int password) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt(getDevicePasswordKey(deviceId), password).apply();
}
}

0 comments on commit ce615c5

Please sign in to comment.