Troubleshooting guide
129
7: Creating connections
Code sample: Listening for data on the serial port and rendering the data when it arrives
Example: BluetoothSerialPortDemo.java
/**
* BluetoothSerialPortDemo.java
* Copyright (C) 2004-2005 Research In Motion Limited.
*/
/* The client side of a simple serial port demonstration application.
* This application listens for text on the serial port and
* renders the data when it arrives.
Send data on the Bluetooth connection. 1. Invoke openDataOutputStream() or openOutputStream().
DataOutputStream _dout =
_bluetoothConnection.openDataOutputStream();
2. Use the write methods on the output stream to write data.
private static final int JUST_OPEN = 4;
_dout.writeInt(JUST_OPEN);
Receive data on the Bluetooth
connection.
1. Create a non-main event thread to read data from the input stream.
2. Invoke openInputStream() or openDataInputStream().
DataInputStream _din = _bluetoothConnection.openDataInputStream();
3. Use the read methods on the input stream to read the data.
String contents = _din.readUTF();
Close the Bluetooth connection. 1. Invoke close() on the input and output streams and on the Bluetooth serial port connection
object.
2. The close() method can throw IOExceptions. Make sure that the application implements
exception handling.
if (_bluetoothConnection != null) {
try {
_bluetoothConnection.close();
} catch(IOException ioe) {
}
}
if (_din != null) {
try {
_din.close();
} catch(IOException ioe) {
}
}
if (_dout != null) {
try {
_dout.close();
} catch(IOException ioe) {
}
}
_bluetoothConnection = null;
_din = null;
_dout = null;
Task Steps