解决方案 »

  1.   

    PC端PCServer.javapackage PCBluetoothTest;import java.util.Scanner;import javax.swing.JOptionPane;public class PCServer {

    public static void main(String[] args){

    BlueService bs=new BlueService();

    if(bs.initialBluetooth()){
    bs.startService();
    }
    else{
    JOptionPane.showMessageDialog(null,"蓝牙栈初始化失败,可能你机器不支持蓝牙模块!");
    }
    }
    }
    BlueServer.javapackage PCBluetoothTest;import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.util.Scanner;import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.bluetooth.UUID;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;import org.omg.IOP.Encoding;
     //蓝牙服务类public class BlueService implements Runnable{
    //服务标示符
    private final static UUID SERVER_UUID=new UUID("6929D16556A3AEF99568BAC4BF1598F7",false); //流连接通知器
    private StreamConnectionNotifier notifier; /** 本地设备管理类 */
    private LocalDevice localDevice=null; //服务记录
    ServiceRecord serviceRecord;

    private boolean bsStop = false;
    private boolean pcStop1 = false;
    private boolean pcStop2 = false; public BlueService(){ } /**
     * 开启服务
     */
    public void startService(){
    new Thread(this).start();
    } public void run() {
    try{
    notifier=(StreamConnectionNotifier)Connector.open(getConnectionStr()); serviceRecord=localDevice.getRecord(notifier);
    }
    catch(Exception ex){
    System.out.println("occur exception here: "+ex.getMessage());
    }

    while(!bsStop){
    StreamConnection conn=null;
    try{
    conn=notifier.acceptAndOpen();
    }
    catch(Exception ex){
    System.out.println("occur exception when accept connection~");
    bsStop = true;
    continue;
    } //开启对连接的处理线程
    new Thread(new ProcessConnection1(conn)).start();
    new Thread(new ProcessConnection2(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();
    }
    /**
     * 蓝牙初始化
     * @return
     */
    public boolean initialBluetooth(){
    boolean btReady=true;
    System.out.println("蓝牙初始化");
    try{
    localDevice=LocalDevice.getLocalDevice();
    if(!localDevice.setDiscoverable(DiscoveryAgent.GIAC)){
    btReady=false;
    }
    }
    catch(Exception e){
    btReady=false;
    e.printStackTrace();
    } return btReady;
    } /**
     * 处理客户端连接的线程
     */
    private class ProcessConnection1 implements Runnable{
    //连接流
    private StreamConnection conn=null;

    //读取流中
    private DataInputStream  dis=null;

    public ProcessConnection1(StreamConnection conn){
    this.conn=conn;
    }

    public void run() {
    System.out.println("client connected...read");

    try{
    dis=conn.openDataInputStream(); while(!pcStop1){
    String msg=readInputString();
    if(msg != null)
    System.out.println("receive Message: "+msg);
    } dis.close();
    conn.close();
    }
    catch(Exception ex){ System.out.println("occur exception ,message is "+ex.getMessage());
    }
    }

    /**
     * 读取接受的数据
     * @param conn
     * @return
     */
    private String readInputString(){
    try{
    String msg=dis.readUTF();

    return msg;
    }
    catch(Exception ex){
    System.out.println("occur exception when read data~");
    pcStop1 = true;
    pcStop2 = true;
    return ex.getMessage();
    }
    }
    }

    private class ProcessConnection2 implements Runnable{
    //连接流
    private StreamConnection conn=null;

    //读取流中
    private DataOutputStream dos=null;

    public ProcessConnection2(StreamConnection conn){
    this.conn=conn;
    }

    public void run() {
    System.out.println("client connected...write");

    try{
    Scanner s = new Scanner(System.in);
    dos=conn.openDataOutputStream();
    String str;
    byte[] tbyte; while(!pcStop2){
    str = s.next();
    tbyte = str.getBytes();
    dos.write(tbyte);
    } dos.close();
    conn.close();
    }
    catch(Exception ex){ pcStop1 = true;
    pcStop2 = true;
    System.out.println("occur exception when write");
    }
    }
    }}