蓝牙开发之从手机走向PC【2】——手机与手机之间的通信实现
2010-02-04 00:00:00 来源:WEB开发网Mainform类中主要提供Server和Client选项供用户选择,详细代码不解释,最后会给出完整的源程序下载~
components包中的BlueClient.java文件内容:http://file.ddvip.com/2010_02/1265271066_ddvip_2097.rar
components包中的BlueServer.java文件内容:http://file.ddvip.com/2010_02/1265271126_ddvip_6208.rar
以上两个类分别是客户端和服务器端的界面类,所有的逻辑操作都被放到bluetooth包中的对应的类中了,几个文件笔者都注释的比较翔实了,所以暂不解释。
bluetooth包中的BlueServerService.java文件:
/**
* 实现服务器端蓝牙服务的类
* @author royen
* @since 2010.1.25
*/
public class BlueServerService implements Runnable{
//服务标示符
private final static UUID SERVER_UUID=new UUID("F0E0D0C0B0A000908070605040302010",false);
//流连接通知器
private StreamConnectionNotifier notifier;
//服务器端界面
private BlueServer serverForm;
//服务记录
ServiceRecord serviceRecord;
public BlueServerService(BlueServer frm){
this.serverForm=frm;
}
/**
* 开启服务线程
*/
public void run() {
boolean btReady=false;
try{
//获取本地蓝牙设备
LocalDevice localDevice=LocalDevice.getLocalDevice();
if(!localDevice.setDiscoverable(DiscoveryAgent.GIAC)){
System.out.println("set discoveryMode failed~");
return ;
}
notifier=(StreamConnectionNotifier)Connector.open(getConnectionStr());
serviceRecord=localDevice.getRecord(notifier);
btReady=true;
}
catch(Exception ex){
System.out.println("occur exception "+ex.getMessage());
}
if(!btReady){
System.out.println("bluetooth init failed~");
return ;
}
serverForm.appendInfo("service setup,waiting for connect...");
//切换界面
serverForm.changeForm();
while(true){
StreamConnection conn=null;
try{
conn=notifier.acceptAndOpen();
}
catch(Exception ex){
System.out.println("occur exception when accept connection~");
continue;
}
//开启对连接的处理线程
new Thread(new ProcessConnection(conn)).start();
}
}
/**
* 获取连接字符串
* @return
*/
private String getConnectionStr(){
StringBuffer sb=new StringBuffer("btspp://");
sb.append("localhost").append(":");
sb.append(SERVER_UUID.toString());
sb.append(";name=BlueMessage");
sb.append(";authorize=false");
return sb.toString();
}
/**
* 处理客户端连接的线程
* @author royen
* @since 2010.1.25
*/
private class ProcessConnection implements Runnable{
//连接流
private StreamConnection conn=null;
public ProcessConnection(StreamConnection conn){
this.conn=conn;
}
public void run() {
try{
String inputStr=readInputString(conn);
serverForm.appendInfo("recive message from client: "+inputStr);
String outputString;
if(inputStr.startsWith("connect...")){
outputString="welcome...";
}
else{
//生成响应
outputString="server echo "+inputStr;
}
sendOutputString(outputString,conn);
conn.close();
}
catch(Exception ex){
System.out.println("occur exception ,message is "+ex.getMessage());
}
}
/**
* 读取接受的数据
* @param conn
* @return
*/
private String readInputString(StreamConnection conn){
try{
DataInputStream dis=conn.openDataInputStream();
String msg=dis.readUTF();
dis.close();
return msg;
}
catch(Exception ex){
System.out.println("occur exception when read data~");
return ex.getMessage();
}
}
/**
* 发送反馈信息
* @param msg
* @param conn
*/
private void sendOutputString(String msg,StreamConnection conn){
try{
DataOutputStream dos=conn.openDataOutputStream();
dos.writeUTF(msg);
dos.close();
}
catch(Exception ex){
System.out.println("occur exception when send data~");
}
}
}
}
更多精彩
赞助商链接