0

Is it possible to print on any ethernet printer using IP address and port? The idea is we don't want to use android's print dialog because if we have to print 1000 documents on the same printer we will manually select the printer 1000 times.

Is there any way to print without manually controlling the interface?

I have tried : How to connect a network printer over Android?

But my printer is ethernet printer and not WIFI printer.

Ajit
  • 339
  • 5
  • 15

1 Answers1

0

Please find below class for wifi printing.

public abstract class WifiPrinterConnection extends AsyncTask<Void, Void, Void>  {
    private byte[] printData;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         //place your progress dialog
    }


    @Override
    protected Void doInBackground(final Void... params) {



        try {
            final Socket socket = new Socket();
            socket.connect(new InetSocketAddress(InetAddress.getByName(((WifiPrintingEntity)this.connectionEntity).getIp()), ((WifiPrintingEntity)this.connectionEntity).getPort()),
                    ((WifiPrintingEntity)this.connectionEntity).getTimeout());
            LOGGER.info(PrinterLoggerMessage.WIFI_PRINTER_CONNECTION_SUCCESS);
            final OutputStream outputStream = socket.getOutputStream();

            final byte[] data = this.printData;
            LOGGER.info(PrinterLoggerMessage.PRINTING_STARTED);
            outputStream.write(data, 0, data.length);

            outputStream.close();
            socket.close();
            entity.setSuccessful(true);
            LOGGER.info(PrinterLoggerMessage.SOCKET_CLOSE);


        } catch (final IOException e) {

            LOGGER.error(e.getMessage(),e);
        } catch (final Exception ee) {


        }

        return null;
    }


    @Override
    public void connection(final PrintingEntity connectionEntity, final IConnectionResponse listner,final byte[] data) {
        this.connectionEntity = connectionEntity;
        this.printData = data;
        this.response = listner;
        this.execute();
    }

}

Call this class as a wifiPrinterConnection.connect()

amity
  • 942
  • 1
  • 8
  • 24
  • 1
    This will not work for ethernet printers.I will try this for wifi printers and let you know. Thanks – Ajit Jun 26 '17 at 16:20