蓝牙五子棋.docx
- 文档编号:7369227
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:28
- 大小:21.09KB
蓝牙五子棋.docx
《蓝牙五子棋.docx》由会员分享,可在线阅读,更多相关《蓝牙五子棋.docx(28页珍藏版)》请在冰豆网上搜索。
蓝牙五子棋
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
publicclassBTGameextendsMIDletimplementsCommandListener{
privatestaticBTGameinstance;
privateMyCanvasmycanvas;
privateChoiceGroupchoices;
privateForminitForm;
/**Constructor*/
publicBTGame(){
instance=this;
}
/**Mainmethod*/
publicvoidstartApp(){
initForm=newForm("Connect4");
//Addthepeerchoicegroup
String[]peerNames={"Server","Client"};
choices=newChoiceGroup("Pleaseselectpeertype:
",Choice.EXCLUSIVE,peerNames,null);
initForm.append(choices);
//AddtheExitandPlaycommands
CommandexitCommand=newCommand("Exit",Command.EXIT,0);
initForm.addCommand(exitCommand);
CommandplayCommand=newCommand("Play",Command.OK,0);
initForm.addCommand(playCommand);
initForm.setCommandListener(this);
//Settheformasthecurrentscreen
Display.getDisplay(this).setCurrent(initForm);
//Display.getDisplay(this).setCurrent(displayable);
}
/**HandlepausingtheMIDlet*/
publicvoidpauseApp(){
}
/**HandledestroyingtheMIDlet*/
publicvoiddestroyApp(booleanunconditional){
}
/**QuittheMIDlet*/
publicstaticvoidquitApp(){
instance.destroyApp(true);
instance.notifyDestroyed();
instance=null;
}
publicvoidcommandAction(Commandc,Displayables){
if(c.getCommandType()==Command.EXIT){
destroyApp(true);
notifyDestroyed();
}
elseif(c.getCommandType()==Command.OK){
//Findoutwhichtypeofpeerconnectionisbeingused
Stringname=choices.getString(choices.getSelectedIndex());
//Createthegamecanvas
if(mycanvas==null){
mycanvas=newMyCanvas(Display.getDisplay(this),name);
CommandexitCommand=newCommand("Exit",Command.EXIT,0);
mycanvas.addCommand(exitCommand);
mycanvas.setCommandListener(this);
//StartupthegameCanvas
mycanvas.start();
}
}
}
}
/**
*
Title:
*
Description:
*
Copyright:
Copyright(c)2005
*
Company:
*@authornotattributable
*@version1.0
*/
publicclassChess{
publicbooleanisServer;
publicChess(){
}
publicChess(booleanisServer)
{
this.isServer=isServer;
}
}
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.util.Vector;
importjavax.microedition.io.Connector;
importjavax.microedition.io.StreamConnection;
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Form;
importjavax.microedition.lcdui.Gauge;
importjavax.microedition.lcdui.StringItem;
importjavax.microedition.lcdui.TextField;
//jsr082API
importjavax.bluetooth.BluetoothStateException;
importjavax.bluetooth.DeviceClass;
importjavax.bluetooth.DiscoveryAgent;
importjavax.bluetooth.DiscoveryListener;
importjavax.bluetooth.LocalDevice;
importjavax.bluetooth.RemoteDevice;
importjavax.bluetooth.ServiceRecord;
importjavax.bluetooth.UUID;
/**
*
Title:
*
Description:
*
Copyright:
Copyright(c)2005
*
Company:
*@authornotattributable
*@version1.0
*/
publicclassClientimplementsRunnable,DiscoveryListener{
privateConnectionconnection;
privateDiscoveryAgentdiscoveryAgent;
//响应服务的UUID
privatestaticfinalUUIDECHO_SERVER_UUID=newUUID(
"F0E0D0C0B0A000908070605040302010",false);
//设备集合
Vectordevices=newVector();
//服务集合
Vectorrecords=newVector();
Stringmessagex,messagey;
Stringmessage_1,message_2;
StreamConnectionconn;
inti=1;
//服务搜索的事务id集合
int[]transIDs;
publicClient(Connectionconnection)
{
this.connection=connection;
}
publicvoidstart()
{
Threadt=newThread(this);
t.start();
}
publicvoidsendMessage(Stringmessage1,Stringmessage2)
{
message_1=message1;
message_2=message2;
ThreadfetchThread=newThread(){
publicvoidrun(){
try{
ServiceRecordsr=(ServiceRecord)records.elementAt(0);
Stringurl=sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false);
//Stringmessage_1,message_2;
conn=(StreamConnection)Connector.open(url);
//Thread.sleep(500);
DataOutputStreamdos=conn.openDataOutputStream();
dos.writeUTF(message_1);
System.out.println("writex"+i+message_1);
dos.writeUTF(message_2);
System.out.println("writey"+i+message_2);
dos.flush();
dos.close();
DataInputStreamdis=conn.openDataInputStream();
messagex=dis.readUTF();
System.out.println("messagexis"+i+messagex);
messagey=dis.readUTF();
System.out.println("messageyis"+i+messagey);
dis.close();
System.out.println(i);
i++;
connection.receiveMessage(messagex,messagey);
}
catch(Exceptione)
{
System.out.println("yichang");
e.printStackTrace();
}
}
};
fetchThread.start();
}
publicsynchronizedvoidrun()
{
try{
LocalDevicelocal=LocalDevice.getLocalDevice();
discoveryAgent=local.getDiscoveryAgent();
}catch(BluetoothStateExceptionbse){}
try{
discoveryAgent.startInquiry(DiscoveryAgent.GIAC,this);
//设备初使化GIAC找的是普通的设备
}catch(BluetoothStateExceptionbse){}
try{wait();}
catch(Exceptione){}
UUID[]uuids=newUUID[1];
uuids[0]=ECHO_SERVER_UUID;
transIDs=newint[devices.size()];
for(inti=0;i RemoteDevicerd=(RemoteDevice)devices.elementAt(i); try{ //记录每一次服务搜索的事务id System.out.println("beginsearchservices"); transIDs[i]=discoveryAgent.searchServices(null,uuids, rd,this); }catch(BluetoothStateExceptione){ continue; } }//endfor try{ //阻塞,由serviceSearchCompleted()回调方法在所有设备都搜索完的情况下唤醒 wait(); }catch(InterruptedExceptione1){ e1.printStackTrace(); } System.out.println("Clientrunend"); } publicvoiddeviceDiscovered(RemoteDeviceremoteDevice,DeviceClassdeviceClass){ devices.addElement(remoteDevice);//remoteDevice就是被发现的设备本身 System.out.println("discoveredadevice"); System.out.println(remoteDevice.getBluetoothAddress()); }//发现设备时候就会调用此程序即由agent.startinquiry发出的 publicvoidinquiryCompleted(intparam){ System.out.println("startinquirycompleted"); switch(param){ caseDiscoveryListener.INQUIRY_COMPLETED: break; caseDiscoveryListener.INQUIRY_ERROR: break; caseDiscoveryListener.INQUIRY_TERMINATED: break; } System.out.println("completeinquary"); synchronized(this){ notify(); } } publicvoidserviceSearchCompleted(inttransID,intrespCode){ System.out.println("completeserviceSearch"); switch(respCode) { caseDiscoveryListener.SERVICE_SEARCH_COMPLETED: break; caseDiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE: break; caseDiscoveryListener.SERVICE_SEARCH_ERROR: break; caseDiscoveryListener.SERVICE_SEARCH_NO_RECORDS: break; caseDiscoveryListener.SERVICE_SEARCH_TERMINATED: break; } synchronized(this){ notify(); } } publicvoidservicesDiscovered(inttransID,ServiceRecord[]serviceRecord){ //servicesFound=serviceRecord; System.out.println("getserver"); for(inti=0;i records.addElement(serviceRecord[i]); } }//立刻返回transID;ID是辨别service的唯一识别; } /** * Title: * Description:
*
Copyright:
Copyright(c)2005
*
Company:
*@authornotattributable
*@version1.0
*/
publicclassConnection{
Serverserver;
Clientclient;
intselectedX,selectedY;
booleanisServer;
Controlcontrol;
publicConnection(Controlcontrol){
this.control=control;
isServer=control.isServer;
}
publicvoidstart()
{
if(isServer){
server=newServer(this);
server.start();
}
else{
client=newClient(this);
client.start();
}
}
publicvoidsendMessage(intselected_X,intselected_Y)
{
if(isServer)
server.sendMessage(Integer.toString(selected_X),Integer.toString(selected_Y));
else{
System.out.println("connectionissendmessage");
System.out.println(Integer.toString(selected_Y));
client.sendMessage(Integer.toString(selected_X),Integer.toString(selected_Y));
}
}
publicvoidreceiveMessage(Stringmessagex,Stringmessagey)
{
selectedX=Integer.parseInt(messagex);
selectedY=Integer.parseInt(messagey);
System.out.println("connectionisreceivemessage");
control.receiveMessage();
}
publicintgetSelectedX()
{
returnselectedX;
}
publicintgetSelectedY()
{
returnselectedY;
}
}
/**
*
Title:
*
Description:
*
Copyright:
Copyright(c)2005
*
Company:
*@authornotattributable
*@version1.0
*/
publicclassControl{
Connectionconnection;
GameWorldgameworld;
MyCanvasmycanvas;
intselectedX,selectedY;
publicbooleanisServer;
booleanmyMove;
publicControl(GameWorldgameworld){
this.gameworld=gameworld;
//this.mycanvas=mycanvas;
isServer=gameworld.isServer;
}
publicvoidstart()
{
connection=newConnection(this);
connection.start();//连接开始
}
publicvoidsendMessage(intselected_X,intselected_Y)
{
connection.sendMessage(selected_X,selected_Y);
}
publicvoidreceiveMessage()
{
gameworld.receiveMessage();
System.out.println("controlisreceivemessage");
}
publicintgetSelectedX()
{
selectedX=connection.getSelectedX();
returnselectedX;
}
publicintgetSelectedY()
{
selectedY=connection.getSelectedY();
returnselectedY;
}
}
importjavax.microedition.lcdui.*;
/**
*
Title:
*
Description:
*
Copyright:
Copyright(c)2005
*
Company:
*@authornotattributable
*@version1.0
*/
publicclassGameWorld{
privateControlcontrol;
privateChess[][]chess;//棋子数组
//privatebooleanmyMove;
//privatebooleanisServer;
privateintplayer1win,player2win;
privateMyCanvas
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 蓝牙 五子棋