IPv6 data reception callback

This mechanism for reading IPv6 data does not block your application. Instead, you can be notified when new IPv6 data has been received if you have subscribed or registered with the IPv6 data reception service by using the addIPDataListener(IIPDataReceiveListener) method.

Network data reception registration

import com.digi.xbee.api.ThreadDevice;
import com.digi.xbee.api.listeners.IIPDataReceiveListener;

[...]

// Instantiate a Thread device object.
ThreadDevice myDevice = new ThreadDevice("COM1", 9600);
myDevice.open();

// Subscribe to IPv6 data reception.
myDevice.addIPDataListener(new MyIPDataReceiveListener());

[...]

The listener provided to the subscribed method, MyIPDataReceiveListener, must implement the IIPDataReceiveListener interface. This interface includes the method that executes when a new IP data is received by the XBee device.

When new IP data is received, the ipDataReceived() method of the IIPDataReceiveListener is executed providing as parameter an IPMessage object which contains the data and other useful information.

IPDataReceiveListener implementation example

import java.net.Inet6Address;
import com.digi.xbee.api.listeners.IIPDataReceiveListener;
import com.digi.xbee.api.models.IPMessage;

public class MyIPDataReceiveListener implements IIPDataReceiveListener {	 
     /*
     * IP data reception callback.
     */   
     @Override
     public void ipDataReceived(IPMessage ipMessage) {
          Inet6Address destAddr = ipMessage.getIPv6Address();
          IPProtocol protocol = ipMessage.getProtocol();    
          int srcPort = ipMessage.getSourcePort();
          int destPort = ipMessage.getDestPort();   
          String dataString = ipMessage.getDataString();
          System.out.println("Received IPv6 data from " + destAddr + ": " + dataString);
     }
}

The IPMessage object provides the following information:

You can retrieve the previous information using the corresponding getters of the IPMessage object.

To stop listening to new received IP data, use the removeIPDataListener(IIPDataReceiveListener) method to unsubscribe the already-registered listener.

Data reception deregistration

[...]

ThreadDevice myDevice = ...
MyIPDataReceiveListener myipDataReceiveListener = ...

myDevice.addIPDataListener(myIPDataReceiveListener);

[...]

// Remove the IP data reception listener.
myDevice.removeIPDataListener(myIPDataReceiveListener);

[...]

Example: Receive IPv6 data with listener

The XBee Java Library includes a sample application that demonstrates how to receive IPv6 data using the listener. You can locate the example in the following path:

/examples/communication/ip/ReceiveIPv6DataSample