Program 2
RadiostreamConnection conn = (RadiostreamConnection) Connector.open("radio://0014.4F01.0000.0007:100");
DataInputStream dis = conn.openDataInputStream(); DataOutputStream dos = conn.openDataOutputStream(); try {
String question = dis.readUTF();
if (question.equals("Hello up there")) { dos.writeUTF("Hello down there");
}else { dos.writeUTF("What???"); dos.flush();
}
} catch (NoRouteException e) {
System.out.println ("No route to 0014.4F01.0000.0007"); } finally {
dis.close();
dos.close();
conn.close();
}
Data is sent over the air when the output stream buffer is full or when a flush() is issued.
The NoRouteException is thrown if no route to the destination can be determined. The stream accesses themselves are fully blocking - that is, the dis.readUTF() call will wait forever (unless a timeout for the connection has been specified – see below).
Behind the scenes, every data transmission between the two devices involves an acknowledgement. The sending stream will always wait until the
Another exception that you may see, which applies to both radiostream and radiogram protocols, is ChannelBusyException. This exception indicates that the radio channel was busy when the SPOT tried to send a radio packet. The normal handling is to catch the exception and retry the send.
The radiogram protocol
The radiogram protocol is a
To open a server connection do:
RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:<portNo>");
where portNo is a port number in the range 0 to 255 that identifies this particular connection. The connection is opened using the default radio channel and default PAN Id (currently channel 26, PAN 3). The section Radio properties shows how to override these defaults.
To open a client connection do:
RadiogramConnection conn = (RadiogramConnection)Connector.open("radiogram://<serveraddr>:<portNo>");
31