Motorola i88s manual Implementation Notes, Example using StreamConnection

Page 20

Motorola Inc.

i88s J2ME Developers’ Guide

3.Get the input stream using the openInputStream() method of InputConnection.

4.Read and write data onto those streams.

5.Close the port using the close() method of both the Connection and open Streams.

Once the connection has been established, simply use the normal methods of any input or output stream to read and write data. The openInputStream and openOutputStream methods of StreamConnection are similar to the methods of the socket StreamConnection.

Example using StreamConnection

Connector.open is used to open the serial port and a StreamConnection is returned. From the StreamConnection the InputStream and OutputStream are opened. It is used to read and write every character until the connection is closed(-1). If an exception is thrown the connection and stream are closed.

StreamConnection sc = null;

InputStream is = null;

OutputStream os = null;

/*

*Create the parameter String with options specified

*/

String parameter = "comm:0;baudrate=19200;parity=n;databits=8;stopbits=1;flowcontrol=n/n”;

try{

sc = (StreamConnection)Connector.open(parameter, Connector.READ_WRITE, false);

os = sc.openOutputStream(); is = sc.openInputStream();

int ch;

while ((ch = is.read() ) != -1) { os.write(ch);

}

} finally {

if (sc != null) sc.close();

if(is != null) is.close();

if(os != null) os.close();

}

2.3Implementation Notes

As stated in the previous sections, the i88s phone supports a vast array of networking options. The networking options however are limited by both memory and bandwidth, which place hard restrictions on the applications. These limitations manifest themselves mainly in

Version 1.0 - Page 20

Image 20
Contents I88s Multi-Communication Device J2ME Developers’ Guide J2ME Networking Document Overview DisclaimerAcronyms Used In This Guide Terminology Definition Contact InformationAcronyms and Definitions Agps On The i88s Phone OverviewAssist Data Position APIAccuracy Example PositionConnection ClassAlmanac Out Of Date Method Descriptions Retrieving Position in JavaGetPosition GetPositionString nameUsing Position in Java GetStatusExample Using PositionConnection Positionresponsenoalmanacoverride Addition, to obtain better accurate speed and direction Recommendation J2ME Networking Http HttpsClass Descriptions Supported Protocols on the i88s Phone HttpHttps ServerSockets TCP SocketsSSL Secure Sockets Serial Port Access UDP SocketsConnection Optional Parameters Communicating on a PortExample using StreamConnection Implementation NotesTips Concurrent Connections For The i88s Phone Protocol MaximumMethod Descriptions Opening a file File I/OClass Description Code Examples Deleting a fileReading and Writing Example # 2 Complete MIDlet code Motorola Inc I88s J2ME Developers’ Guide Os = sc.openOutputStream //get InputStream Sc.close Caveats Motorola Inc I88s J2ME Developers’ Guide
Related manuals
Manual 43 pages 17.94 Kb