Skip to content

Commit

Permalink
Merge pull request #53 from NordicSemiconductor/migration/ble-lib-2.3.1
Browse files Browse the repository at this point in the history
Migration to BLE lib 2.3.1
  • Loading branch information
philips77 authored Oct 4, 2021
2 parents 8a5ffa4 + bc7d0f3 commit 4a8cd09
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
7 changes: 2 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ 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'

// 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'
}
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
android:name=".SplashScreenActivity"
android:theme="@style/AppTheme.SplashScreen"
android:noHistory="true"
android:launchMode="singleTop">
android:launchMode="singleTop"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand All @@ -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">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="no.nordicsemi.android.nrftoolbox.LAUNCHER"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,16 @@ 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;
return supported;
}

@Override
protected void onDeviceDisconnected() {
protected void onServicesInvalidated() {
buttonCharacteristic = null;
ledCharacteristic = null;
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -47,7 +51,7 @@ public BlinkyViewModel(@NonNull final Application application) {
}

public LiveData<ConnectionState> getConnectionState() {
return blinkyManager.getState();
return blinkyManager.state;
}

public LiveData<Boolean> getButtonState() {
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}

/**
Expand All @@ -108,8 +117,6 @@ public void setLedState(final boolean on) {
@Override
protected void onCleared() {
super.onCleared();
if (blinkyManager.isConnected()) {
disconnect();
}
disconnect();
}
}

0 comments on commit 4a8cd09

Please sign in to comment.