J2ME蓝牙程序开发实战入门

分类: J2ME   出处:iocblog整理  更新时间:2009-02-01   添加到收藏  


  概述
  
  目前,很多手机已经具备了蓝牙功能。虽然midp2.0没有包括蓝牙api,但是jcp定义了jsr82, java apis for bluetooth wireless technology (jabwt).这是一个可选api,很多支持midp2.0的手机已经实现了,比如nokia 6600, nokia 6670,nokia7610等等。对于一个开发者来说,如果目标平台支持jsr82的话,在制作联网对战类型游戏或者应用的时候,蓝牙是一个相当不错的选择。本文给出了一个最简单的蓝牙应用的j2me程序,用以帮助开发者快速的掌握jsr82。该程序分别在2台蓝牙设备上安装后,一台设备作为服务端先运行,一台设备作为客户端后运行。在服务端上我们发布了一个服务,该服务的功能是把客户端发过来的字符串转变为大写字符串。客户端起动并搜索到服务端的服务后,我们就可以从客户端的输入框里输入任意的字符串,发送到服务端去,同时观察服务端的反馈结果。
  
  本文并不具体讲述蓝牙的运行机制和jsr82的api结构,关于这些知识点,请参考本文的参考资料一节,这些参考资料会给你一个权威的精确的解释。
  
  实例代码
  
  该程序包括3个java文件。一个是midlet,另外2个为服务端gui和客户端gui。该程序已经在wtk22模拟器和nokia 6600,nokia 6670两款手机上测试通过。
  
  stupidbtmidlet.java
  
  import javax.microedition.lcdui.alert;
  import javax.microedition.lcdui.alerttype;
  import javax.microedition.lcdui.command;
  import javax.microedition.lcdui.commandlistener;
  import javax.microedition.lcdui.display;
  import javax.microedition.lcdui.displayable;
  import javax.microedition.lcdui.list;
  import javax.microedition.midlet.midlet;
  import javax.microedition.midlet.midletstatechangeexception;
  
  /**
  * @author jagie
  *
  * midlet
  */
  public class stupidbtmidlet extends midlet implements commandlistener {
  list list;
  
  serverbox sb;
  
  clientbox cb;
  
  /*
  * (non-javadoc)
  *
  * @see javax.microedition.midlet.midlet#startapp()
  */
  protected void startapp() throws midletstatechangeexception {
  list = new list("傻瓜蓝牙入门", list.implicit);
  list.append("client", null);
  list.append("server", null);
  list.setcommandlistener(this);
  display.getdisplay(this).setcurrent(list);
  
  }
  
  /**
  * debug方法
  * @param s 要显示的字串
  */
  
  public void showstring(string s) {
  displayable dp = display.getdisplay(this).getcurrent();
  alert al = new alert(null, s, null, alerttype.info);
  al.settimeout(2000);
  display.getdisplay(this).setcurrent(al, dp);
  }
  
  /**
  * 显示主菜单
  *
  */
  
  public void showmainmenu() {
  display.getdisplay(this).setcurrent(list);
  }
  
  protected void pauseapp() {
  // todo auto-generated method stub
  
  }
  
  public void commandaction(command com, displayable disp) {
  if (com == list.select_command) {
  list list = (list) disp;
  int index = list.getselectedindex();
  if (index == 1) {
  if (sb == null) {
  sb = new serverbox(this);
  }
  sb.setstring(null);
  display.getdisplay(this).setcurrent(sb);
  } else {
  //每次都生成新的客户端实例
  cb = null;
  system.gc();
  cb = new clientbox(this);
  
  display.getdisplay(this).setcurrent(cb);
  }
  }
  }
  
  protected void destroyapp(boolean arg0) throws midletstatechangeexception {
  // todo auto-generated method stub
  
  }
  
  }
  clientbox.java
  
  import java.io.datainputstream;
  import java.io.dataoutputstream;
  import java.io.ioexception;
  
  import java.util.vector;
  
  import javax.microedition.io.connector;
  import javax.microedition.io.streamconnection;
  import javax.microedition.lcdui.command;
  import javax.microedition.lcdui.commandlistener;
  import javax.microedition.lcdui.displayable;
  import javax.microedition.lcdui.form;
  import javax.microedition.lcdui.gauge;
  import javax.microedition.lcdui.stringitem;
  import javax.microedition.lcdui.textfield;
  
  //jsr082 api
  import javax.bluetooth.bluetoothstateexception;
  
  import javax.bluetooth.deviceclass;
  import javax.bluetooth.discoveryagent;
  import javax.bluetooth.discoverylistener;
  import javax.bluetooth.localdevice;
  import javax.bluetooth.remotedevice;
  import javax.bluetooth.servicerecord;
  import javax.bluetooth.uuid;
  

[1] [2] 下一页


Tag: 蓝牙程序



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。