Data reception callback

This second mechanism to read data does not block your application. Instead, you can be notified when new data has been received if you are subscribed or registered to the data reception service using the addDataListener(IDataReceiveListener) method with a data reception listener as parameter.

Data reception registration
import com.digi.xbee.api.XBeeDevice;
[...]

// Instantiate an XBee device object.
XBeeDevice myXBeeDevice = new XBeeDevice("COM1", 9600);
myXBeeDevice.open();

// Create the data reception listener.
MyDataReceiveListener myDataReceiveListener = ...

// Subscribe to data reception.
myXBeeDevice.addDataListener(myDataReceiveListener);

[...]

The listener that is provided to the subscribe method, MyDataReceiveListener, must implement the IDataReceiveListener interface. This interface includes the method executed when new data is received by the XBee device.

It does not matter which type of local XBee device you have instanced, as this data reception operation is implemented in the same way for all the local XBee device classes that support the receive data mechanism.

When new data is received, the dataReceived() method of the IDataReceiveListener is executed providing an XBeeMessage object as a parameter, which contains the data and other useful information.

IDataReceiveListener implementation example, MyDataReceiveListener
import com.digi.xbee.api.listeners.IDataReceiveListener;

public class MyDataReceiveListener implements IDataReceiveListener {
	/*
	* Data reception callback.
	*/
	@Override
	public void dataReceived(XBeeMessage xbeeMessage) {	
		String address = xbeeMessage.getDevice().get64BitAddress().toString();
		String dataString = xbeeMessage.getDataString();
		System.out.println("Received data from " + address +
				": " + dataString);	
	}
}

The XBeeMessage object provides the following information:

You can retrieve the previous information using the corresponding getters of the XBeeMessage object:

Get the XBeeMessage information
[...]
 

public class MyDataReceiveListener implements IDataReceiveListener {

    /*

     * Data reception callback.

     */

    @Override

    public void dataReceived(XBeeMessage xbeeMessage) {

        XBee64BitAddress address = xbeeMessage.getDevice().get64BitAddress();

        byte[] data = xbeeMessage.getData();

        boolean isBroadcast = xbeeMessage.isBroadcast();

    }

}

 
[...]

To stop listening to new received data, use the removeDataListener(IDataReceiveListener) method to unsubscribe the already registered listener.

Data reception deregistration
[...]

XBeeDevice myXBeeDevice = ...
MyDataReceiveListener myDataReceiveListener = ...

myXBeeDevice.addDataListener(myDataReceiveListener);

[...]

// Remove the new data reception listener.
myXBeeDevice.removeDataListener(myDataReceiveListener);

[...]
Data reception callback example

The XBee Java Library includes a sample application that shows you how to subscribe to the data reception service to receive data. The example is located in the following path:

/examples/communication/ReceiveDataSample