2012-12-31 107 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