⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientmidlet.java

📁 功能更强的基于J2ME的蓝牙程序
💻 JAVA
字号:
/*
 * @(#)DemoMIDlet.java	1.1 04/04/24
 *
 * Copyright (c) 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms
 */
package example.bluetooth.demo;
import javax.microedition.midlet.MIDlet;
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.lcdui.Image;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.StringItem;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Contains the Bluetooth API demo, that allows to download
 * the specific images from the other devices.
 *
 * @author Vladimir K. Beliaev
 * @version 1.1, 04/24/04
 */
public final class ClientMIDlet extends MIDlet implements CommandListener {
    
    /** The messages are shown in this demo this amount of time. */
    static final int ALERT_TIMEOUT = 2000;
    
    /** This command goes to demo main screen. */
    private final Command exitCommand = new Command("Exit", Command.EXIT,
            2);
    
    /** Starts the proper services search. */
    private final Command SCR_MAIN_SEARCH_CMD = new Command("Find", Command.OK,
            1);
    
    /** Cancels the device/services discovering. */
    private final Command SCR_SEARCH_CANCEL_CMD = new Command("Cancel",
            Command.BACK, 2);
    
    /** This command goes to client main screen. */
    private final Command SCR_IMAGES_BACK_CMD = new Command("Back",
            Command.BACK, 2);
    
    /** Start the chosen image download. */
    private final Command SCR_IMAGES_LOAD_CMD = new Command("Load", Command.OK,
            1);
    
    /** Cancels the image download. */
    private final Command SCR_LOAD_CANCEL_CMD = new Command("Cancel",
            Command.BACK, 2);
    
    /** This command goes from image screen to images list one. */
    private final Command SCR_SHOW_BACK_CMD = new Command("Back", Command.BACK,
            2);
    
    /** The main screen of the client part. */
    private final Form mainScreen = new Form("Image Viewer");
    
    /** The screen with found images names. */
    private final List listScreen = new List("Image Viewer", List.IMPLICIT);
    
    /** The screen with download image. */
    private final Alert imageScreen = new Alert("Image Viewer");
    
    /** This object handles the real transmission. */
    private BTImageClient bt_client;
    

    
    /**
     * Process the command events.
     *
     * @param c - the issued command.
     * @param d - the screen object the command was issued for.
     */
    public void commandAction(Command c, Displayable d) {
        
        // back to demo main screen
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
            return;
        }
        
        // starts images (device/services) search
        if (c == SCR_MAIN_SEARCH_CMD) {
            Form f = new Form("Searching...");
            f.addCommand(SCR_SEARCH_CANCEL_CMD);
            f.setCommandListener(this);
            f.append(new Gauge("Searching images...", false, Gauge.INDEFINITE,
                    Gauge.CONTINUOUS_RUNNING));
            Display.getDisplay(this).setCurrent(f);
            bt_client.requestSearch();
            return;
        }
        
        // cancels device/services search
        if (c == SCR_SEARCH_CANCEL_CMD) {
            bt_client.cancelSearch();
            Display.getDisplay(this).setCurrent(mainScreen);
            return;
        }
        
        // back to client main screen
        if (c == SCR_IMAGES_BACK_CMD) {
            bt_client.requestLoad(null);
            Display.getDisplay(this).setCurrent(mainScreen);
            return;
        }
        
        // starts image download
        if (c == SCR_IMAGES_LOAD_CMD) {
            Form f = new Form("Loading...");
            f.addCommand(SCR_LOAD_CANCEL_CMD);
            f.setCommandListener(this);
            f.append(new Gauge("Loading image...", false, Gauge.INDEFINITE,
                    Gauge.CONTINUOUS_RUNNING));
            Display.getDisplay(this).setCurrent(f);
            List l = (List) d;
            bt_client.requestLoad(l.getString(l.getSelectedIndex()));
            return;
        }
        
