Java Reference
In-Depth Information
// we need to create a new datagram that can hold 4 bytes
Datagram respDatagram = datagramConnection.newDatagram (4);
// receives the datagram containing the actual time
datagramConnection.receive (respDatagram);
// in order to convert the 4 bytes that are
// received to a long we need the following lines
byte[] received = respDatagram.getData();
long time = (((((long) received [0]) & 0x0ff) << 24)
+ ((((long) received [1]) & 0x0ff) << 16)
+ ((((long) received [2]) & 0x0ff) << 8)
+ ((((long) received [3]) & 0x0ff)));
// Convert seconds since 1.1.2000 to Milliseconds since 1.1.1970
time = (time - 2208988800l) * 1000;
// calculate deviation
long difference = time - System.currentTimeMillis();
// closes the datagram connection
datagramConnection.close();
}
catch (IOException e) {
// Handle the exception that occurred while opening a datagram
connection
}
Serial Connections
Some devices running a CLDC KVM might be equipped with a serial port and support the protocol for
serial connections. In order to transfer data over the serial port, the device needs to be connected to a
desktop computer or to another device that supports serial communication—for example, a GPS
receiver. In order to test serial communication, a simple terminal can be used.
PDAP specifies that a PDAP implementation can support the CommConnection if supported by the
underlying operating system and hardware. Such a CommConnection can be established as shown in
the following code snippet that opens a 9600 baud communication connection on comm port 0 with 8
bits per character and no parity
For example,
CommConnection connection =
(CommConnection) Connector.open
("comm:0;baudrate=9600;bitsperchar=8;parity=none");
The actual serial port properties depend on the device's hardware. A comma-separated list of all
available comm ports can be obtained from the system property "microedition.commports" using the
System.getProperty() method. These comm ports might not only include physical RS-232
ports, but also IR or Bluetooth ports that are mapped to serial ports. Commonly supported parameters
are
• Number of the comport: comm=0..n or IR=0..n
• baudrate=57600, 38400, 19200, 9600, 2400, 1200
• bitsperchar=8, 7
• parity=even, odd, none
Search WWH ::




Custom Search