How to properly send a message from Arduino to Java using Xbee-S2

Hello everyone. I am currently testing out the Digi Xbee Java library and working through the examples. I’ve been able to send data from the Router xbee (located on pc) to the Coordinator xbee (located on arduino) and succesfully turned on/off an led, this was done in Java. Now I am trying to send a message from the arduino side to the pc side and display the message in my java app. However, I have not had any luck. I’m pretty sure the problem is within my arduino code and if someone could let me know what I am doing wrong I would be grateful.

The following is my Arduino code, It’s purpose is just to send out messages.


SoftwareSerial XBee(2, 3); // Arduino RX, TX (XBee Dout, Din)

void setup(){
  pinMode(13, OUTPUT);

void loop(){
  digitalWrite(13, HIGH);
  XBee.print("Hello, can you hear me");
  digitalWrite(13, LOW);

The java files are the examples located in the communication section of the api and the links to them are as follows:

Listener class:

Your code seems to be fine. Please change arduino RX,TX pins to (0,1) and try as these are default pins.

Please provide some more information about the hardware you are using for establishing communication between Arduino and XBee module like Arduino XBee sheild.

Thanks for taking a look at my code. I am using a Sunfounder uno board with the Sparkfun Xbee shield, the coordinator xbee is located on this shield. My uno board is powered by a 9v power supply. On the PC side I have the sparkfun xbee explorer usb connected with my router xbee.

I would like my Java application to receive a string sent by the coordinator xbee on the uno board. The application is able to send broadcast messages so I can send out data.

My XBee’s are also in API mode.


Please paste the following code in main method of java class and check what you are getting in the console.
try {;
myXBeeNetwork = myDevice.getNetwork();
myXBeeNetwork.addDiscoveryListener(new DiscoveryProcess());
myDevice.addDataListener(new MyDataReceiveListener());
>> Discovering remote XBee devices…");
devicesList = myXBeeNetwork.getDevices();
for(int i = 0 ; i < devicesList.size(); i++){
RemoteXBeeDevice xbee = new RemoteXBeeDevice(myDevice,devicesList.get(i).get64BitAddress());
myDevice.sendData(xbee, data.getBytes());
} catch (XBeeException e) {

Please let me know wether you are getting coordinator address on console or not.

I entered the code you provided and this was the result:

WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2

>> Discovering remote XBee devices…
>> Device discovered: 0013A20040CAE669 -
>> Discovery process finished successfully.

The address I obtained from your code is my coordinator address. I’ve also been trying to implement the XBee arduino library since I am trying to communicate in API mode.

Hi ,
The code which you are using for Arduino is AT mode. To establish communication in API mode using XBee-arduino library, refer the following link :

Sorry for the late response and thank you very much for the help you have provided me. I have been looking over that library but I am still having trouble sending a response to the Java app. The following is my arduino code:


XBee xbee = XBee();

uint8_t payload[] = {‘H’,‘I’};
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40CAA93E);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));

void setup() {


void loop() {


pinMode(13, HIGH);

pinMode(13, LOW);

I am using the same listener code that I originally posted.

Please I have the same problem how I must configure my xbee beceause in the xbee arduino library the xbees must be configured in AP=2 and for the xbee java library the xbees must be configured in AP=1 please any help ?