公共类 WifiPrint 扩展 Activity { TextView 打印机名称,
网关,打印机端口;国际端口 = 9100; WifiManager wifi = null;
android.net.DhcpInfo d;字符串 gateway_ip = "";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_print); wifi = (WifiManager)
获取系统服务(上下文。WIFI_SERVICE);打印机名称 = (TextView)
findViewById(R.id.textView1); gate_way = (TextView)
findViewById(R.id.textView2);打印机端口 = (文本视图)
findViewById(R.id.textView3);
/* de.lohndirekt.print.IppPrintService svc = new IppPrintService(printerURI); InputStream 流 = 新
BufferedInputStream(new FileInputStream("image.epl")); DocFlavor
风味 = DocFlavor.INPUT_STREAM.AUTOSENSE;文档 myDoc = 新
SimpleDoc(流,风味,空); DocPrintJob 作业 =
svc.createPrintJob(); job.print(myDoc, null);*/
}
@Override public boolean onCreateOptionsMenu(Menu menu) { //
膨胀菜单;如果存在,这会将项目添加到操作栏。
getMenuInflater().inflate(R.menu.wifi_print, menu);返回真;
}
public void wifisettings(View v) { startActivityForResult(new
意图(
android.provider.Settings.ACTION_WIFI_SETTINGS), 0); }
public void connectSocket(View v) {
/*Uri 文件路径=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pdfsample); Toast.makeText(this, filepath.getPath(), Toast.LENGTH_LONG).show(); */ ClientThread clientThread = new
客户端线程(“”);线程 clientstartThread = new
线程(客户端线程); clientstartThread.start(); }
public String intToIp(int i) { return ((i & 0xFF) + "." + ((i >> 8)
& 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + ((i >> 24) & 0xFF)); }
@Override protected void onActivityResult(int requestCode, int
resultCode, Intent data) { // TODO 自动生成的方法存根
super.onActivityResult(requestCode, resultCode, data); if
(requestCode == 0) { WifiInfo wifiinfo = wifi.getConnectionInfo();
d = wifi.getDhcpInfo();
printer_name.setText("Name:" + wifiinfo.getSSID()); gateway_ip =
intToIp(d.gateway); gate_way.setText("打印机IP:" + gateway_ip);
打印机端口.setText("端口:" + 端口);
} }
public File createFileFromInputStream(InputStream inputStream) {
try{
File f = new File("/sdcard/testpdf.pdf");
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
return f;
}catch (IOException e) {
//Logging exception
}
return null; }
class ClientThread 实现 Runnable { String filepath = "";
BufferedInputStream bis = null; FileInputStream fis;
Socket socket;
public ClientThread(String filename) { filepath = filename; }
@Override public void run() { // TODO Auto-generated method
存根尝试{
InputStream in = getResources().openRawResource(R.raw.pdfsample);
createFileFromInputStream(in);
文件 pdfFile = new File("/sdcard/testpdf.pdf");
byte[] mybytearray = new byte[(int) pdfFile.length()];
socket = new Socket(gateway_ip, 端口);
runOnUiThread(new Runnable() {
@覆盖
公共无效运行(){
// TODO 自动生成的方法存根
如果(socket.isConnected()){
Toast.makeText(WifiPrint.this,
"套接字连接",
Toast.LENGTH_LONG).show();
}
}
});
fis = new FileInputStream(pdfFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
输出流 os = socket.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
if (bis != null) {
bis.close();
os.close();
socket.close();
fis.close();
}
} catch (final UnknownHostException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(WifiPrint.this,
"UnHost Exceptions" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}); } catch (final IOException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(WifiPrint.this,
"Io Exceptions" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}); } finally {
try {
if (bis != null) {
bis.close();
socket.close();
fis.close();
}
} catch (final IOException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(
WifiPrint.this,
"Io Exeption in Closing Socket"
+ e.getMessage(), Toast.LENGTH_LONG)
.show();
}
});
} } }
}
}