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

📄 btimageclient.java

📁 一个利用手机上的bluetooth来开发的小程序 也是很经典的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                isDownloadCanceled = false;                // ok, wait for download or need to wait for next search                try {                    wait();                } catch (InterruptedException e) {                    System.err.println("Unexpected interuption: " + e);                    return;                }                // check the component is destroyed                if (isClosed) {                    return;                }                // this means "go to begining"                if (imageNameToLoad == null) {                    break;                }                // load selected image data                Image img = loadImage();                // FIXME: this never happen - monitor is taken...                if (isClosed) {                    return;                }                if (isDownloadCanceled) {                    continue; // may be next image to be download                }                if (img == null) {                    parent.informLoadError("Can't load image: "                            + imageNameToLoad);                    continue; // may be next image to be download                }                // ok, show image to user                parent.showImage(img);                // may be next image to be download                continue;            }        }    }    /**     * Search for bluetooth devices.     *     * @return false if should end the component work.     */    private boolean searchDevices() {        // ok, start a new search then        state = DEVICE_SEARCH;        devices.removeAllElements();        try {            discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);        } catch (BluetoothStateException e) {            System.err.println("Can't start inquiry now: " + e);            parent.informSearchError("Can't start device search");            return true;        }        try {            wait(); // until devices are found        } catch (InterruptedException e) {            System.err.println("Unexpected interuption: " + e);            return false;        }        // this "wake up" may be caused by 'destroy' call        if (isClosed) {            return false;        }        // no?, ok, let's check the return code then        switch (discType) {        case INQUIRY_ERROR:            parent.informSearchError("Device discovering error...");            // fall through        case INQUIRY_TERMINATED:            // make sure no garbage in found devices list            devices.removeAllElements();            // nothing to report - go to next request            break;        case INQUIRY_COMPLETED:            if (devices.size() == 0) {                parent.informSearchError("No devices in range");            }            // go to service search now            break;        default:            // what kind of system you are?... :(            System.err.println("system error:"                    + " unexpected device discovery code: " + discType);            destroy();            return false;        }        return true;    }    /**     * Search for proper service.     *     * @return false if should end the component work.     */    private boolean searchServices() {        state = SERVICE_SEARCH;        records.removeAllElements();        searchIDs = new int[devices.size()];        boolean isSearchStarted = false;        for (int i = 0; i < devices.size(); i++) {            RemoteDevice rd = (RemoteDevice) devices.elementAt(i);            try {                searchIDs[i] = discoveryAgent.searchServices(attrSet, uuidSet,                        rd, this);            } catch (BluetoothStateException e) {                System.err.println("Can't search services for: "                        + rd.getBluetoothAddress() + " due to " + e);            }            isSearchStarted = true;        }        // at least one of the services search should be found        if (!isSearchStarted) {            return true;        }        try {            wait(); // until services are found        } catch (InterruptedException e) {            System.err.println("Unexpected interuption: " + e);            return false;        }        // this "wake up" may be caused by 'destroy' call        if (isClosed) {            return false;        }        // actually, no services were found        if (records.size() == 0) {            parent.informSearchError("No proper services were found");        }        return true;    }    /**     * Gets the collection of the images titles (names)     * from the services, prepares a hashtable to match     * the image name to a services list, presents the images names     * to user finally.     *     * @return false if no names in found services.     */    private boolean presentUserSearchResults() {        base.clear();        for (int i = 0; i < records.size(); i++) {            ServiceRecord sr = (ServiceRecord) records.elementAt(i);            // get the attribute with images names            DataElement de = sr.getAttributeValue(IMAGES_NAMES_ATTRIBUTE_ID);            if (de == null) {                System.err.println("Unexpected service - missed attribute");                continue;            }            // get the images names from this attribute            Enumeration en = (Enumeration) de.getValue();            while (en.hasMoreElements()) {                de = (DataElement) en.nextElement();                String name = (String) de.getValue();                // name may be stored already                Object obj = base.get(name);                // that's either the ServiceRecord or Vector                if (obj != null) {                    Vector v;                    if (obj instanceof ServiceRecord) {                        v = new Vector();                        v.addElement(obj);                    } else {                        v = (Vector) obj;                    }                    v.addElement(sr);                    obj = v;                } else {                    obj = sr;                }                base.put(name, obj);            }        }        return parent.showImagesNames(base);    }    /**     * Loads selected image data.     */    private Image loadImage() {        if (imageNameToLoad == null) {            System.err.println("Error: imageNameToLoad=null");            return null;        }        // ok, get the list of service records        ServiceRecord[] sr = null;        Object obj = base.get(imageNameToLoad);        if (obj == null) {            System.err.println("Error: no record for: " + imageNameToLoad);            return null;        } else if (obj instanceof ServiceRecord) {            sr = new ServiceRecord[] { (ServiceRecord) obj };        } else {            Vector v = (Vector) obj;            sr = new ServiceRecord[v.size()];            for (int i = 0; i < v.size(); i++) {                sr[i] = (ServiceRecord) v.elementAt(i);            }        }        // now try to load the image from each services one by one        for (int i = 0; i < sr.length; i++) {            StreamConnection conn = null;            String url = null;            // the process may be canceled            if (isDownloadCanceled) {                return null;            }            // first - connect            try {                url = sr[i].getConnectionURL(                        ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);                conn = (StreamConnection) Connector.open(url);            } catch (IOException e) {                System.err.println("Note: can't connect to: " + url);                // ignore                continue;            }            // then open a steam and write a name            try {                OutputStream out = conn.openOutputStream();                out.write(imageNameToLoad.length()); // length is 1 byte                out.write(imageNameToLoad.getBytes());                out.close();            } catch (IOException e) {                System.err.println("Can't write to server for: " + url);                // close stream connection                try {                    conn.close();                } catch (IOException ee) {} // ignore                continue;            }            // then open a steam and read an image            byte[] imgData = null;            try {                InputStream in = conn.openInputStream();                // read a length first                int length = in.read() << 8;                length |= in.read();                if (length <= 0) {                    throw new IOException("Can't read a length");                }                // read the image now                imgData = new byte[length];                length = 0;                while (length != imgData.length) {                    int n = in.read(imgData, length, imgData.length - length);                    if (n == -1) {                        throw new IOException("Can't read a image data");                    }                    length += n;                }                in.close();            } catch (IOException e) {                System.err.println("Can't read from server for: " + url);                continue;            } finally {                // close stream connection anyway                try {                    conn.close();                } catch (IOException e) {} // ignore            }            // ok, may it's a chance            Image img = null;            try {                img = Image.createImage(imgData, 0, imgData.length);            } catch (Exception e) {                // may be next time                System.err.println("Error: wrong image data from: " + url);                continue;            }            return img;        }        return null;    }} // end of class 'BTImageClient' definition

⌨️ 快捷键说明

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