Task returned from timer.scheduleAsync faulted

edited March 15 in Android
Hi, 
I'm trying to schedule a task that reads the RSSI every 500 ms.

timer = mwBoard.getModule(Timer.class);
timer.scheduleAsync(500, true, new CodeBlock() {
@Override
public void program() {
mwBoard.readRssiAsync().onSuccess(task -> {...})
return null;
}
}).continueWith(task -> {
if (task.isFaulted()){
Log.i(LOG_TAG,"Error : " + task.getError());
}
scheduledTask = task.getResult();
Log.i(LOG_TAG,String.valueOf(task.getResult().id()));
return null;
});

But the task returned is always failed and the Error is:
Error : java.lang.NullPointerException: Attempt to read from null array

What i'm doing wrong?

And another question: Do you think that this is a good way to monitorize the RSSI? 
I've read about the posibility of doing it with the method Handler.post(readRSSI)
being readRSSI a runable code like that:

Runnable readRSSI = new Runnable() {
@Override
public void run() {
mwBoard.readRssiAsync()...
handler.postDelayed(readRSSI, 500);
}
}

Or do you think there is a better way to do that?

Thank you.

Comments

  • Please post the full stack trace not just the error message.

    The CodeBlock interface is used to wrap MetaWear commands which readRssiAsync is not.  You'll need to schedule reads at the Android level which can be done with a Handler object as you outlined.
  • Ok, thanks for the answer, I've changed the code, and now i'm using the Handler class, this is the code:
    Runnable readRSSI = new Runnable() {
    @Override
    public void run() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mwBoard != null && mwBoard.isConnected() && mBluetoothAdapter.isEnabled()) {
    Log.i(LOG_TAG, "connection still active");
    handler.postDelayed(readRSSI, 500);

    //Read RSSI
    mwBoard.readRssiAsync().onSuccess(task -> {
    if (task != null && task.isCompleted() && !task.isFaulted() && !task.isCancelled()){
    Log.i(LOG_TAG,"RSSI leido de la placa : " + task.getResult());
    sendBroadcastMessage(task.getResult().toString());
    }
    return null;
    });
    } else {
    Log.i(LOG_TAG, "connection lost");
    handler.removeCallbacks(readRSSI);
    stopSelf();
    }
    }
    };
  • edited March 16
    Now is working fine for 10 to 30 seconds, but sudendly the app crash and I have this error:

                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'bolts.Task bolts.TaskCompletionSource.getTask()' on a null object reference
                                                                               at com.mbientlab.metawear.android.BtleService$AndroidPlatform.readRssiAsync(BtleService.java:580)
                                                                               at com.mbientlab.metawear.impl.JseMetaWearBoard.readRssiAsync(JseMetaWearBoard.java:723)
                                                                               at es.udc.adriangonzalez.tea.RSSIService$1.run(RSSIService.java:150)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:155)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

    RSSIService.java:150 is this line in the code that I have pasted before:
     mwBoard.readRssiAsync().onSuccess(task -> {

    This is the first time working with tasks for me, so I really appreciate some help, thank you very much.

    (Sorry for the double post, the message was too long)
  • Hrm, that seems like a bug in the APi with how it is managing the rssi task.
  • edited March 20
    Oh ok, thanks for your answer. I'll wait for the solution :)

    Edit: one more question: what units is using the method readRssiasync()?
  • RSSI values, as reported on Android devices, are in dBm
  • edited March 23
    I've tried the new 3.0.17 version, but I have the same error:


    03-23 11:22:37.678 19358-19358/es.udc.adriangonzalez.tea E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: es.udc.adriangonzalez.tea, PID: 19358
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'bolts.Task bolts.TaskCompletionSource.getTask()' on a null object reference
                                                                                   at com.mbientlab.metawear.android.BtleService$AndroidPlatform.readRssiAsync(BtleService.java:580)
                                                                                   at com.mbientlab.metawear.impl.JseMetaWearBoard.readRssiAsync(JseMetaWearBoard.java:723)
                                                                                   at es.udc.adriangonzalez.tea.RSSIService$1.run(RSSIService.java:170)
                                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:155)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)


    Maybe i'm doing something wrong?
    I'm executing the same code that i've pasted before.

    Thank you very much for the quick version update.
  • You need to rebuild the project.
  • Oh! I've used the "sync" button and the "make project" button but I haven't used the "Rebuild" button.

    Now it's working.

    Thank you very much for your patience, and answers to questions that are non-related with metawear.
Sign In or Register to comment.