📄 server.java
字号:
import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
public class Server implements Runnable{
private Canvas canvas;
private volatile boolean isReady;
private String myAdd;
private LocalDevice localdevice;
private StreamConnectionNotifier notifier;
private ServerProcessor processor;
//响应服务的UUID
private static final UUID ECHO_SERVER_UUID=new UUID("F0E0D0C0B0A000908070605040302010", false);
private static final String url="btspp://localhost:" + ECHO_SERVER_UUID+";authenticate=false;authorize=false;encrypt=false;name=btsppEcho";
// -----------------------------构造函数---------------------------
public Server(Canvas canvas){
this.canvas=canvas;
try{
localdevice=LocalDevice.getLocalDevice();//获得设备
localdevice.setDiscoverable(DiscoveryAgent.GIAC);//开启,GIAC
myAdd=localdevice.getBluetoothAddress();
}catch(Exception e){}
new Thread(this).start();
}
// -----------------------------取得本地地址---------------------------
public String getAdd(){
return myAdd;
}
// -----------------------------主线程---------------------------
public void run(){
isReady=false;
try{
notifier=(StreamConnectionNotifier)Connector.open(url.toString());//创建通告
}catch(Exception e){
isReady=true;
}
while(!isReady){
StreamConnection conn=null;
try{
conn=notifier.acceptAndOpen();//接受并等待连接
}catch(IOException e){
continue;//错误的客户或中断,继续等待
}
processor=new ServerProcessor(conn,this);
processor.start();
}
}
public void open(){
try{
localdevice.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE);//关闭
}catch(Exception e){}
canvas.change();
}
public void read(byte[] temp){
canvas.read(temp);
}
public void send(String data){
processor.send(data);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -