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

📄 printimageclient.java

📁 一个利用手机上的bluetooth来开发的小程序 也是很经典的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)ObexDemoMIDlet.java	1.7 04/01/30
 *
 * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms
 */
package example.bluetooth.demo;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
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 java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

/**
 * @version 1.7
 */
public final class PrintImageClient extends MIDlet implements
        CommandListener {

    /** Soft button for exiting the demo. */
    private final Command exitCmd = new Command("Exit", Command.EXIT, 1);

    /** Soft button for stopping download and returning to the mane screen. */
    private final Command stopCmd = new Command("Stop", Command.BACK, 1);
    
    /** Soft button for stopping download and returning to the mane screen. */
    private final Command backCmd = new Command("Back", Command.BACK, 1);
    
    /** Soft button make choise of printed picture. */
    private final Command selectCmd = new Command("Select", Command.ITEM, 1);

    /** A menu list instance */
    private final List pictureList = new List("Print Image Bluetooth Demo",
             List.IMPLICIT);

    /** A menu list instance */
    private final List printerList = new List("Print Image Bluetooth Demo",
             List.IMPLICIT);

    /** Form to show current state of seding a pictire */
    private final Form senderForm = new Form("Send Image");

    /** Gauge to indicate progress of downloading process */
    private final Gauge sendingItem = new Gauge("", false, 256, 0);

    /** Shows that demo was paused */
    private boolean isPaused;

    /** Container of images file names */
    private Vector pictureNames = new Vector();
    
    /** Container of printers bluetooth addresses */
    private Vector printerRecords= new Vector();

    /** Class to found available printers*/
    private PrinterFinder finder = null;
    
    /** Class to send image file to printer*/
    private BTSPPImageSender sender = null;
    
    public PrintImageClient() {
        pictureList.setCommandListener(this);
        pictureList.addCommand(exitCmd);
        pictureList.addCommand(selectCmd);
        initPictureList();
        printerList.setCommandListener(this);
        printerList.addCommand(backCmd);
        printerList.addCommand(selectCmd);
        senderForm.append(sendingItem);
        senderForm.addCommand(stopCmd);
        senderForm.setCommandListener(this);
        finder = new PrinterFinder(this);
        sender = new BTSPPImageSender(this);
    }

    public boolean isPaused() {
        return isPaused;
    }

    public void startApp() {
        isPaused = false;
        showPictureList();
    }

    public void pauseApp() {
        isPaused = true;
    }

    public void destroyApp(boolean unconditional) {
        stop();
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCmd) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == backCmd) {
            stop();
            showPictureList();
        } else if (c == stopCmd) {
            stop();
            showPictureList();
        } else if ((c == selectCmd) || (c == List.SELECT_COMMAND)) {
            if (Display.getDisplay(this).getCurrent() == pictureList) {
                initPrinterList();
            } else {
                uploadPicture();
            }
        }
    }


    /**
     * Shows list with image names to select one  for printing
     */
    void showPictureList() {
        Display.getDisplay(this).setCurrent(pictureList);
    }

    /** Create list of available image */
    private void initPictureList() {
        for (int n = 1; n < 100; n++) {
            String nthImage = "ImageName-" + n;
            String image = getAppProperty(nthImage);

            if (image == null || image.length() == 0) {
                break;
            }
            String nthTitle = "ImageTitle-" + n;
            String title = getAppProperty(nthTitle);

            if (title == null || title.length() == 0) {
                title = image;
            }
            pictureNames.addElement(image);
            pictureList.append(title, null);
        }
    }
    
    /**
     * Shows list with found printer services
     */
    void showPrinterList() {
        synchronized (printerRecords) {
            printerList.deleteAll();
            printerRecords.removeAllElements();
        }
        Display.getDisplay(this).setCurrent(printerList);
        //initPrinterList();
    }
    
    void updatePrinterList(ServiceRecord serviceRecord) {
        RemoteDevice dev = serviceRecord.getHostDevice();
        String btAddress = dev.getBluetoothAddress();
        
        synchronized (printerRecords) {
            printerRecords.addElement(serviceRecord);
            printerList.append(btAddress, null);
        }
    }

    /** Create list of available printers */
    private void initPrinterList() {
        finder.start();
     }
    
    /**
     * Shows progress of image uploading
     */
    void showProgress(String label, int maxValue) {
        sendingItem.setLabel(label);
        sendingItem.setValue(0);
        sendingItem.setMaxValue(maxValue);
        Display.getDisplay(this).setCurrent(senderForm);
    }

    /**
     * Update progress of image uploading
     */
    void updateProgress(int value) {
        sendingItem.setValue(value);
    }

    private void uploadPicture() {
        int index = pictureList.getSelectedIndex();
        String pictureName = (String) pictureNames.elementAt(index);
        ServiceRecord printerRecord;

        synchronized (printerRecords) {
            index = printerList.getSelectedIndex();
            
            if(index == -1) {
                return;
            }
            printerRecord = (ServiceRecord) printerRecords.elementAt(index);
        }

        finder.stopDiscover();
        sender.start(pictureName, printerRecord);
    }
    
    private void stop() {
        finder.stopDiscover();
        sender.stop();
    }
}

final class PrinterFinder implements DiscoveryListener {
    
    private static final UUID PICTURE_PRINT_UUID =
            new UUID("53756E57544B495053164704B78CE5D1", false);
    
    /** Reference to GUI part of sender */
    private PrintImageClient gui = null;
    
    /** Agent to start and stop discover */
    private DiscoveryAgent da = null;
    
    private Object initLock = new Object();
    private boolean initStarted = false;
    private boolean devDisStarted = false;
    private Hashtable foundDevs = null;
    private Hashtable startedReqs = null;
    

    PrinterFinder(PrintImageClient gui) {
        this.gui = gui;
    }
    
    void start() {
        try {
            gui.showPrinterList();
            findServices();
        } catch (BluetoothStateException bse) {
            // FIXME:
            bse.printStackTrace();
            gui.showPictureList();
        }
    }
    
    void stopDiscover() {
        Enumeration en;
        Hashtable reqs;
        
        synchronized (initLock) {
            if (!initStarted) {
                return;
            }
            reqs = startedReqs;
        }
        da.cancelInquiry(this);
        
        synchronized (reqs) {
            en = reqs.keys();
            

⌨️ 快捷键说明

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