2012-12-31 79 views

回答

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

+0

嘿,你打印的文字,进来的格式是否正确,或者他们是否编码? – Kasanova

+1

进来适当的格式只是试试你好世界等不多 –

+0

好吧,很酷。如果我遇到任何问题,我会做并发布。谢谢。 – Kasanova

1

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

+0

谢谢。但是有没有任何代码示例或我可以参考的任何链接。因为我想知道应该在android java编程中使用什么库(例如:Socket等)来连接和物理打印。谢谢 – Kasanova