📄 clientchess.java
字号:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
class ClientChess implements DiscoveryListener, Runnable
{
private UUID uuid=null;
private UUID[] uuidSet;
private DiscoveryAgent discoveryAgent = null;
private int[] transIds = null;
private int serviceRecordNumber = 0;
private ServiceRecord[] service = new ServiceRecord[5];
private int remoteDeviceNumber = 0;
private RemoteDevice[] remoteDevice = new RemoteDevice[5];
private volatile boolean stop=false;
private Flash flash=null;
private int selectedSaveIndex=0;
private MainSelect mainSelect = null;
//与选择的来源有关的变量
private int[][] chess = null;
private boolean ifSelect = false;
//与发送变化相关变量
private int clientType = 0;
private int[] chance = null;
private boolean state = false;
private ChessCanvas chessCanvas = null;
private ViewerCanvas viewerCanvas = null;
private boolean serverOrClient = false;
private int viewerSendNumber = 0;
//取得用户设置
private String oneUuid = null;
private String twoUuid = null;
private String threeUuid = null;
//用于标记serviceSearchCompleted()方法只被调用一次
private boolean isCalled = false;
private boolean isOkCalled = false;
//发送数据相关变量
private Form form = null;
private boolean isPhase = false;
private int[][] reset = null;
//背景声音相关变量
private int musicIndex, soundIndex;
ClientChess(MainSelect ms,int saveIndex, int musicIndex, int soundIndex)
{
clientType = 0;
mainSelect = ms;
selectedSaveIndex=saveIndex;
this.musicIndex = musicIndex;
this.soundIndex = soundIndex;
//取得用户设置
uuid=new UUID("0",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
flash=new Flash();
flash.startFlash();
connectToServer();
}
ClientChess(int[] chance, ChessCanvas cc, boolean b)
{//向对方发送棋盘变化
clientType = 1;
this.chance = chance;
chessCanvas = cc;
serverOrClient = b;
//取得用户设置
uuid=new UUID("1",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
connectToServer();
}
ClientChess(int[] chance, ChessCanvas cc, int state)
{//向看擂者发送棋盘变化
//System.out.println("向看擂者发送棋盘变化构造函数完成");
clientType = 2;
if(state == 0)
serverOrClient = true;
else
serverOrClient = false;
chessCanvas = cc;
this.chance = chance;
//取得用户设置
uuid=new UUID("2",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
connectToServer();
}
ClientChess(ViewerCanvas vc)
{//看擂者联系服务器用来接收初始棋盘数据
chess = new int[10][9];
clientType = 3;
viewerCanvas = vc;
//取得用户设置
uuid=new UUID("3",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
connectToServer();
}
ClientChess(ViewerCanvas vCanvas, int[][] chess, int[] chance)
{//向观众发送数据
viewerCanvas = vCanvas;
this.chess = chess;
this.chance = chance;
clientType = 4;
uuid=new UUID("4",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
connectToServer();
}
ClientChess(Form f, int[][] chess, Flash flash)
{//发送共享数据
form = f;
this.chess = chess;
this.flash = flash;
isPhase = true;
clientType = 5;
uuid=new UUID("5",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
connectToServer();
}
ClientChess(Form f, int[][] chess, int[][] reset, Flash flash)
{//发送共享数据
form = f;
this.chess = chess;
this.reset = reset;
this.flash = flash;
isPhase = false;
clientType = 5;
uuid=new UUID("5",false);
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = uuid;
connectToServer();
}
public void stopClient()
{
stop=true;
}
//*******************固定接口
public void deviceDiscovered(RemoteDevice rd,DeviceClass dc)
{
//System.out.println("deviceDiscovered方法被调用,发现的设备是:"+rd);
remoteDevice[remoteDeviceNumber++] = rd;
}
public void inquiryCompleted(int x)
{
synchronized(this)
{
notify();
}
}
public void servicesDiscovered(int transId,ServiceRecord[] serviceRecord)
{//搜索每个设备上的服务完毕后调用一次
//System.out.println("搜索到的服务个数是:"+serviceRecord.length);
for(int i = 0; i < serviceRecord.length; i++)
{
service[serviceRecordNumber] = serviceRecord[i];
serviceRecordNumber = serviceRecordNumber + 1;
}
}
public void serviceSearchCompleted(int transID, int respCode)
{//这个方法当所有的设备上所有的服务搜索完毕后才调用
//System.out.println("serviceSearchCompleted is recalled");
for (int i = 0; i < transIds.length; i++)
{
if (transIds[i] == transID)
{
transIds[i] = -1;
break;
}
}
//如果所有的设备都已经搜索服务完毕,则唤醒初始化线程。
//System.out.println("serviceSearch is recalled");
boolean finished = true;
for (int i = 0; i < transIds.length; i++)
{
if (transIds[i] != -1)
{
finished = false;
break;
}
}
if (finished)
{
synchronized (this)
{
//System.out.println("enter to notify");
notify();
//System.out.println("notify end");
}
}
}
//*******************初始化线程
private void connectToServer()
{
new Thread(this).start();
}
public synchronized void run()
{
try
{
LocalDevice ld=LocalDevice.getLocalDevice();
discoveryAgent=ld.getDiscoveryAgent();
}
catch (Exception e)
{
//System.out.println("获取本地蓝牙设备管理器出现错误");
//提示错误,退出程序
Alert alert=new Alert("Message","获取本地蓝牙设备管理器出现错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
//Chess.quitApp();
}
//System.out.println("开始搜索设备");
try
{
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
}
catch (BluetoothStateException e)
{
//System.out.println("搜索设备时产生异常");
//提示错误,退出程序
Alert alert=new Alert("Message","搜索设备时发生错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
//Chess.quitApp();
}
try
{
//阻塞,由inquiryCompleted()回调方法唤醒
wait();
}
catch (InterruptedException e1)
{
}
if(remoteDevice.length == 0)
{
//没有搜索到设备后的处理
//System.out.println("没有搜索到设备");
Chess.quitApp();
}
//System.out.println("客户端找到"+remoteDeviceNumber+"个设备");
if(remoteDeviceNumber == 0)
{
if(clientType == 4)
{
//System.out.println("没有搜索到服务");
viewerCanvas.initViewerAcceptThread();
}
return;
}
transIds = new int[remoteDeviceNumber];
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -