开发者

How to connect a network printer over Android?

开发者 https://www.devze.com 2023-03-12 09:21 出处:网络
I want to code an Andro开发者_C百科id app, which will connect to a network printer with a specific IP address, and then make a printing.

I want to code an Andro开发者_C百科id 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?


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();
    } 


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.


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);
}


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


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); //
}


My solution. I used Epson TM series. I think the port is 9100 for default. In Manifest add:

<uses-permission android:name="android.permission.INTERNET"/>

in the activity use a Thread otherwise u you can the android.os.NetworkOnMainThreadException error.

Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
               try  {
                  Socket sock = new Socket("192.168.1.168", 9100);
                  PrintWriter oStream = new PrintWriter(sock.getOutputStream());
                  oStream.println("Hi, test from Android Device");
                  oStream.println("\n");
                  oStream.close();
                  sock.close();
                } catch (UnknownHostException e)
                  {
                   e.printStackTrace();
                  }
                  catch (IOException e)
                  {
                    e.printStackTrace();
                  }
                    }});

If not enough, add in manifest these:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消