?? deviceservercomm.java
字號:
package example.simple;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* A class that demonstrates Bluetooth communication between server mode PC and
* client mode device through serial port profile.
*
* @see <a href="http://sourceforge.net/projects/bluecove/">BlueCove - JSR-82
* implementation</a>
*/
public class DeviceServerCOMM extends MIDlet {
/*-
* ================
* Bluetooth Server
* ================
*
* This example application is a straighforward implementation of
* a bluetooth server.
*
*
* Usage
* =====
*
* Start the program. Events are logged by printing them out with standard
* output stream. Once the server is up and ready to accept connections,
* connect to server with client.
*
*
* How it Works
* ============
*
* The application starts a loop to wait for a new connection. After a new
* connection is reseived the connection is handled. In the handling
* operation few tokens and end token is written to connection stream.
* Each read token is logged to standard output. After handling session
* the loop continues by waiting for a new connection.
*
*/
/*-
*
* ---- Bluetooth attributes ----
*/
// serial port profile
protected String UUID = new UUID("1101", true).toString();
protected int discoveryMode = DiscoveryAgent.GIAC; // no paring needed
/*-
*
* ---- Connection handling attributes ----
*/
protected int endToken = 255;
/*-
*
* ---- User interface attributes ----
*/
protected Form infoArea = new Form("Bluetooth Server");
protected void startApp() throws MIDletStateChangeException {
infoArea.deleteAll();
Display.getDisplay(this).setCurrent(infoArea);
try {
LocalDevice device = LocalDevice.getLocalDevice();
device.setDiscoverable(DiscoveryAgent.GIAC);
String url = "btspp://localhost:" + UUID + ";name=DeviceServerCOMM";
log("Create server by uri: " + url);
StreamConnectionNotifier notifier = (StreamConnectionNotifier) Connector
.open(url);
serverLoop(notifier);
} catch (Throwable e) {
log(e);
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
private void serverLoop(StreamConnectionNotifier notifier) {
try {
while (true) { // infinite loop to accept connections.
log("Waiting for connection...");
handleConnection(notifier.acceptAndOpen());
}
} catch (Exception e) {
log(e);
}
}
private void handleConnection(StreamConnection conn) throws IOException {
OutputStream out = conn.openOutputStream();
InputStream in = conn.openInputStream();
startReadThread(in);
try { // to write some tokens through the bluetooth link
int[] tokens = new int[] { 1, 3, 5, 7, endToken };
for (int i = 0; i < tokens.length; i++) {
out.write(tokens[i]);
out.flush();
log("write:" + tokens[i]);
// wait a sec before next write.
Thread.sleep(1 * 1000);
}
} catch (Exception e) {
log(e);
} finally {
log("Close connection.");
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
}
private void startReadThread(final InputStream in) {
Thread reader = new Thread() {
public void run() {
try {
while (true) {
int r = in.read();
log("read:" + r);
if (r == endToken)
break;
}
} catch (Throwable e) {
log(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
};
reader.start();
}
/*-
* ------- Utility section -------
*/
synchronized private void log(String msg) {
infoArea.append(msg);
infoArea.append("\n\n");
}
private void log(Throwable e) {
log(e.getMessage());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -