?? chatapplication.java
字號:
package example.chat;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.LocalDevice;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ChatApplication extends MIDlet {
/*-
* ===============================================
* Bluetooth Chat Application for Multiple Clients
* ===============================================
*
* This example application is a chat application. The application
* forms a net of chatters via bluetooth connections. An bluetooth
* connection is formed to each chatter, i.e, no centralized server
* exists.
*
* The application has two threads: client thread and server thread.
* The client thread, or discovery thread to be specific, searches
* for new devices. A service search is performed to each found
* device. If the chat service is found a connection is constructed
* to device. The server thread waits for new connections. Only one
* connection is constructed to remote chatter, i.e., if chatter
* A's discovery thread contacts to chatter B's server thread no
* connection from B's discovery thread is made to A's server
* thread.
*
*
* Messaging Protocol
* ==================
*
* The messaging protocol is simple. Every read and written string
* is informative. String are encoded with
* java.io.DataOutputStream.writeUTF(java.lang.String)
* and decoded with
* java.io.DataInputStream.readUTF().
*
*
* Server Thread
* -------------
*
* In server thread writing is done as follows. First written string
* is chatter's user name. After this all written strings are
* user's messages to other chatters.
*
* The first read string is remote chatter's bluetooth address.
* Second read string is the user name of the remote chatter.
* The user name should be less than 8 chars. All other read
* strings are chat messages from other chatters and should be
* displayed in the user interface as messages from a specific
* user.
*
*
* Client Thread
* -------------
*
* In client thread first string to write is device's bluetooth address.
* Then username shall be written. All writes after that are user's
* chat messages.
*
* First string to read is the username of the remote chatter. All other messages
* are normal chat messages from the user.
*
*
*/
protected void startApp() throws MIDletStateChangeException {
LcduiUI ui = new LcduiUI(this);
ConnectionProtocol protocol = new ConnectionProtocol(ui);
try {
protocol
.setUserName(LocalDevice.getLocalDevice().getFriendlyName());
} catch (BluetoothStateException e) {
protocol.setUserName("user");
}
new ServerThread(protocol).start();
new DiscoveryThread(protocol).start();
ui.setCurrent();
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
Log.clear();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -