Android 14 nordic DFU library

hello,

I'm try to fit my application to Android 14.

my application implement nordic DFU library ( for nrf52833 running  freeRTOS firmware SDK 16 ) version 2.5.0

while running DFU the process run but crash and the complition:

dfu run method:

    private void runDfuOnDevice(BluetoothDevice device) {
DfuServiceInitiator.createDfuNotificationChannel(getActivity().getApplicationContext());
mDfustarter = new DfuServiceInitiator(device.getAddress())
.setDeviceName(device.getName())
.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true)
.setKeepBond(false);
//mDfustarter.setPrepareDataObjectDelay(300L);

mDfustarter.setZip(mPath+"/"+mChosenFile);
mDfustarter.setScope(DfuServiceInitiator.SCOPE_APPLICATION);

UIhandler.post(new Runnable() {
@Override
public void run() {
mDfuProgressText.setText(getString(R.string.dfu_starting));
}
});

mDfustarter.start(getActivity(), DfuService.class);
}

and getting:

E FATAL EXCEPTION: main
Process: com.application, PID: 6846
android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{f55800f u0 com.application/.DfuService}
at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2273)
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2244)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2538)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at android.app.ActivityThread.main(ActivityThread.java:8671)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:565)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
Caused by: android.app.StackTrace: Last startServiceCommon() call for this service was made here
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1963)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1917)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:841)
at no.nordicsemi.android.dfu.DfuServiceInitiator.start(SourceFile:876)
at com.application.feature.DeviceDFUFrag.runDfuOnDevice(SourceFile:399)
at com.application.feature.DeviceDFUFrag.access$2000(SourceFile:73)
at com.application.feature.DeviceDFUFrag$10.run(SourceFile:511)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)

same code worked on previous android versions and  previous dfu library version

Parents Reply Children
  • Are you saying there are some difficulties with complying with the new requirements in Android 14, and you remove startForeground() because of that?

  • I didn't modified the DFU library, I just set "false" to initiator to run in the foreground .

    if I won't do that it will compile but give the FATAL EXCEPTION as in the original post right after the DFU is completed.

  • Do you mean with DfuServiceInitiator.setForeground(false)?

    Can you check with a debugger that when the service is run in the foreground, these happen?

    1. startAsForegroundService is true in DfuServiceController.start()
      https://github.com/NordicSemiconductor/Android-DFU-Library/blob/v.2.0.1/lib_dfu/src/main/java/no/nordicsemi/android/dfu/DfuServiceInitiator.java#L802 
    2. startForeground() is called in onHandleIntent() in your subclass of DfuBaseService.
      https://github.com/NordicSemiconductor/Android-DFU-Library/blob/v.2.0.1/lib_dfu/src/main/java/no/nordicsemi/android/dfu/DfuBaseService.java#L1142
  • when initiating DFU:

    1. I can see error on start dfu:

    2024-06-25 13:34:24.072 20592-20715 DfuBaseService com.application E Service cannot be started in foreground
    java.lang.SecurityException: Permission Denial: startForeground from pid=20592, uid=10091 requires android.permission.FOREGROUND_SERVICE
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
    at android.os.Parcel.createException(Parcel.java:3041)
    at android.os.Parcel.readException(Parcel.java:3024)
    at android.os.Parcel.readException(Parcel.java:2966)
    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7282)
    at android.app.Service.startForeground(Service.java:775)
    at no.nordicsemi.android.dfu.DfuBaseService.startForeground(SourceFile:1926)
    at no.nordicsemi.android.dfu.DfuBaseService.onHandleIntent(SourceFile:1153)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:77)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:222)
    at android.os.Looper.loop(Looper.java:314)
    at android.os.HandlerThread.run(HandlerThread.java:67)
    Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:6371)
    at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2149)
    at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1737)
    at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:14033)
    at android.app.IActivityManager$Stub.onTransact$setServiceForeground$(IActivityManager.java:12784)

    but I have the foreground service permission in the manifest

    2. at the end of the DFU i'm getting the fatal exception fron the DFUInitiationController for StartForgroundService() mechod.

    3. also using the 2.5.0 DFU version my device is correpted after finish the second DFU ( tested OK on 1.1.0 DFU version):

    a. flashing using JTAG NRF52833 with full application image ( no code change there)
    b. run DFU first time all ok .
    c. run DFU second time firmwar get corrupted and need to be JTAG again

  • asaf said:

    1. I can see error on start dfu:

    2024-06-25 13:34:24.072 20592-20715 DfuBaseService com.application E Service cannot be started in foreground
    java.lang.SecurityException: Permission Denial: startForeground from pid=20592, uid=10091 requires android.permission.FOREGROUND_SERVICE

    Can you share the manifest of the app?

    asaf said:
    2. at the end of the DFU i'm getting the fatal exception fron the DFUInitiationController for StartForgroundService() mechod.

    I am confused. If you get an exception when you start DFU, how do you get to the end of DFU?

    Are you saying this attempt is when you use background DFU and somehow still get an exception in a foreground method?

    asaf said:

    3. also using the 2.5.0 DFU version my device is correpted after finish the second DFU ( tested OK on 1.1.0 DFU version):

    a. flashing using JTAG NRF52833 with full application image ( no code change there)
    b. run DFU first time all ok .
    c. run DFU second time firmwar get corrupted and need to be JTAG again

    Do you know how it is corrupted? Have you gotten any logs from the bootloader?

Related