Rene
Rene

Reputation: 74

Callback method only get called when debugger is attached

I'm writing an Android app which connects to an BLE device and shows the end result of the measurement. I'm using a library of the company for communicating with the device and the problem I have is that the callback functions (onMeasurementFinished,onMeasurementFailed, etc.) only get called if I set a breakpoint and run the app with a debugger attached.

Here is how I connect to the device:

@Override
    public void onClick(View view) {
        try {
            if (view.getId() == R.id.btnConnect) {
                deviceArm.scan(this, this);
            }
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

This is the callback of the scan that is working:

 @Override
    public void onDeviceConnected() {
        txtResults.setText("Connected");
        deviceArm.startMeasurement(this);
    }

These are the callbacks of startMeasurement that only work if I debug:

@Override
public void onMeasurementError(Error error) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            txtResults.setText("Error");
        }
    });
}

@Override
public void onMeasurementFinished(MeasurementType measurementType, final Object o) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
           txtResults.setText("Finished"+o.toString());
        }
    });
}

@Override
public void onMeasurementStarted() {

}

@Override
public void onMeasurementProgress(final MeasurementType measurementType, final Object o) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            txtResults.setText(o.toString());
        }
    });
}

I know it's not much information but I can only hope that mabey some of you have an idea what the problem could be.

Upvotes: 0

Views: 973

Answers (1)

N. Smith
N. Smith

Reputation: 235

What's likely happening is the peripheral device is doing some extra setup after connection. So putting a breakpoints in onDeviceConnected gives it time to do this.

As you say, not much information to go on, but it's possible the peripheral is sending the measurement results via notification/indication. So the peripheral needs time to configure these before starting measurments.

Try adding a delay in onDeviceConnected before starting the measurment. Ideally your library will have a callback along the lines of onDeviceReady, that you could use instead.

Upvotes: 1

Related Questions