How to connect a network printer over Android?

2019-01-16 21:11发布

I want to code an Android app, which will connect to a network printer with a specific IP address, and then make a printing.

For printing I know that I need to write my own Postscript for specific files types, and connecting to a network is not a problem over WIFI.

How to connect to the network printer?

6条回答
Root(大扎)
2楼-- · 2019-01-16 21:23

Just Add This Code After oncreate Method

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
查看更多
我只想做你的唯一
3楼-- · 2019-01-16 21:26

Well, you cant connect any devices directly as you will need the driver installed. there are 3rd party apps like Google Cloud print that works seamlessly with Android though.

查看更多
仙女界的扛把子
4楼-- · 2019-01-16 21:27

Try to use PrintManager: https://developer.android.com/training/printing/custom-docs

  private void doPrint() {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getActivity()
            .getSystemService(Context.PRINT_SERVICE);

    // Set job name, which will be displayed in the print queue
    String jobName = getActivity().getString(R.string.app_name) + " Document";

    // Start a print job, passing in a PrintDocumentAdapter implementation
    // to handle the generation of a print document
    printManager.print(jobName, new MyPrintDocumentAdapter(getActivity()),
            null); //
}
查看更多
疯言疯语
5楼-- · 2019-01-16 21:34

Star has an Android SDK which has port discovery. It'll find any of their wifi receipt printers on your network. http://starmicronics.com/support/SDKDocumentation.aspx

查看更多
Anthone
6楼-- · 2019-01-16 21:42

Any device connected to a network will communicate via their IP and Ports / sockets. The simplest way to connect via telnet or socket and write the data to their socket buffers.

try 
    {
    Socket sock = new Socket("192.168.1.222", 9100);
    PrintWriter oStream = new PrintWriter(sock.getOutputStream());
        oStream.println("HI,test from Android Device");
        oStream.println("\n\n\n");
        oStream.close();
        sock.close(); 
    }
    catch (UnknownHostException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    { 
        e.printStackTrace();
    } 
查看更多
劫难
7楼-- · 2019-01-16 21:49

You might be able to use lpdspooler, that is, if the printer supports LPR/LPD. If you can give some more details about the environment (printer, etc), I might be able to give more information.

查看更多
登录 后发表回答