📄 spp_server.java
字号:
package net.benhui.btgallery.spp_bt;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.io.*;
import net.benhui.btgallery.*;
import net.benhui.btgallery.spp_gui.*;
/**
*
* <p>Title: Example Serial Port Profile Server.</p>
* <p>Description: This example server only handle one SPP server connection
* and wait for a client to connect. Once a client connection accept, it read
* one string off the connection stream, display this string on screen, then
* echo the string back to client.
* Then it waits for the next client connection again.</p>
* <p>Description: Important area: run() </p>
*
* @author Ben Hui (www.benhui.net)
* @version 1.0
*
* LICENSE:
* This code is licensed under GPL. (See http://www.gnu.org/copyleft/gpl.html)
*/
public class SPP_Server implements Runnable
{
// Bluetooth singleton object
LocalDevice device;
DiscoveryAgent agent;
// SPP_Server specific service UUID
// note: this UUID must be a string of 32 char
// do not use the 0x???? constructor because it won't
// work. not sure if it is a N6600 bug or not
public final static UUID uuid = new UUID("102030405060708090A0B0C0D0E0F010", false);
//
// major service class as SERVICE_TELEPHONY
private final static int SERVICE_TELEPHONY = 0x400000;
// control flag for run loop
// set true to exit loop
public boolean done = false;
// our BT server connection
public StreamConnectionNotifier server;
public SPP_Server()
{
}
public void run_server()
{
try
{
//
// initialize the JABWT stack
device = LocalDevice.getLocalDevice(); // obtain reference to singleton
device.setDiscoverable(DiscoveryAgent.GIAC); // set Discover mode to LIAC
// start a thread to serve the server connection.
// for simplicity of this demo, we only start one server thread
// see run() for the task of this thread
Thread t = new Thread( this );
t.start();
} catch ( BluetoothStateException e )
{
e.printStackTrace();
}
}
public void run()
{
// human friendly name of this service
String appName = "SPPServerExample";
// connection to remote device
StreamConnection c = null;
try
{
String url = "btspp://localhost:" + uuid.toString() +";name="+ appName;
log("server url: " + url );
// Create a server connection object, using a
// Serial Port Profile URL syntax and our specific UUID
// and set the service name to BlueChatApp
server = (StreamConnectionNotifier)Connector.open( url );
// Retrieve the service record template
ServiceRecord rec = device.getRecord( server );
// set ServiceRecrod ServiceAvailability (0x0008) attribute to indicate our service is available
// 0xFF indicate fully available status
// This operation is optional
rec.setAttributeValue( 0x0008, new DataElement( DataElement.U_INT_1, 0xFF ) );
// Print the service record, which already contains
// some default values
Util.printServiceRecord( rec );
// Set the Major Service Classes flag in Bluetooth stack.
// We choose Telephony Service
rec.setDeviceServiceClasses( SERVICE_TELEPHONY );
} catch (Exception e)
{
e.printStackTrace();
log(e.getClass().getName()+" "+e.getMessage());
}
while( !done)
{
try {
///////////////////////////////
log("local service waiting for client connection...");
//
// start accepting client connection.
// This method will block until a client
// connected
c = server.acceptAndOpen();
log("accepted a client connection. reading it...");
//
// retrieve the remote device object
RemoteDevice rdev = RemoteDevice.getRemoteDevice( c );
// obtain an input stream to the remote service
DataInputStream in = c.openDataInputStream();
// read in a string from the string
String s = in.readUTF();
log("read in data '"+s+"' from client");
DataOutputStream out = c.openDataOutputStream();
out.writeUTF( s );
out.flush();
log("echo '"+s+"' back to client");
// close current connection, wait for the next one
c.close();
} catch (Exception e)
{
e.printStackTrace();
SPP_MIDlet.alert(e, SPP_MIDlet.instance.spp_screen );
}
} // while
}
/**
* An utility function to display a log message
* @param s String
*/
public void log( String s )
{
SPP_MIDlet.log( s );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -