要求Android程序连接到wifi打印机并物理打印内容

时间:2012-12-31 05:53:23

标签: java android printing wifi

我有一个使用asp网络服务的Android程序。现在我希望我的Android程序连接到wifi打印机并打印它从Web服务中消耗的内容。我不知道从哪里开始以及使用什么资源。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:5)

我使用以下代码打印

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,我已经这样做了。

答案 1 :(得分:1)

我首先看一下this文档。
然后您必须了解(我假设您正在与特定供应商合作,对吗?)“打印机端”协议 -
要发送给它的确切数据(以及如何)。
我不确定所有的wifi打印机是否共享相同的API,由你来检查。