From a6b372cd73d471287938c95eac5e44d564c08871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giovanni=20Ortu=C3=B1o?= Date: Tue, 2 Dec 2014 13:20:37 -0800 Subject: [PATCH 1/2] Different toasts depending on the error --- .../org/uribeacon/beacon/ConfigUriBeacon.java | 2 ++ .../org/uribeacon/sample/ConfigActivity.java | 28 +++++++++++-------- .../src/main/res/values/strings.xml | 4 +++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/android-uribeacon/uribeacon-library/src/main/java/org/uribeacon/beacon/ConfigUriBeacon.java b/android-uribeacon/uribeacon-library/src/main/java/org/uribeacon/beacon/ConfigUriBeacon.java index 7db90f9..6278d1c 100644 --- a/android-uribeacon/uribeacon-library/src/main/java/org/uribeacon/beacon/ConfigUriBeacon.java +++ b/android-uribeacon/uribeacon-library/src/main/java/org/uribeacon/beacon/ConfigUriBeacon.java @@ -20,6 +20,8 @@ public class ConfigUriBeacon extends UriBeacon { private static final String TAG = ConfigUriBeacon.class.getCanonicalName(); + // This error should be defined in the BluetoothGatt library but it isn't. + public static final int INSUFFICIENT_AUTHORIZATION = 8; public static final int PERIOD_NONE = -1; public static final byte POWER_MODE_NONE = -1; public static final byte POWER_MODE_ULTRA_LOW = 0; diff --git a/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java b/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java index 50108c3..c5721e7 100644 --- a/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java +++ b/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java @@ -71,22 +71,28 @@ public class ConfigActivity extends Activity implements PasswordDialogFragment.P private final UriBeaconCallback mUriBeaconCallback = new UriBeaconCallback() { @Override public void onUriBeaconRead(ConfigUriBeacon configUriBeacon, int status) { - checkRequest(status); - updateInputFields(configUriBeacon); + if (status == BluetoothGatt.GATT_SUCCESS) { + updateInputFields(configUriBeacon); + } else if (status == BluetoothGatt.GATT_FAILURE) { + Toast.makeText(ConfigActivity.this, R.string.failed_to_read, Toast.LENGTH_SHORT).show(); + finish(); + } else { + Toast.makeText(ConfigActivity.this, getString(R.string.failed_with_code) + status, Toast.LENGTH_SHORT).show(); + finish(); + } } @Override public void onUriBeaconWrite(int status) { - checkRequest(status); - mUriBeaconConfig.closeUriBeacon(); - finish(); - } - - private void checkRequest(int status) { - if (status == BluetoothGatt.GATT_FAILURE) { - Toast.makeText(ConfigActivity.this, "Failed to update the beacon", Toast.LENGTH_SHORT) - .show(); + if (status == BluetoothGatt.GATT_SUCCESS) { + mUriBeaconConfig.closeUriBeacon(); finish(); + } else if (status == BluetoothGatt.GATT_FAILURE) { + Toast.makeText(ConfigActivity.this, R.string.failed_to_write, Toast.LENGTH_SHORT).show(); + } else if (status == ConfigUriBeacon.INSUFFICIENT_AUTHORIZATION) { + Toast.makeText(ConfigActivity.this, R.string.wrong_password, Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(ConfigActivity.this, getString(R.string.failed_with_code) + status, Toast.LENGTH_SHORT).show(); } } }; diff --git a/android-uribeacon/uribeacon-sample/src/main/res/values/strings.xml b/android-uribeacon/uribeacon-sample/src/main/res/values/strings.xml index b961dfe..51431bb 100644 --- a/android-uribeacon/uribeacon-sample/src/main/res/values/strings.xml +++ b/android-uribeacon/uribeacon-sample/src/main/res/values/strings.xml @@ -62,4 +62,8 @@ Could not reset UriBeacon to its default values Please enter a password Passwords don\'t match + Failed to read beacon + Failed with code: + Wrong password please try again... + Failed to update beacon From 470ddffaa78755b065c91d0a39bed71dff2021f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giovanni=20Ortu=C3=B1o?= Date: Tue, 2 Dec 2014 13:39:50 -0800 Subject: [PATCH 2/2] Enable ui if passwords fails --- .../org/uribeacon/sample/ConfigActivity.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java b/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java index c5721e7..7a595c4 100644 --- a/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java +++ b/android-uribeacon/uribeacon-sample/src/main/java/org/uribeacon/sample/ConfigActivity.java @@ -94,21 +94,21 @@ public void onUriBeaconWrite(int status) { } else { Toast.makeText(ConfigActivity.this, getString(R.string.failed_with_code) + status, Toast.LENGTH_SHORT).show(); } + enableUi(true); } }; - private void blockUi() { - mSchema.setEnabled(false); - mUriValue.setEnabled(false); - mFlagsValue.setEnabled(false); + private void enableUi(boolean block) { + mSchema.setEnabled(block); + mUriValue.setEnabled(block); + mFlagsValue.setEnabled(block); for (EditText txCal : mAdvertisedTxPowerLevels) { - txCal.setEnabled(false); + txCal.setEnabled(block); } - mTxPowerMode.setEnabled(false); - mBeaconPeriod.setEnabled(false); - mLockState.setEnabled(false); + mTxPowerMode.setEnabled(block); + mBeaconPeriod.setEnabled(block); + mLockState.setEnabled(block); } - public void saveConfigBeacon(MenuItem menu) { try { if (mUriBeaconConfig.getVersion().equals(ProtocolV2.CONFIG_SERVICE_UUID)) { @@ -120,7 +120,7 @@ public void saveConfigBeacon(MenuItem menu) { } } else { - blockUi(); + enableUi(false); ConfigUriBeacon configUriBeacon = new ConfigUriBeacon.Builder() .uriString(mUriValue.getText().toString()) .txPowerLevel(DEFAULT_TX_POWER) @@ -142,6 +142,7 @@ public void onResetClicked(MenuItem menu) { } private void resetConfigBeacon(byte[] key) { try { + enableUi(false); ConfigUriBeacon configUriBeacon = new ConfigUriBeacon.Builder() .key(key) .reset(true) @@ -152,7 +153,7 @@ private void resetConfigBeacon(byte[] key) { } } private void writeUriBeaconV2(byte[] key) throws URISyntaxException { - blockUi(); + enableUi(false); ConfigUriBeacon.Builder builder = new ConfigUriBeacon.Builder() .key(key) .lockState(mLockState.isChecked())