⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameserver.java

📁 支持蓝牙的手机游戏 支持蓝牙的手机游戏的分析与实现
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package org.challenge.chengshi.game.server;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import javax.bluetooth.DataElement;import javax.bluetooth.DiscoveryAgent;import javax.bluetooth.LocalDevice;import javax.bluetooth.ServiceRecord;import javax.bluetooth.UUID;// midp/cldc APIimport javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;import javax.microedition.io.StreamConnectionNotifier;import org.challenge.chengshi.game.GameUI;/** * * 蓝牙服务器端 * @author challenge */public class GameServer implements Runnable {    /** Describes this server */    private static final UUID SERVER_UUID =        new UUID("F0E0D0C0B0A000908070605040302010", false);    /** The attribute id of the record item with images names. */    private static final int IMAGES_NAMES_ATTRIBUTE_ID = 0x4321;    /** Keeps the local device reference. */    private LocalDevice localDevice;    /** Accepts new connections. */    private StreamConnectionNotifier notifier;    /** Keeps the information about this server. */    private ServiceRecord record;    public String writeMessage;    StreamConnection conn;    DataInputStream in;    DataOutputStream out;    GameUI game;    Thread th;    Thread read;    Thread write;    public GameServer(GameUI game){        this.game=game;        th=new Thread(this);        th.start();    }    //向客房端发送信息    public void sendMessage(String message){        if(this.write==null){            return;        }        this.writeMessage=message;        synchronized(this.write){            this.write.notify();//有信息发送时唤醒发信息线程        }    }    public void run() {        init();    }    //初始化    public boolean init(){        boolean isBTReady = false;        try {            // create/get a local device            localDevice = LocalDevice.getLocalDevice();            // set we are discoverable            if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {                // Some implementations always return false, even if                // setDiscoverable successful                // throw new IOException("Can't set discoverable mode...");            }            // prepare a URL to create a notifier            StringBuffer url = new StringBuffer("btspp://");            // indicate this is a server            url.append("localhost").append(':');            // add the UUID to identify this service            url.append(SERVER_UUID.toString());            // add the name for our service            url.append(";name=ChengShiGame Server");            // request all of the client not to be authorized            // some devices fail on authorize=true            url.append(";authorize=false");            // create notifier now            notifier = (StreamConnectionNotifier)Connector.open(url.toString());            // and remember the service record for the later updates            record = localDevice.getRecord(notifier);            // create a special attribute with images names            DataElement base = new DataElement(DataElement.DATSEQ);            record.setAttributeValue(IMAGES_NAMES_ATTRIBUTE_ID, base);            // remember we've reached this point.            isBTReady = true;            this.conn=this.notifier.acceptAndOpen();            in=this.conn.openDataInputStream();            out=this.conn.openDataOutputStream();            this.read=new Read();//接收信息线程            this.write=new Write();//发送信息线程            this.read.start();            this.write.start();        } catch (Exception e) {            System.err.println("Can't initialize bluetooth: " + e);        }        return isBTReady;    }        class Read extends Thread{        public void run(){            while(true){                try {                    String str = in.readUTF();                    if (str != null) {                        game.receiveMessage(str);//接收到信息时回调给游戏                    }                } catch (IOException ex) {                    ex.printStackTrace();                }            }        }    }        class Write extends Thread{                public void run(){            while(true){                synchronized(this){                    try{                        wait();                    }catch(InterruptedException e){                        e.printStackTrace();                    }                    if(writeMessage!=null){                        try {                            out.writeUTF(writeMessage);                        } catch (IOException ex) {                            ex.printStackTrace();                        }                    }                }            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -