Skip to content

Commit

Permalink
Migration to BLE library 2.2.0 (#71)
Browse files Browse the repository at this point in the history
* Ensuring handler can execute
When callback is called from BLE thread or any other without the Looper, it would just print an error message.

* Dark Theme improvements

* Migrating to BLE Library 2.2.0

* Improved error handling

* Android Studio 3.6.3

* FS and Image View Models migrated to new API
  • Loading branch information
philips77 authored May 15, 2020
1 parent d8a238a commit d213898
Show file tree
Hide file tree
Showing 54 changed files with 490 additions and 263 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

buildscript {
ext.kotlin_version = '1.3.71'
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
Expand Down
2 changes: 1 addition & 1 deletion mcumgr-ble/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

dependencies {
// Import the BLE Library
api 'no.nordicsemi.android:ble:2.0.3'
api 'no.nordicsemi.android:ble:2.2.0'

// Logging
implementation 'org.slf4j:slf4j-api:1.7.30'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.runtime.mcumgr.exception.McuMgrTimeoutException;
import io.runtime.mcumgr.response.McuMgrResponse;
import no.nordicsemi.android.ble.BleManager;
import no.nordicsemi.android.ble.BleManagerCallbacks;
import no.nordicsemi.android.ble.Request;
import no.nordicsemi.android.ble.annotation.ConnectionPriority;
import no.nordicsemi.android.ble.callback.FailCallback;
Expand All @@ -56,8 +55,8 @@
* existing BLE implementation, you may simply implement {@link McuMgrTransport} or use this class
* to perform your BLE actions by calling {@link BleManager#enqueue(Request)}.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class McuMgrBleTransport extends BleManager<BleManagerCallbacks> implements McuMgrTransport {
@SuppressWarnings("unused")
public class McuMgrBleTransport extends BleManager implements McuMgrTransport {

private static final Logger LOG = LoggerFactory.getLogger(McuMgrBleTransport.class);

Expand Down Expand Up @@ -110,8 +109,6 @@ public class McuMgrBleTransport extends BleManager<BleManagerCallbacks> implemen
public McuMgrBleTransport(@NonNull Context context, @NonNull BluetoothDevice device) {
super(context);
mDevice = device;
// By default, the callbacks will ignore all calls to it
setGattCallbacks(new McuMgrBleCallbacksStub());
}

/**
Expand All @@ -128,7 +125,7 @@ public BluetoothDevice getBluetoothDevice() {
@NonNull
@Override
protected BleManagerGattCallback getGattCallback() {
return mGattCallback;
return new McuMgrGattCallback();
}

/**
Expand Down Expand Up @@ -436,6 +433,7 @@ public void onRequestFailed(@NonNull BluetoothDevice device, int status) {

@Override
public void release() {
cancelQueue();
disconnect().enqueue();
}

Expand Down Expand Up @@ -477,7 +475,7 @@ public void onRequestCompleted(@NonNull BluetoothDevice device) {
// Ble Manager Callbacks
//*******************************************************************************************

private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
private class McuMgrGattCallback extends BleManagerGattCallback {

// Determines whether the device supports the SMP Service
@Override
Expand Down Expand Up @@ -532,9 +530,15 @@ public void onRequestFailed(@NonNull final BluetoothDevice device, final int sta
protected void onDeviceDisconnected() {
mSmpService = null;
mSmpCharacteristic = null;
notifyDisconnected();

runOnCallbackThread(new Runnable() {
@Override
public void run() {
notifyDisconnected();
}
});
}
};
}

//*******************************************************************************************
// Manager Connection Observers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public void run() {

if (remainingTime > 0) {
LOG.trace("Waiting for estimated swap time {}ms", mEstimatedSwapTime);
new Handler().postDelayed(reconnect, remainingTime);
new Handler(Looper.getMainLooper()).postDelayed(reconnect, remainingTime);
} else {
reconnect.run();
}
Expand Down
10 changes: 5 additions & 5 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0-alpha03'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
implementation 'androidx.appcompat:appcompat:1.2.0-beta01'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha03'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'com.google.android.material:material:1.2.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta5'
implementation 'com.google.android.material:material:1.2.0-alpha06'

// Lifecycle extensions
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
Expand All @@ -58,7 +58,7 @@ dependencies {
annotationProcessor 'com.google.dagger:dagger-android-processor:2.27'

// Brings the new BluetoothLeScanner API to older platforms
implementation 'no.nordicsemi.android.support.v18:scanner:1.1.0'
implementation 'no.nordicsemi.android.support.v18:scanner:1.4.3'

// Timber & SLF4J
implementation 'com.arcao:slf4j-timber:3.1@aar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@
import android.bluetooth.BluetoothDevice;
import android.content.Context;

import javax.inject.Named;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import javax.inject.Named;

import dagger.Module;
import dagger.Provides;
import io.runtime.mcumgr.McuMgrTransport;
import io.runtime.mcumgr.ble.McuMgrBleTransport;
import io.runtime.mcumgr.sample.di.McuMgrScope;
import io.runtime.mcumgr.sample.observable.ObservableMcuMgrBleTransport;

@Module
public class McuMgrTransportModule {

@Provides
@Named("busy")
@McuMgrScope
@NonNull
static MutableLiveData<Boolean> provideBusyStateLiveData() {
return new MutableLiveData<>();
}

@Provides
@McuMgrScope
@NonNull
static McuMgrTransport provideMcuMgrTransport(@NonNull final Context context,
@NonNull final BluetoothDevice device) {
return new McuMgrBleTransport(context, device);
return new ObservableMcuMgrBleTransport(context, device);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,44 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mViewModel.getConnectionState().observe(getViewLifecycleOwner(),
resId -> mConnectionStatus.setText(resId));
mViewModel.getBondState().observe(getViewLifecycleOwner(), resId ->
mBondingStatus.setText(resId));
mViewModel.getConnectionState().observe(getViewLifecycleOwner(), state -> {
switch (state) {
case CONNECTING:
mConnectionStatus.setText(R.string.status_connecting);
break;
case INITIALIZING:
mConnectionStatus.setText(R.string.status_initializing);
break;
case READY:
mConnectionStatus.setText(R.string.status_connected);
break;
case DISCONNECTING:
mConnectionStatus.setText(R.string.status_disconnecting);
break;
case DISCONNECTED:
mConnectionStatus.setText(R.string.status_disconnected);
break;
case TIMEOUT:
mConnectionStatus.setText(R.string.status_connection_timeout);
break;
case NOT_SUPPORTED:
mConnectionStatus.setText(R.string.status_not_supported);
break;
}
});
mViewModel.getBondState().observe(getViewLifecycleOwner(), state -> {
switch (state) {
case NOT_BONDED:
mBondingStatus.setText(R.string.status_not_bonded);
break;
case BONDING:
mBondingStatus.setText(R.string.status_bonding);
break;
case BONDED:
mBondingStatus.setText(R.string.status_bonded);
break;
}
});
mViewModel.getBusyState().observe(getViewLifecycleOwner(), busy ->
mWorkIndicator.setVisibility(busy ? View.VISIBLE : View.GONE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private void printError(@Nullable final String error) {
if (error != null) {
final SpannableString spannable = new SpannableString(error);
spannable.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(requireContext(), R.color.error)),
ContextCompat.getColor(requireContext(), R.color.colorError)),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected void onFileLoadingFailed(final int error) {
private void printError(@NonNull final String error) {
final SpannableString spannable = new SpannableString(error);
spannable.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(requireContext(), R.color.error)),
ContextCompat.getColor(requireContext(), R.color.colorError)),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void printError(@Nullable final String error) {
if (error != null) {
final SpannableString spannable = new SpannableString(error);
spannable.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(requireContext(), R.color.error)),
ContextCompat.getColor(requireContext(), R.color.colorError)),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected void onFileLoadingFailed(final int error) {
private void printError(@NonNull final String error) {
final SpannableString spannable = new SpannableString(error);
spannable.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(requireContext(), R.color.error)),
ContextCompat.getColor(requireContext(), R.color.colorError)),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected void onFileLoadingFailed(final int error) {
private void printError(@NonNull final String error) {
final SpannableString spannable = new SpannableString(error);
spannable.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(requireContext(), R.color.error)),
ContextCompat.getColor(requireContext(), R.color.colorError)),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -107,8 +108,9 @@ private void printStats(@NonNull final List<McuMgrStatResponse> responses) {
private void printError(@Nullable final String error) {
if (error != null) {
final SpannableString spannable = new SpannableString(error);
TypedValue typedValue = new TypedValue();
spannable.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(requireContext(), R.color.error)),
ContextCompat.getColor(requireContext(), R.color.colorError)),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD),
0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.runtime.mcumgr.sample.observable;

public enum BondingState {
NOT_BONDED,
BONDING,
BONDED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.runtime.mcumgr.sample.observable;

public enum ConnectionState {
CONNECTING,
INITIALIZING,
READY,
DISCONNECTING,
DISCONNECTED,
TIMEOUT,
NOT_SUPPORTED
}
Loading

0 comments on commit d213898

Please sign in to comment.