打印到网络连接的打印机android

时间:2018-10-10 12:43:04

标签: android printing bonjour

我尝试了多种方法来打印到网络连接的打印机。基本上我想打印到局域网连接的打印机。使用默认的NSD管理器,我们无法检测到它。但是我能够通过

获得服务
  compile 'com.github.andriydruk:rxdnssd:0.9.10'

但是主要问题是套接字连接。在大多数情况下,它会引发IOException并在某些情况下运行整个PJL命令,但打印机没有执行它。任何人都可以共享一些代码或示例项目以打印到那些打印机吗?

sendTPrinter() {
        DataOutputStream outToServer = null;
        Socket clientSocket = null;
        String result;
        InputStream fileInputStream=null;
        try {

        clientSocket = new Socket(ipAddress, portNumber);
        InputStream inputStream = clientSocket.getInputStream();
        outToServer = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));

        char ESC = 0x1b;// "0x1b";
        String UEL = ESC + "%-12345X";
        String ESC_SEQ = ESC + "%-12345";
        outToServer.write(convertTOUtf(UEL+"@PJL COMMENT *Start Job*"));
        outToServer.write(convertTOUtf("@PJL \r\n"));
        outToServer.write(convertTOUtf("@PJL JOB NAME = $myjob"));
        outToServer.write(convertTOUtf("@PJL SET RET = "));
        //outToServer.writeChars(("@PJL SET PAPER=" + "A4"));
        outToServer.write((convertTOUtf("@PJL SET COPIES= 1")));
        outToServer.write(convertTOUtf("@PJL ENTER LANGUAGE = PCL"));
        outToServer.write(convertTOUtf(ESC+"E"));
        outToServer.write(convertTOUtf(ESC+"EHello World"));

        outToServer.write(convertTOUtf(ESC_SEQ));
        outToServer.write(convertTOUtf("@PJL \r\n"));
        outToServer.write(convertTOUtf("@PJL RESET \r\n"));
        outToServer.write(convertTOUtf("@PJL EOJ NAME = $myjob"));
        outToServer.write(convertTOUtf(UEL));

        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        int count = bufferedInputStream.available();
        Logr.d("count "+count);
        result = SUCCESSFULLY_SENT;
    } catch (ConnectException connectException){
        Log.e(TAG, connectException.toString());
        result = connectException.toString();
    } catch (UnknownHostException unknownHostException) {
        Log.e(TAG, unknownHostException.toString());
        result = unknownHostException.toString();
    } catch (IOException ioException) {
        Log.e(TAG, ioException.toString());
        result = ioException.toString();
    } finally{
        try {
           /* if (clientSocket!=null)clientSocket.close();

            if (outToServer!=null){
                outToServer.close();
           }*/
            if (fileInputStream!=null){
                fileInputStream.close();
            }
        }catch (IOException ioException){
            result = ioException.toString();
        }
    }
    return result;
}`

0 个答案:

没有答案