📄 bluetoothclient.java
字号:
package abc;/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Vector;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;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.pim.Contact;import javax.microedition.pim.ContactList;import javax.microedition.pim.PIM;import javax.microedition.pim.PIMException;import javax.microedition.pim.PIMItem;/** * @author Administrator */public class BlueToothClient extends MIDlet implements CommandListener,DiscoveryListener { private boolean midletPaused = false; static final String DEBUG_address = "0013FDC157C8"; // N6630 protected UUID uuid = new UUID(0x1101); // serial port profile protected int inquiryMode = DiscoveryAgent.GIAC; // no pairing is needed protected int connectionOptions = ServiceRecord.NOAUTHENTICATE_NOENCRYPT; OutputStream out; InputStream in; StreamConnection stream; Vector deviceListVec = new Vector(); PIM pim = PIM.getInstance(); ContactList cl = null; Contact contact=null; Enumeration s_contacts = null; String UserAction=""; StringTokenizer token = new StringTokenizer(""); /* Thread references */ Thread browseContact=null; Thread contactSynchroninzing=null; Thread contactRestoring=null; //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0| private Form logForm; private List deviceList; private List menuList; private List contactList; private Alert deviceAlert; private Alert waitBrowseAlert; private Alert waitSynchronizeAlert; private Command Send; private Command Log; private Command screenCommand; private Command backCommand; private Command exitCommand; private Command Connect; private Command Log1; private Command backCommand1; private Command screenCommand1; private Command Sychronize; private Command backCommand2; private Command Log2; private Command screenCommand2; private Command exitCommand2; private Command backCommand3; private Command exitCommand1; private Command exitCommand3; private Command screenCommand3; //</editor-fold>//GEN-END:|fields|0| /** * The BlueToothClient constructor. */ public BlueToothClient() { } //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0| //</editor-fold>//GEN-END:|methods|0| //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize /** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. */ private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize // write pre-initialize user code here//GEN-LINE:|0-initialize|1|0-postInitialize // write post-initialize user code here }//GEN-BEGIN:|0-initialize|2| //</editor-fold>//GEN-END:|0-initialize|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction // write pre-action user code here switchDisplayable(null, getMenuList());//GEN-LINE:|3-startMIDlet|1|3-postAction // write post-action user code here }//GEN-BEGIN:|3-startMIDlet|2| //</editor-fold>//GEN-END:|3-startMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction // write pre-action user code here//GEN-LINE:|4-resumeMIDlet|1|4-postAction // write post-action user code here }//GEN-BEGIN:|4-resumeMIDlet|2| //</editor-fold>//GEN-END:|4-resumeMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch /** * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch // write pre-switch user code here Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); }//GEN-END:|5-switchDisplayable|1|5-postSwitch // write post-switch user code here }//GEN-BEGIN:|5-switchDisplayable|2| //</editor-fold>//GEN-END:|5-switchDisplayable|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction /** * Called by a system to indicated that a command has been invoked on a particular displayable. * @param command the Command that was invoked * @param displayable the Displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction // write pre-action user code here if (displayable == contactList) {//GEN-BEGIN:|7-commandAction|1|53-preAction if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|53-preAction // write pre-action user code here contactListAction();//GEN-LINE:|7-commandAction|2|53-postAction // write post-action user code here } else if (command == Log2) {//GEN-LINE:|7-commandAction|3|58-preAction // write pre-action user code here switchDisplayable(null, getLogForm());//GEN-LINE:|7-commandAction|4|58-postAction // write post-action user code here } else if (command == backCommand2) {//GEN-LINE:|7-commandAction|5|60-preAction // write pre-action user code here switchDisplayable(null, getMenuList());//GEN-LINE:|7-commandAction|6|60-postAction // write post-action user code here } else if (command == exitCommand3) {//GEN-LINE:|7-commandAction|7|78-preAction // write pre-action user code here notifyDestroyed();//GEN-LINE:|7-commandAction|8|78-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|9|34-preAction } else if (displayable == deviceList) { if (command == Connect) {//GEN-END:|7-commandAction|9|34-preAction // write pre-action user code here try{ startServiceSearch((RemoteDevice) deviceListVec.elementAt(getDeviceList().getSelectedIndex())); }catch(Exception e){ log(e); }//GEN-LINE:|7-commandAction|10|34-postAction // write post-action user code here switchDisplayable(getWaitSynchronizeAlert(),menuList); } else if (command == List.SELECT_COMMAND) {//GEN-LINE:|7-commandAction|11|31-preAction // write pre-action user code here deviceListAction();//GEN-LINE:|7-commandAction|12|31-postAction // write post-action user code here } else if (command == Log1) {//GEN-LINE:|7-commandAction|13|38-preAction // write pre-action user code here switchDisplayable(null, getLogForm());//GEN-LINE:|7-commandAction|14|38-postAction // write post-action user code here } else if (command == exitCommand2) {//GEN-LINE:|7-commandAction|15|70-preAction // write pre-action user code here notifyDestroyed();//GEN-LINE:|7-commandAction|16|70-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|17|25-preAction } else if (displayable == logForm) { if (command == backCommand) {//GEN-END:|7-commandAction|17|25-preAction // write pre-action user code here switchDisplayable(null, getContactList());//GEN-LINE:|7-commandAction|18|25-postAction // write post-action user code here } else if (command == backCommand1) {//GEN-LINE:|7-commandAction|19|41-preAction // write pre-action user code here switchDisplayable(null, getDeviceList());//GEN-LINE:|7-commandAction|20|41-postAction // write post-action user code here } else if (command == backCommand3) {//GEN-LINE:|7-commandAction|21|72-preAction // write pre-action user code here switchDisplayable(null, getMenuList());//GEN-LINE:|7-commandAction|22|72-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|23|45-preAction } else if (displayable == menuList) { if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|23|45-preAction // write pre-action user code here menuListAction();//GEN-LINE:|7-commandAction|24|45-postAction // write post-action user code here } else if (command == exitCommand1) {//GEN-LINE:|7-commandAction|25|65-preAction // write pre-action user code here notifyDestroyed();//GEN-LINE:|7-commandAction|26|65-postAction // write post-action user code here } else if (command == screenCommand3) {//GEN-LINE:|7-commandAction|27|75-preAction // write pre-action user code here switchDisplayable(null, getLogForm());//GEN-LINE:|7-commandAction|28|75-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|29|7-postCommandAction }//GEN-END:|7-commandAction|29|7-postCommandAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|30| //</editor-fold>//GEN-END:|7-commandAction|30| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Send ">//GEN-BEGIN:|14-getter|0|14-preInit /** * Returns an initiliazed instance of Send component. * @return the initialized component instance */ public Command getSend() { if (Send == null) {//GEN-END:|14-getter|0|14-preInit // write pre-init user code here Send = new Command("Send", Command.SCREEN, 0);//GEN-LINE:|14-getter|1|14-postInit // write post-init user code here }//GEN-BEGIN:|14-getter|2| return Send; } //</editor-fold>//GEN-END:|14-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: logForm ">//GEN-BEGIN:|17-getter|0|17-preInit /** * Returns an initiliazed instance of logForm component. * @return the initialized component instance */ public Form getLogForm() { if (logForm == null) {//GEN-END:|17-getter|0|17-preInit // write pre-init user code here logForm = new Form("Log Details");//GEN-BEGIN:|17-getter|1|17-postInit logForm.addCommand(getBackCommand()); logForm.addCommand(getBackCommand1()); logForm.addCommand(getBackCommand3()); logForm.setCommandListener(this);//GEN-END:|17-getter|1|17-postInit // write post-init user code here }//GEN-BEGIN:|17-getter|2| return logForm; } //</editor-fold>//GEN-END:|17-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Log ">//GEN-BEGIN:|19-getter|0|19-preInit /** * Returns an initiliazed instance of Log component. * @return the initialized component instance */ public Command getLog() { if (Log == null) {//GEN-END:|19-getter|0|19-preInit // write pre-init user code here Log = new Command("Log", Command.SCREEN, 0);//GEN-LINE:|19-getter|1|19-postInit // write post-init user code here }//GEN-BEGIN:|19-getter|2| return Log; } //</editor-fold>//GEN-END:|19-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand ">//GEN-BEGIN:|22-getter|0|22-preInit /** * Returns an initiliazed instance of screenCommand component. * @return the initialized component instance */ public Command getScreenCommand() { if (screenCommand == null) {//GEN-END:|22-getter|0|22-preInit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -