📄 printimageclient.java
字号:
while (en.hasMoreElements()) { Integer reqHandle = (Integer) en.nextElement(); da.cancelServiceSearch(reqHandle.intValue()); } } } private void findServices() throws BluetoothStateException { RemoteDevice[] devs = null; synchronized (initLock) { if (initStarted) { throw new BluetoothStateException( "The previous device discovery is running..."); } initStarted = true; devDisStarted = true; this.foundDevs = new Hashtable(); this.startedReqs = new Hashtable(); } da = LocalDevice.getLocalDevice().getDiscoveryAgent(); devs = da.retrieveDevices(DiscoveryAgent.PREKNOWN); if (devs != null) { for (int i = 0; i < devs.length; i++) { findService(devs[i], foundDevs, startedReqs); } } devs = da.retrieveDevices(DiscoveryAgent.CACHED); if (devs != null) { for (int i = 0; i < devs.length; i++) { findService(devs[i], foundDevs, startedReqs); } } try { da.startInquiry(DiscoveryAgent.GIAC, this); } catch (BluetoothStateException btse) { synchronized (initLock) { initStarted = false; devDisStarted = false; this.foundDevs = null; this.startedReqs = null; } throw btse; } } private void findService(RemoteDevice dev, Hashtable devs, Hashtable reqs) throws BluetoothStateException { /* if this bloototh device was found in preknown or * cached devices skips it now. */ synchronized (devs) { if (devs.put(dev,dev) != null) { return; } } UUID[] uuidSet = new UUID[] {PICTURE_PRINT_UUID}; int transID = da.searchServices(null, uuidSet, dev, this); Integer transHandle = new Integer(transID); synchronized (reqs) { reqs.put( transHandle, transHandle); } } public void deviceDiscovered(RemoteDevice device, DeviceClass cod) { try { findService(device, foundDevs, startedReqs); } catch (BluetoothStateException bse) { bse.printStackTrace(); } } public void inquiryCompleted(int discType) { synchronized (initLock) { devDisStarted = false; if ((startedReqs == null) || (startedReqs.isEmpty())) { initStarted = false; this.foundDevs = null; this.startedReqs = null; } } } public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { for (int i = 0; i < servRecord.length; i++) { gui.updatePrinterList(servRecord[i]); } } public void serviceSearchCompleted(int transID, int respCode) { synchronized (startedReqs) { startedReqs.remove(new Integer(transID)); } synchronized (initLock) { if ((devDisStarted == false) && (startedReqs.isEmpty())) { initStarted = false; this.foundDevs = null; this.startedReqs = null; } } }}final class BTSPPImageSender implements Runnable { /** Shows if debug prints should be done. */ private static final boolean DEBUG = false; /** DEBUG only: keeps the class name for debug. */ private static final String cn = "ObexImageSender"; /** Indicate that uploading should be stopped */ //private boolean stop = true; /** Stream with image data */ private InputStream imageSource = null; /** Special stream to load image data to byte array */ private ByteArrayOutputStream baos = null; /** Output stream of obex */ private OutputStream outputStream = null; /** Array with image data */ private byte[] imageData = null; /** Contains name of uploading image */ private String pictureName = null; private ServiceRecord printerRecord = null; /** Reference to GUI part of sender */ private PrintImageClient gui = null; private boolean stop = false; BTSPPImageSender(PrintImageClient gui) { this.gui = gui; } void start(String pictureName, ServiceRecord printerRecord) { this.pictureName = pictureName; this.printerRecord = printerRecord; (new Thread(this)).start(); } void stop() { stop = true; close(); } /** * Used to send an image */ public void run() { boolean loaded = false; boolean connected = false; stop = false; try { loadImageData(pictureName); loaded = true; connect(); connected = true; uploadImage(pictureName); } catch (IOException e) { if (DEBUG) { e.printStackTrace(); } /* if (!stop) { if (connected) { gui.stopMessage(); } else if (loaded) { gui.notReadyMessage(); } else { gui.errorMessage(); } closeAll(); return; } */ } catch (Throwable e) { e.printStackTrace(); } close(); gui.showPictureList(); } /** load image data to array */ private void loadImageData(String pictureName) throws IOException { imageSource = getClass().getResourceAsStream(pictureName); // read image data and create a byte array byte[] buff = new byte[1024]; baos = new ByteArrayOutputStream(1024); while (true) { // check stop signal if (stop) { throw new IOException(); } int length = imageSource.read(buff); if (length == -1) { break; } baos.write(buff, 0, length); } imageData = baos.toByteArray(); } /** Connects with image receiver */ private void connect() throws IOException { String url = printerRecord.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // update the Gauge message before connecting gui.showProgress("Connecting ...", imageData.length); StreamConnection streamConnection; synchronized (this) { streamConnection = (StreamConnection) Connector.open(url); } // update the Gauge message after connecting gui.showProgress("Uploading Image ...", imageData.length); // start put operation outputStream = streamConnection.openOutputStream(); streamConnection.close(); } /** Uploads image to receiver */ private void uploadImage(String pictureName) throws IOException { int position = 0; OutputStream outputStream = this.outputStream; byte[] imageData = this.imageData; if (outputStream == null) { throw new IOException(); } while (position != imageData.length) { int sendLength = imageData.length - position > 256 ? 256 : imageData.length - position; outputStream.write(imageData, position, sendLength); position += sendLength; gui.updateProgress(position); } outputStream.close(); } private void close() { if (outputStream != null) { try { outputStream.close(); } catch (IOException ioe) {} outputStream = null; } imageData = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -