Non-zero on onConnectionStateChange status(133) on Task fault
I have this class that should be connecting to the metawear board and passing incoming data via callback to my activity. My issue is that the Task faults at seemingly random times. The sensors will work fine maybe half the time but after a couple restarts it just faults. This is pretty much the same code as the docs. Does board.teardown() not clean up all resources?
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
// Typecast the binder to the service's LocalBinder class
serviceBinder = (BtleService.LocalBinder) service;
retrieveBoard();
// ---------------------------------------------------------------------------------------------
board.connectAsync().onSuccessTask(new Continuation<Void, Task<Route>>() {
@Override
public Task<Route> then(Task<Void> task) throws Exception {
accelerometer = board.getModule(Accelerometer.class);
accelerometer.configure()
.odr(25f) // Set sampling frequency to 25Hz, or closest valid ODR
.commit();
// Adds a Stream route to access the accelerometer module features
return accelerometer.acceleration().addRouteAsync(new RouteBuilder() {
@Override
public void configure(RouteComponent source) {
source.stream(new Subscriber() {
@Override
public void apply(Data data, Object... env) {
// When data is received update the accelerator text field
float x = data.value(Acceleration.class).x();
float y = data.value(Acceleration.class).y();
float z = data.value(Acceleration.class).z();
// do something
}
}
});
}
});
}
}).continueWith(new Continuation<Route, Void>() {
@Override
public Void then(Task<Route> task) throws Exception {
if(task.isFaulted()){
// Task Failed
Exception error = task.getError();
error.getMessage();
} else {
// Task Worked
accelerometer.acceleration().start();
accelerometer.start();
}
return null;
}
});
// ---------------------------------------------------------------------------------------------
// Gyro
// ---------------------------------------------------------------------------------------------
board.connectAsync().onSuccessTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
gyroBmi160 = board.getModule(GyroBmi160.class);
gyroBmi160.configure()
.odr(GyroBmi160.OutputDataRate.ODR_50_HZ)
.range(GyroBmi160.Range.FSR_2000)
.commit();
// Adds a Stream route to access the gyro module features
return gyroBmi160.angularVelocity().addRouteAsync(new RouteBuilder() {
@Override
public void configure(RouteComponent source) {
source.stream(new Subscriber() {
@Override
public void apply(Data data, Object ... env) {
// When data is received update the gyro text field
float x = data.value(AngularVelocity.class).x();
float y = data.value(AngularVelocity.class).y();
float z = data.value(AngularVelocity.class).z();
// do something
}
});
}
}).continueWith(new Continuation<Route, Void>() {
@Override
public Void then(Task<Route> task) throws Exception {
if(task.isFaulted()){
// Task Failed
Exception error = task.getError();
} else {
// Task Worked
gyroBmi160.angularVelocity().start();
gyroBmi160.start();
}
return null;
}
});
}
});
// ---------------------------------------------------------------------------------------------
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
board.tearDown();
}
public void startSensors(){
accelerometer.acceleration().start();
accelerometer.start();
gyroBmi160.angularVelocity().start();
gyroBmi160.start();
}
public void stop(){
accelerometer.acceleration().stop();
accelerometer.stop();
gyroBmi160.angularVelocity().stop();
gyroBmi160.stop();
board.tearDown();
}
public void setBoardListener( OnMetaBoardDataObserved listener){
this.mDataObserved = listener;
}
}``
Comments
Should I retrieve the Accelerometer and Gyro data in the same task? I'm very very new to Android so I'm unsure about how to handle retries?
Chain async tasks. There is an example of this in the free fall tutorial and read the documentation for the Task object.
This isn't an Android specific problem. Again, examples of this are in the tutorial projects.