需要Android程序连接到一个WiFi打印机和身体打印内容连接到、打印机、身体、程序

2023-09-07 04:21:37 作者:念白

我有一个Android程序,消耗一个asp web服务。现在我想我的Andr​​oid程序连接到一个WiFi打印机并打印,它从Web服务所消耗的内容。我不知道从哪里开始,使用什么样的资源。

I have an Android program that consumes an asp web service. Now I want my Android program to connect to a wifi printer and print the contents that it consumed from the web service. I have no idea where to start from and what resources to use.

任何帮助将是非常美联社preciated。

Any help would be highly appreciated.

推荐答案

我用下面的code打印

i have used following code to print

import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import com.iPrint.iObject;
import com.iPrint.iPaperType;
import com.iPrint.iPrint;
import com.iPrint.iPrinters;
import com.iPrint.iSettings;
import com.iPrint.ipConnection;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iSettings set = new iSettings();
    set.SetPaperType(iPaperType.LETTER);
    set.PrinterType(iPrinters.HP);

    //Creating Connection object

    ipConnection wfc = new ipConnection();

    wfc.ipAddress("IP-Address");
    wfc.Port(port-number);
    try {
        wfc.Connect();
        Log.e("Connected",  wfc.isConnected()+"");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("In Error", "In Error");
        e.printStackTrace();
    }

    //Creating a Printing object
    iPrint p = new iPrint(getApplicationContext()); 
    p.Connection(wfc);
    p.Settings(set);
    if (wfc.isConnected())
    {
        p.Start();
        iObject ob = new iObject();
        ob.Text("Hello iPrint!");
        p.Add(ob);
        p.End(); 
        p.Print(); 
        ob = null;
    }
    else
    {
        Log.v("Error",wfc.ErrorMessage()+"");
       //showDialog(wfc.ErrorMessage(), AppContext);
    }
    print(getApplicationContext());
}
private void print(Context AppContext) 
{ 
//Creating Settings object
iSettings set = new iSettings();
set.SetPaperType(iPaperType.LETTER);
set.PrinterType(iPrinters.HP);

//Creating Connection object

ipConnection wfc = new ipConnection();
wfc.ipAddress("ip-address");
wfc.Port(port-number);
try {
    wfc.Connect();
} catch (IOException e) {
    // TODO Auto-generated catch block
    Log.e("In Error", "In Error");
    e.printStackTrace();
}

//Creating a Printing object
iPrint p = new iPrint(AppContext); 
p.Connection(wfc);
p.Settings(set);
if (wfc.isConnected())
{
    p.Start();
    iObject ob = new iObject();
    ob.Text("Hello iPrint!");
    p.Add(ob);
    p.End(); 
    p.Print(); 
    ob = null;
}
else
{
    Log.v("Error",wfc.ErrorMessage()+"");
}
 set = null;
 wfc = null;
 p = null; 
 }
}

添加 iprint.jar ,我已经做了这种方式。

add iprint.jar , i have done this way .