Bug fixed: releasing broadcast receiver, waiting longer for ACL_DISCONNECTED

This commit is contained in:
Aleksander Nowakowski 2016-09-14 18:01:51 +02:00
parent adef02a6ec
commit c1455e2b6f
3 changed files with 18 additions and 27 deletions

View File

@ -36,7 +36,6 @@ import android.content.IntentFilter;
import android.os.Build;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.UUID;
@ -49,6 +48,8 @@ import no.nordicsemi.android.dfu.internal.exception.UploadAbortedException;
/* package */ abstract class BaseDfuImpl implements DfuService {
public static final String TAG = "DfuImpl";
public static final boolean DEBUG = true; // TODO change me to false!
protected static final UUID GENERIC_ATTRIBUTE_SERVICE_UUID = new UUID(0x0000180100001000L, 0x800000805F9B34FBL);
protected static final UUID SERVICE_CHANGED_UUID = new UUID(0x00002A0500001000L, 0x800000805F9B34FBL);
protected static final UUID CLIENT_CHARACTERISTIC_CONFIG = new UUID(0x0000290200001000L, 0x800000805f9b34fbL);
@ -218,7 +219,8 @@ import no.nordicsemi.android.dfu.internal.exception.UploadAbortedException;
service.registerReceiver(mBondStateBroadcastReceiver, bondFilter);
}
/* package */ void onDestroy() {
@Override
public void release() {
mService.unregisterReceiver(mBondStateBroadcastReceiver);
mService = null;
}
@ -679,17 +681,17 @@ import no.nordicsemi.android.dfu.internal.exception.UploadAbortedException;
}
protected void logw(final String message) {
if (BuildConfig.DEBUG)
if (DEBUG)
Log.w(TAG, message);
}
protected void logi(final String message) {
if (BuildConfig.DEBUG)
if (DEBUG)
Log.i(TAG, message);
}
protected void logd(final String message) {
if (BuildConfig.DEBUG)
if (DEBUG)
Log.d(TAG, message);
}
}

View File

@ -743,10 +743,6 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
unregisterReceiver(mDfuActionReceiver);
unregisterReceiver(mConnectionStateBroadcastReceiver);
if (mDfuServiceImpl != null) {
mDfuServiceImpl.onDestroy();
mDfuServiceImpl = null;
}
}
@Override
@ -891,6 +887,8 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
// Wait a second... If we were connected before it's good to give some time before we start reconnecting.
waitFor(1000);
// Looks like a second is not enough. The ACL_DISCONNECTED broadcast sometimes comes later (on Android 7.0)
waitFor(1000);
/*
* Now let's connect to the device.
@ -956,11 +954,12 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
// Reset the attempt counter
intent.putExtra(EXTRA_ATTEMPT, 0);
DfuService dfuService = null;
try {
/*
* Device services were discovered. Based on them we may now choose the implementation.
*/
final DfuService dfuService = mDfuServiceImpl = DfuServiceProvider.getDfuImpl(this, gatt);
dfuService = mDfuServiceImpl = DfuServiceProvider.getDfuImpl(this, gatt);
if (dfuService == null || !dfuService.hasRequiredService(gatt)) {
sendLogBroadcast(LOG_LEVEL_WARNING, "Connected. DFU Service not found");
terminateConnection(gatt, ERROR_SERVICE_NOT_FOUND);
@ -981,15 +980,6 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
} catch (final UploadAbortedException e) {
logi("Upload aborted");
sendLogBroadcast(LOG_LEVEL_WARNING, "Upload aborted");
// if (mConnectionState == STATE_CONNECTED_AND_READY)
// try {
// mAborted = false;
// logi("Sending Reset command (Op Code = 6)");
// writeOpCode(gatt, controlPointCharacteristic, OP_CODE_RESET);
// sendLogBroadcast(LOG_LEVEL_APPLICATION, "Reset request sent");
// } catch (final Exception e1) {
// // do nothing
// }
terminateConnection(gatt, PROGRESS_ABORTED);
} catch (final DeviceDisconnectedException e) {
sendLogBroadcast(LOG_LEVEL_ERROR, "Device has disconnected");
@ -1008,15 +998,11 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
sendLogBroadcast(LOG_LEVEL_ERROR, String.format("Error (0x%02X): %s", error, GattError.parse(error)));
}
loge(e.getMessage());
// if (mConnectionState == STATE_CONNECTED_AND_READY)
// try {
// logi("Sending Reset command (Op Code = 6)");
// writeOpCode(gatt, controlPointCharacteristic, OP_CODE_RESET);
// sendLogBroadcast(LOG_LEVEL_APPLICATION, "Reset request sent");
// } catch (final Exception e1) {
// // do nothing
// }
terminateConnection(gatt, e.getErrorNumber() /* we return the whole error number, including the error type mask */);
} finally {
if (dfuService != null) {
dfuService.release();
}
}
} finally {
try {

View File

@ -55,4 +55,7 @@ import no.nordicsemi.android.dfu.internal.exception.UploadAbortedException;
/** Performs the DFU process. */
void performDfu(final Intent intent) throws DfuException, DeviceDisconnectedException, UploadAbortedException;
/** Releases the service. */
void release();
}