Help Center

Local Navigation

Use a USB or serial port connection

Using a serial or USB connection, BlackBerry® device applications can communicate with desktop applications when they are connected to a computer using a serial or USB port. This type of connection also lets BlackBerry device applications communicate with a peripheral device that plugs into the serial or USB port.

  1. Import the following classes:
    • javax.microedition.io.Connector
    • java.io.DataOutputStream
    • java.lang.String
    • java.io.DataInputStream
  2. Import the javax.microedition.io.StreamConnection interface.
  3. Invoke Connector.open(), and specify comm as the protocol and COM1 or USB as the port to open a USB or serial port connection, .
    private StreamConnection _conn = (StreamConnection)Connector.open(
    "comm:COM1;baudrate=9600;bitsperchar=8;parity=none;stopbits=1");
    
  4. To send data on the USB or serial port connection, invoke openDataOutputStream() or openOutputStream().
    DataOutputStream _dout = _conn.openDataOutputStream();
    
  5. Use the write methods on the output stream to write data.
    private String data = "This is a test";
    _dout.writeChars(data);
    
  6. To receive data on the USB or serial port connection, use a non-main event thread to read data from the input stream. Invoke openInputStream() or openDataInputStream().
    DataInputStream _din = _conn.openInputStream();
    Use the read methods on the input stream to read data.
    
  7. Use the read methods on the input stream to read data.
    String contents = _din.readUTF();
    
  8. To close the USB or serial port connection, invoke close() on the input and output streams, and on the port connection object. The close() method can throw IOExceptions. Make sure the BlackBerry device application implements exception handling.
    _din.close();
    _dout.close();
    conn.close();
    
Previous topic: Use a datagram connection

Was this information helpful? Send us your comments.