I am writing a Java program using the Java API (xbee-java-library-1.3.0.jar) that will connect a XBee2/3 to a SmartThings Hub. I have been following the packet layout listed in https://nzfalco.jimdofree.com/electronic-projects/xbee-to-smartthings/.
I am running into an interesting problem. After sending several packets in the sequence I stop getting called by the ExplicitDataListener. I see in logging I am receiving the packet but even with a break point the callback never gets hit. This seems to be a timing related issue since it happens at different points of the sequence.
Example, I receive the packet (shown here in logging):
[Thread-1] DEBUG com.digi.xbee.api.DataReader - [COM6 - 9600/8/N/1/N] Packet received:
Packet: 7E001791286D97000206071B000001080000010411001E000100DA
Start delimiter: 7E
Length: 00 17 (23)
Frame type: 91 (Explicit RX Indicator)
64-bit source address: 28 6D 97 00 02 06 07 1B
16-bit source address: 00 00
Source endpoint: 01
Dest. endpoint: 08
Cluster ID: 00 00
Profile ID: 01 04
Receive options: 11
RF data: 00 1E 00 01 00
Checksum: DA
But the handler never gets called with the ExplicitXBeeMessage.
Here is the (incomplete) listener:
@Override
public synchronized void explicitDataReceived(ExplicitXBeeMessage explicitXBeeMessage) {
synchronized (this.myDevice) {
// this.myDevice.addExplicitDataListener(this);
final int clusterID = explicitXBeeMessage.getClusterID();
final int profileID = explicitXBeeMessage.getProfileID();
System.out.println("explicitDataReceived Profile " + profileID + ", Cluster " + clusterID);
try {
switch (profileID) {
case 0x0000:
switch (clusterID) {
case ZigbeeMessage.ACTIVE_ENDPOINT_REQUEST:
System.out.println("Received ACTIVE_ENDPOINT_REQUEST");
ActiveEndpointsResponse.create(explicitXBeeMessage, (byte) 0x00, (byte) 0x01, (byte) 0x08)
.send(this.myDevice);
break;
case ZigbeeMessage.SIMPLE_DESCRIPTOR_REQUEST:
System.out.println("Received SIMPLE_DESCRIPTOR_REQUEST");
SimpleDescriptorResponse
.create(explicitXBeeMessage, (byte) 0x00, (byte) 0x08, 0x0104, 0x0002, (byte) 0x30)
.clusterIn(0x0000).clusterIn(0x0003).clusterIn(0x0006).send(this.myDevice);
break;
default:
System.out.println("Undefined Profile " + profileID + ", Cluster " + clusterID);
break;
}
break;
default:
System.out.println("Undefined Profile " + profileID + ", Cluster " + clusterID);
break;
}
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
One possibility I see is that the receiver runs on a thread pool. I have been using the Listener method to create and send response packets. Should I have a multi-thread Finite State Machine such that the listener simply records the event and then notifies the foreground thread to send the response? It’s a lot more work but I’m running out of ideas?
Note, the examples I’ve seen in the Java API are all very simplistic, sending only a few packets before exiting. It would be nice to have a full protocol engine there.