Using data listeners

To retrieve data from a sensor in the asynchronous mode, implement a DataListener:

class MyDataListener implements DataListener {
    public void dataReceived(SensorConnection aConnection, Data[] aData, boolean aIsDataLost) {
        // Store received data
    }
}

The DataListener.dataReceived method receives a similar Data object array as the getData output. It also receives the SensorConnection from which the data was received and a boolean flag indicating whether data was lost between this and the previous data delivery.

Data listening is started by calling the SensorConnection.setDataListener method and stopped by calling the SensorConnection.removeDataListener method. Each SensorConnection can have only one DataListener. If setDataListener is called when a SensorConnection already has a registered DataListener, the previous listener is replaced and the data listening parameters are updated.

MyDataListener datalistener = new MyDataListener();

int bufferSize = 10;
sensorConnection.setDataListener(dataListener, bufferSize);

// ...

int bufferingPeriod = 1000;
boolean includeTimestamps = true;
boolean includeUncertainities = false;
boolean includeValidities = false;
sensorConnection.setDataListener(dataListener,
                                 bufferSize,
                                 bufferingPeriod,
                                 includeTimestamps,
                                 includeUnvertainities,
                                 includeValities);

// ...

sensorConnection.removeDataListener();

The implemented dataReceived method should return quickly, so it should only store the received data and signal some other thread to do the more time-consuming processing.

For a comparison of data retrieval methods, see section Choosing the listening method.