Unable to print on WiFi Printer using socket connection in Xamarin Android

PratitiSamriyaPratitiSamriya INMember ✭✭
edited November 2017 in Xamarin.Android

Hi,

I tried referring to this code to get printing done from my HP Wifi printer, but no luck yet.

    private async Task Print()
       {
            await Task.Run (() => 
            {
                try
                {
                    //As you will see, all depends on your printer commands
                     Socket socket = new Socket(YOUR_PRINTSERVER_IP, YOUR_PRINSERVER_TCP_PORT);
                     PrintWriter pw = new PrintWriter (socket .OutputStream, true);

                     //here is the beginning of printing
                     pw.Write(0x1B); //Example of command to start a printer
                     pw.Print("THIS IS YOUR AWESOME TEXT FOR PRINT");

                     pw.Write(0x0C); //here you release the paper
                     pw.Write(0x40); //finish printing
                     pw.Flush ();
                     pw.Close ();
                     pw.Dispose();
                     socket.Close ();
                     socket.Dispose();

            }
            catch(SocketException ex)
            {
            Console.WriteLine("SocketError: "+ex.Message);
            }
        });
    }

Please suggest.

Sign In or Register to comment.