蓝牙五子棋Word文档下载推荐.docx
- 文档编号:20478723
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:28
- 大小:21.09KB
蓝牙五子棋Word文档下载推荐.docx
《蓝牙五子棋Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《蓝牙五子棋Word文档下载推荐.docx(28页珍藏版)》请在冰豆网上搜索。
//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);
mycanvas.addCommand(exitCommand);
mycanvas.setCommandListener(this);
//StartupthegameCanvas
mycanvas.start();
}
/**
*<
p>
Title:
<
/p>
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;
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);
writey"
+i+message_2);
dos.flush();
dos.close();
DataInputStreamdis=conn.openDataInputStream();
messagex=dis.readUTF();
messagexis"
+i+messagex);
messagey=dis.readUTF();
messageyis"
+i+messagey);
dis.close();
System.out.println(i);
i++;
connection.receiveMessage(messagex,messagey);
catch(Exceptione)
yichang"
e.printStackTrace();
fetchThread.start();
publicsynchronizedvoidrun()
LocalDevicelocal=LocalDevice.getLocalDevice();
discoveryAgent=local.getDiscoveryAgent();
}catch(BluetoothStateExceptionbse){}
discoveryAgent.startInquiry(DiscoveryAgent.GIAC,this);
//设备初使化GIAC找的是普通的设备
try{wait();
catch(Exceptione){}
UUID[]uuids=newUUID[1];
uuids[0]=ECHO_SERVER_UUID;
transIDs=newint[devices.size()];
for(inti=0;
i<
devices.size();
i++){
RemoteDevicerd=(RemoteDevice)devices.elementAt(i);
//记录每一次服务搜索的事务id
beginsearchservices"
);
transIDs[i]=discoveryAgent.searchServices(null,uuids,
rd,this);
}catch(BluetoothStateExceptione){
continue;
}//endfor
//阻塞,由serviceSearchCompleted()回调方法在所有设备都搜索完的情况下唤醒
wait();
}catch(InterruptedExceptione1){
e1.printStackTrace();
Clientrunend"
publicvoiddeviceDiscovered(RemoteDeviceremoteDevice,DeviceClassdeviceClass){
devices.addElement(remoteDevice);
//remoteDevice就是被发现的设备本身
discoveredadevice"
System.out.println(remoteDevice.getBluetoothAddress());
}//发现设备时候就会调用此程序即由agent.startinquiry发出的
publicvoidinquiryCompleted(intparam){
startinquirycompleted"
switch(param){
caseDiscoveryListener.INQUIRY_COMPLETED:
break;
caseDiscoveryListener.INQUIRY_ERROR:
caseDiscoveryListener.INQUIRY_TERMINATED:
completeinquary"
synchronized(this){
notify();
publicvoidserviceSearchCompleted(inttransID,intrespCode){
completeserviceSearch"
switch(respCode)
caseDiscoveryListener.SERVICE_SEARCH_COMPLETED:
caseDiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
caseDiscoveryListener.SERVICE_SEARCH_ERROR:
caseDiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
caseDiscoveryListener.SERVICE_SEARCH_TERMINATED:
publicvoidservicesDiscovered(inttransID,ServiceRecord[]serviceRecord){
//servicesFound=serviceRecord;
getserver"
serviceRecord.length;
records.addElement(serviceRecord[i]);
}//立刻返回transID;
ID是辨别service的唯一识别;
publicclassConnection{
Serverserver;
Clientclient;
intselectedX,selectedY;
booleanisServer;
Controlcontrol;
publicConnection(Controlcontrol){
this.control=control;
isServer=control.isServer;
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));
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);
connectionisreceivemessage"
control.receiveMessage();
publicintgetSelectedX()
returnselectedX;
publicintgetSelectedY()
returnselectedY;
publicclassControl{
Connectionconnection;
GameWorldgameworld;
MyCanvasmycanvas;
booleanmyMove;
publicControl(GameWorldgameworld){
this.gameworld=gameworld;
//this.mycanvas=mycanvas;
isServer=gameworld.isServer;
connection=newConnection(this);
connection.start();
//连接开始
publicvoidsendMessage(intselected_X,intselected_Y)
connection.sendMessage(selected_X,selected_Y);
publicvoidreceiveMessage()
gameworld.receiveMessage();
controlisreceivemessage"
selectedX=connection.getSelectedX();
selectedY=connection.getSelectedY();
publicclassGameWorld{
privateControlcontrol;
privateChess[][]chess;
//棋子数组
//privatebooleanmyMove;
//privatebooleanisServer;
privateintplayer1win,player2win;
privateMyCanvas
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 蓝牙 五子棋