Christian Amado Christian Amado - 1 year ago 226
C# Question

Xamarin: How to print from Android device to dot matrix printer?

I have the following hardware:

  • Dot matrix printer: LX 300-II.

  • Android device with Android version 4.2.2.

  • Connection type: USB, Wi-FI, Bluetooth.

I have an Android Activity that create an invoice, so I want to print some text on printer. But I can't make it work.

I tried using USBManager with no success. Is there any way to achieve this?

Answer Source

I found a solution. It's the only way to make it works. USBManager cannot access to printer because driver is missing. So I tried with a WI-FI option.

You need some extra hardware here:

  • Print Server (In this case I tried TL-WPS510-U).
  • Android Device 4.0.3 or superior
  • Epson LX-300+II

First of all you must configure your Print Server. I did it following this tutorial.

Now I have the Print Server pointing to my device (acting as Server) and waiting for any printer job.

What I did on my Xamarin.Android project?
Created an instance of a Socket class pointing to the Printer Server ;)

No Epson command needed (Ok some of them). The code is the following:

private async Task SendCommand()
    await Task.Run (() => 
            Socket sock = new Socket("", 9100);
            PrintWriter oStream = new PrintWriter (sock.OutputStream, true);;
            OutputStreamWriter outWriter;

            oStream.Write(0x1B); //T1
            oStream.Write(0x40); //T2 Start Printer
            oStream.Print("This is great!!! á é í ó ú ü Ü Ñ ñ");

            oStream.Write(0x0C); //release paper
            oStream.Write(0x1B); //t1
            oStream.Write(0x40); //t2 Finish Printer

           oStream.Flush ();
           oStream.Close ();
           sock.Close ();

        catch(SocketException ex)
            string m = ex.Message;
            RunOnUiThread(() => Toast.MakeText(this, m, ToastLength.Long).Show());
        catch(Exception ex)
            Toast.MakeText(this, ex.ToString(), ToastLength.Long).Show();


Now is working very fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download