J2ME蓝牙实战入实例代码和参考资料可实现两人或多人通过蓝牙通信的游戏对弈Sun Java Wireless ToolkitWord文档格式.docx
- 文档编号:22818030
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:35
- 大小:24.47KB
J2ME蓝牙实战入实例代码和参考资料可实现两人或多人通过蓝牙通信的游戏对弈Sun Java Wireless ToolkitWord文档格式.docx
《J2ME蓝牙实战入实例代码和参考资料可实现两人或多人通过蓝牙通信的游戏对弈Sun Java Wireless ToolkitWord文档格式.docx》由会员分享,可在线阅读,更多相关《J2ME蓝牙实战入实例代码和参考资料可实现两人或多人通过蓝牙通信的游戏对弈Sun Java Wireless ToolkitWord文档格式.docx(35页珍藏版)》请在冰豆网上搜索。
该程序包括3个java文件。
一个是MIDlet,另外2个为服务端GUI和客户端GUI。
该程序已经在wtk22模拟器和Nokia6600,Nokia6670两款手机上测试通过。
StupidBTMIDlet.java
importjavax.microedition.lcdui.Alert;
importjavax.microedition.lcdui.AlertType;
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.List;
importjavax.microedition.midlet.MIDlet;
importjavax.microedition.midlet.MIDletStateChangeException;
/**
*@authorJagie
*
*MIDlet
*/
publicclassStupidBTMIDletextendsMIDletimplementsCommandListener{
Listlist;
ServerBoxsb;
ClientBoxcb;
/*
*(non-Javadoc)
*@seejavax.microedition.midlet.MIDlet#startApp()
protectedvoidstartApp()throwsMIDletStateChangeException{
list=newList("
傻瓜蓝牙入门"
List.IMPLICIT);
list.append("
Client"
null);
Server"
list.setCommandListener(this);
Display.getDisplay(this).setCurrent(list);
}
/**
*debug方法
*@params要显示的字串
publicvoidshowString(java/lang/String.java.html"
target="
_blank"
>
Strings){
Displayabledp=Display.getDisplay(this).getCurrent();
Alertal=newAlert(null,s,null,AlertType.INFO);
al.setTimeout(2000);
Display.getDisplay(this).setCurrent(al,dp);
*显示主菜单
publicvoidshowMainMenu(){
protectedvoidpauseApp(){
//TODOAuto-generatedmethodstub
publicvoidcommandAction(Commandcom,Displayabledisp){
if(com==List.SELECT_COMMAND){
Listlist=(List)disp;
intindex=list.getSelectedIndex();
if(index==1){
if(sb==null){
sb=newServerBox(this);
sb.setString(null);
Display.getDisplay(this).setCurrent(sb);
}else{
//每次都生成新的客户端实例
cb=null;
java/lang/System.java.html"
System.gc();
cb=newClientBox(this);
Display.getDisplay(this).setCurrent(cb);
protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{
}
ClientBox.java
importjava.io.java/io/DataInputStream.java.html"
DataInputStream;
importjava.io.java/io/DataOutputStream.java.html"
DataOutputStream;
importjava.io.java/io/IOException.java.html"
IOException;
importjava.util.java/util/Vector.java.html"
Vector;
importjavax.microedition.io.Connector;
importjavax.microedition.io.StreamConnection;
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;
*客户端GUI
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
publicclassClientBoxextendsFormimplementsjava/lang/Runnable.java.html"
Runnable,CommandListener,
DiscoveryListener{
//字串输入框
TextFieldinput=newTextField(null,"
"
50,TextField.ANY);
//loger
StringItemresult=newStringItem("
结果:
"
);
privateDiscoveryAgentdiscoveryAgent;
privateUUID[]uuidSet;
//响应服务的UUID
privatestaticfinalUUIDECHO_SERVER_UUID=newUUID(
"
F0E0D0C0B0A000908070605040302010"
false);
//设备集合
java/util/Vector.java.html"
Vectordevices=newjava/util/Vector.java.html"
Vector();
//服务集合
Vectorrecords=newjava/util/Vector.java.html"
//服务搜索的事务id集合
int[]transIDs;
StupidBTMIDletmidlet;
publicClientBox(StupidBTMIDletmidlet){
super("
this.midlet=midlet;
this.append(result);
this.addCommand(newCommand("
取消"
Command.CANCEL,1));
this.setCommandListener(this);
newjava/lang/Thread.java.html"
Thread(this).start();
publicvoidcommandAction(Commandarg0,Displayablearg1){
if(arg0.getCommandType()==Command.CANCEL){
midlet.showMainMenu();
}else{
//匿名内部Thread,访问远程服务。
java/lang/Thread.java.html"
ThreadfetchThread=newjava/lang/Thread.java.html"
Thread(){
publicvoidrun(){
for(inti=0;
i<
records.size();
i++){
ServiceRecordsr=(ServiceRecord)records.elementAt(i);
if(accessService(sr)){
//访问到一个可用的服务即可
break;
};
fetchThread.start();
privatebooleanaccessService(ServiceRecordsr){
booleanresult=false;
try{
java/lang/String.java.html"
Stringurl=sr.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false);
StreamConnectionconn=(StreamConnection)Connector.open(url);
java/io/DataOutputStream.java.html"
DataOutputStreamdos=conn.openDataOutputStream();
dos.writeUTF(input.getString());
dos.close();
java/io/DataInputStream.java.html"
DataInputStreamdis=conn.openDataInputStream();
Stringecho=dis.readUTF();
dis.close();
showInfo("
反馈结果是:
+echo);
result=true;
}catch(java/io/IOException.java.html"
IOExceptione){
returnresult;
publicsynchronizedvoidrun(){
//发现设备和服务的过程中,给用户以Gauge
Gaugeg=newGauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING);
this.append(g);
蓝牙初始化..."
booleanisBTReady=false;
LocalDevicelocalDevice=LocalDevice.getLocalDevice();
discoveryAgent=localDevice.getDiscoveryAgent();
isBTReady=true;
}catch(java/lang/Exception.java.html"
Exceptione){
e.printStackTrace();
if(!
isBTReady){
蓝牙不可用"
//删除Gauge
this.delete
(1);
return;
uuidSet=newUUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0]=newUUID(0x1101);
uuidSet[1]=ECHO_SERVER_UUID;
discoveryAgent.startInquiry(DiscoveryAgent.GIAC,this);
}catch(BluetoothStateExceptione){
//阻塞,由inquiryCompleted()回调方法唤醒
wait();
}catch(java/lang/InterruptedException.java.html"
InterruptedExceptione1){
e1.printStackTrace();
设备搜索完毕,共找到"
+devices.size()+"
个设备,开始搜索服务"
transIDs=newint[devices.size()];
for(inti=0;
i<
devices.size();
i++){
RemoteDevicerd=(RemoteDevice)devices.elementAt(i);
//记录每一次服务搜索的事务id
transIDs[i]=discoveryAgent.searchServices(null,uuidSet,
rd,this);
continue;
//阻塞,由serviceSearchCompleted()回调方法在所有设备都搜索完的情况下唤醒
服务搜索完毕,共找到"
+records.size()+"
个服务,准备发送请求"
if(records.size()>
0){
this.append(input);
发送"
Command.OK,0));
*debug
*@params
privatevoidshowInfo(java/lang/String.java.html"
Strings){
java/lang/StringBuffer.java.html"
StringBuffersb=newjava/lang/StringBuffer.java.html"
StringBuffer(result.getText());
if(sb.length()>
sb.append("
\n"
sb.append(s);
result.setText(sb.toString());
*回调方法
publicvoiddeviceDiscovered(RemoteDevicebtDevice,DeviceClasscod){
if(devices.indexOf(btDevice)==-1){
devices.addElement(btDevice);
*回调方法,唤醒初始化线程
publicvoidinquiryCompleted(intdiscType){
synchronized(this){
notify();
*回调方
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- J2ME蓝牙实战入实例代码和参考资料可实现两人或多人通过蓝牙通信的游戏对弈Sun Java Wireless Toolkit J2ME
![提示](https://static.bdocx.com/images/bang_tan.gif)
链接地址:https://www.bdocx.com/doc/22818030.html