From a8274a367a48b94e53fad11199cdbb465fba9e5e Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Fri, 1 Oct 2021 14:25:46 +0200 Subject: [PATCH 1/2] Migration to BLE Library 2.3.1 --- app/build.gradle | 5 +---- .../android/blinky/BlinkyActivity.java | 5 +++-- .../android/blinky/profile/BlinkyManager.java | 14 +++++++------ .../blinky/profile/data/BlinkyLED.java | 5 +++++ .../blinky/viewmodels/BlinkyViewModel.java | 21 ++++++++++++------- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 30dcdbd4..49c70be4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,8 +46,5 @@ dependencies { implementation 'no.nordicsemi.android:log:2.3.0' // BLE library - implementation 'no.nordicsemi.android:ble-livedata:2.2.4' - // To add BLE Library as a module, replace the above dependency with the following - // and uncomment 2 lines in settings.gradle file. - // implementation project(":ble-livedata") + implementation 'no.nordicsemi.android:ble-livedata:2.3.1' } diff --git a/app/src/main/java/no/nordicsemi/android/blinky/BlinkyActivity.java b/app/src/main/java/no/nordicsemi/android/blinky/BlinkyActivity.java index 1e6fd216..cc9b7937 100644 --- a/app/src/main/java/no/nordicsemi/android/blinky/BlinkyActivity.java +++ b/app/src/main/java/no/nordicsemi/android/blinky/BlinkyActivity.java @@ -31,6 +31,7 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.ViewModelProvider; import no.nordicsemi.android.ble.livedata.state.ConnectionState; +import no.nordicsemi.android.ble.observer.ConnectionObserver; import no.nordicsemi.android.blinky.adapter.DiscoveredBluetoothDevice; import no.nordicsemi.android.blinky.databinding.ActivityBlinkyBinding; import no.nordicsemi.android.blinky.viewmodels.BlinkyViewModel; @@ -88,9 +89,9 @@ protected void onCreate(final Bundle savedInstanceState) { binding.deviceContainer.setVisibility(View.GONE); binding.progressContainer.setVisibility(View.GONE); final ConnectionState.Disconnected stateWithReason = (ConnectionState.Disconnected) state; - if (stateWithReason.isNotSupported()) { + if (stateWithReason.getReason() == ConnectionObserver.REASON_NOT_SUPPORTED) { binding.infoNotSupported.container.setVisibility(View.VISIBLE); - } else if (stateWithReason.isTimeout()) { + } else { binding.infoTimeout.container.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/no/nordicsemi/android/blinky/profile/BlinkyManager.java b/app/src/main/java/no/nordicsemi/android/blinky/profile/BlinkyManager.java index 297c5950..7926c4c4 100644 --- a/app/src/main/java/no/nordicsemi/android/blinky/profile/BlinkyManager.java +++ b/app/src/main/java/no/nordicsemi/android/blinky/profile/BlinkyManager.java @@ -176,8 +176,8 @@ public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { boolean writeRequest = false; if (ledCharacteristic != null) { - final int rxProperties = ledCharacteristic.getProperties(); - writeRequest = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0; + final int ledProperties = ledCharacteristic.getProperties(); + writeRequest = (ledProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0; } supported = buttonCharacteristic != null && ledCharacteristic != null && writeRequest; @@ -185,7 +185,7 @@ public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { } @Override - protected void onDeviceDisconnected() { + protected void onServicesInvalidated() { buttonCharacteristic = null; ledCharacteristic = null; } @@ -206,8 +206,10 @@ public void turnLed(final boolean on) { return; log(Log.VERBOSE, "Turning LED " + (on ? "ON" : "OFF") + "..."); - writeCharacteristic(ledCharacteristic, - on ? BlinkyLED.turnOn() : BlinkyLED.turnOff()) - .with(ledCallback).enqueue(); + writeCharacteristic( + ledCharacteristic, + BlinkyLED.turn(on), + BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT + ).with(ledCallback).enqueue(); } } diff --git a/app/src/main/java/no/nordicsemi/android/blinky/profile/data/BlinkyLED.java b/app/src/main/java/no/nordicsemi/android/blinky/profile/data/BlinkyLED.java index 94ed5ea2..473689d3 100644 --- a/app/src/main/java/no/nordicsemi/android/blinky/profile/data/BlinkyLED.java +++ b/app/src/main/java/no/nordicsemi/android/blinky/profile/data/BlinkyLED.java @@ -30,6 +30,11 @@ public final class BlinkyLED { private static final byte STATE_OFF = 0x00; private static final byte STATE_ON = 0x01; + @NonNull + public static Data turn(final boolean on) { + return on ? turnOn() : turnOff(); + } + @NonNull public static Data turnOn() { return Data.opCode(STATE_ON); diff --git a/app/src/main/java/no/nordicsemi/android/blinky/viewmodels/BlinkyViewModel.java b/app/src/main/java/no/nordicsemi/android/blinky/viewmodels/BlinkyViewModel.java index 6156ff14..63f18e05 100644 --- a/app/src/main/java/no/nordicsemi/android/blinky/viewmodels/BlinkyViewModel.java +++ b/app/src/main/java/no/nordicsemi/android/blinky/viewmodels/BlinkyViewModel.java @@ -26,9 +26,11 @@ import android.bluetooth.BluetoothDevice; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; +import no.nordicsemi.android.ble.ConnectRequest; import no.nordicsemi.android.ble.livedata.state.ConnectionState; import no.nordicsemi.android.blinky.adapter.DiscoveredBluetoothDevice; import no.nordicsemi.android.blinky.profile.BlinkyManager; @@ -38,6 +40,8 @@ public class BlinkyViewModel extends AndroidViewModel { private final BlinkyManager blinkyManager; private BluetoothDevice device; + @Nullable + private ConnectRequest connectRequest; public BlinkyViewModel(@NonNull final Application application) { super(application); @@ -47,7 +51,7 @@ public BlinkyViewModel(@NonNull final Application application) { } public LiveData getConnectionState() { - return blinkyManager.getState(); + return blinkyManager.state; } public LiveData getButtonState() { @@ -81,10 +85,11 @@ public void connect(@NonNull final DiscoveredBluetoothDevice target) { */ public void reconnect() { if (device != null) { - blinkyManager.connect(device) + connectRequest = blinkyManager.connect(device) .retry(3, 100) .useAutoConnect(false) - .enqueue(); + .then(d -> connectRequest = null); + connectRequest.enqueue(); } } @@ -93,7 +98,11 @@ public void reconnect() { */ private void disconnect() { device = null; - blinkyManager.disconnect().enqueue(); + if (connectRequest != null) { + connectRequest.cancelPendingConnection(); + } else if (blinkyManager.isConnected()) { + blinkyManager.disconnect().enqueue(); + } } /** @@ -108,8 +117,6 @@ public void setLedState(final boolean on) { @Override protected void onCleared() { super.onCleared(); - if (blinkyManager.isConnected()) { - disconnect(); - } + disconnect(); } } From bc7d0f362f470923b1dc05c69c2eed9310bb954e Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Mon, 4 Oct 2021 14:13:17 +0200 Subject: [PATCH 2/2] Dependencies updated --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 49c70be4..643f75c0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,7 +40,7 @@ dependencies { implementation 'com.google.android.material:material:1.4.0' // Brings the new BluetoothLeScanner API to older platforms - implementation 'no.nordicsemi.android.support.v18:scanner:1.5.0' + implementation 'no.nordicsemi.android.support.v18:scanner:1.5.1' // Log Bluetooth LE events in nRF Logger implementation 'no.nordicsemi.android:log:2.3.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e2712933..c67dfd5e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -47,7 +47,8 @@ android:name=".SplashScreenActivity" android:theme="@style/AppTheme.SplashScreen" android:noHistory="true" - android:launchMode="singleTop"> + android:launchMode="singleTop" + android:exported="true"> @@ -57,7 +58,8 @@ android:name=".ScannerActivity" android:icon="@drawable/ic_blinky_feature" android:label="@string/feature_name" - android:launchMode="singleTop"> + android:launchMode="singleTop" + android:exported="true">