        // cancels image load
        if (c == SCR_LOAD_CANCEL_CMD) {
            bt_client.cancelLoad();
            Display.getDisplay(this).setCurrent(listScreen);
            return;
        }
        
        // back to client main screen
        if (c == SCR_SHOW_BACK_CMD) {
            Display.getDisplay(this).setCurrent(listScreen);
            return;
        }
    }
    
    /**
     * We have to provide this method due to "do not do network
     * operation in command listener method" restriction, which
     * is caused by crooked midp design.
     *
     * This method is called by BTImageClient after it is done
     * with bluetooth initialization and next screen is ready
     * to appear.
     */
    void completeInitialization(boolean isBTReady) {
        
        // bluetooth was initialized successfully.
        if (isBTReady) {
            StringItem si = new StringItem("Ready for images search!", null);
            si.setLayout(StringItem.LAYOUT_CENTER | StringItem.LAYOUT_VCENTER);
            mainScreen.append(si);
            Display.getDisplay(this).setCurrent(mainScreen);
            return;
        }
        
        // something wrong
        Alert al = new Alert("Error", "Can't inititialize bluetooth", null,
                AlertType.ERROR);
        al.setTimeout(ClientMIDlet.ALERT_TIMEOUT);
        Display.getDisplay(this).setCurrent(al, mainScreen);
    }
    
    /** Destroys this component. */
    void destroy() {
        
        // finilize the image client work
        bt_client.destroy();
    }
    
    /**
     * Informs the error during the images search.
     */
    void informSearchError(String resMsg) {
        Alert al = new Alert("Error", resMsg, null, AlertType.ERROR);
        al.setTimeout(ClientMIDlet.ALERT_TIMEOUT);
        Display.getDisplay(this).setCurrent(al, mainScreen);
    }
    
    /**
     * Informs the error during the selected image load.
     */
    void informLoadError(String resMsg) {
        Alert al = new Alert("Error", resMsg, null, AlertType.ERROR);
        al.setTimeout(ClientMIDlet.ALERT_TIMEOUT);
        Display.getDisplay(this).setCurrent(al, listScreen);
    }
    
    /**
     * Shows the downloaded image.
     */
    void showImage(Image img) {
        imageScreen.setImage(img);
        Display.getDisplay(this).setCurrent(imageScreen);
    }
    
    /**
     * Shows the available images names.
     *
     * @returns false if no images names were found actually
     */
    boolean showImagesNames(Hashtable base) {
        Enumeration keys = base.keys();
        
        // no images actually
        if (!keys.hasMoreElements()) {
            informSearchError("No images names in found services");
            return false;
        }
        
        // prepare the list to be shown
        while (listScreen.size() != 0) {
            listScreen.delete(0);
        }
        
        while (keys.hasMoreElements()) {
            listScreen.append((String) keys.nextElement(), null);
        }
        Display.getDisplay(this).setCurrent(listScreen);
        return true;
    }
    
    /**
     * Constructs main screen of the MIDlet.
     */
    public ClientMIDlet() {
        bt_client = new BTImageClient(this);
        mainScreen.addCommand(exitCommand);
        mainScreen.addCommand(SCR_MAIN_SEARCH_CMD);
        mainScreen.setCommandListener(this);
        listScreen.addCommand(SCR_IMAGES_BACK_CMD);
        listScreen.addCommand(SCR_IMAGES_LOAD_CMD);
        listScreen.setCommandListener(this);
        imageScreen.addCommand(SCR_SHOW_BACK_CMD);
        imageScreen.setCommandListener(this);
        imageScreen.setTimeout(Alert.FOREVER);
    }
    
    /**
     * Creates the demo view and action buttons.
     */
    public void startApp() {
       Display.getDisplay(this).setCurrent(mainScreen);
    }
    
    /**
     * Destroys the application.
     */
    protected void destroyApp(boolean unconditional) {
    }
    
    /**
     * Does nothing. Redefinition is required by MIDlet class.
     */
    protected void pauseApp() {}    
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -