Troubleshooting guide
128
BlackBerry Java Development Environment Development Guide
Using port connections
Using a serial or USB connection, BlackBerry device ® applications can communicate with computer applications
when they are connected to a computer using a serial or USB port. This type of connection also lets applications
communicate with a peripheral device that plugs into the serial or USB port.
Use USB or serial port connections
Use Bluetooth serial port connections
You can use the Bluetooth® API (net.rim.device.api.bluetooth) to let your application access the Bluetooth
Serial Port Profile and initiate a server or client Bluetooth serial port connection to a computer or other Bluetooth
enabled device.
Task Steps
Open a USB or serial port connection. >Invoke Connector.open(), specifying comm as the protocol and COM1 or USB as the port
type.
private StreamConnection _conn = (StreamConnection)Connector.open(
"comm:COM1;baudrate=9600;bitsperchar=8;parity=none;stopbits=1");
Send data on the USB or serial port
connection.
1. Invoke openDataOutputStream() or openOutputStream().
DataOutputStream _dout = _conn.openDataOutputStream();
2. Use the write methods on the output stream to write data.
private String data = "This is a test";
_dout.writeChars(data);
Receive data on the USB or serial port
connection.
Use a non-main event thread to read data from the input stream.
1. Invoke openInputStream() or openDataInputStream().
DataInputStream _din = _conn.openInputStream();
2. Use the read methods on the input stream to read data.
String contents = _din.readUTF();
Close the USB or serial port connection. 1. Invoke close() on the input and output streams and on the port connection object.
2. The close() method can throw IOExceptions. Make sure that the application implements
exception handling.
_din.close();
_dout.close();
conn.close();
Task Steps
Open a Bluetooth connection. >Invoke Connector.open(), providing the serial port information that
BluetoothSerialPort.getSerialPortInfo() returns as a parameter.
BluetoothSerialPortInfo[] info =
BluetoothSerialPort.getSerialPortInfo();
StreamConnection _bluetoothConnection =
(StreamConnection)Connector.open( info[0].toString(),
Connector.READ_WRITE );