Using data listeners

Data from a sensor can be retrieved in an asynchronous mode by implementing a DataListener interface.

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

DataListener.dataReceived() gets 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 method SensorConnection.setDataListener() and stopped by calling the SensorConnection.removeDataListener() method. Each SensorConnection can have only one DataListener. If setDataListener() is called when SensorConnection already has a registered DataListener, the previous listener will be replaced and 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.