![](/img/trans.png)
[英]Print a PDF file to a printer using PrintWriter via Socket connection
[英]Print pdf file through bluetooth printer
作為android藍牙的新功能,它可以在打印機中連接和打印內容。但是現在能夠使用我的android設備連接到藍牙打印機。因此,根據我的項目要求,我必須打印pdf文件的內容。能夠打印字符串變量內容。
現在隨着項目需求的變化,將有一個pdf文件優化任務。首先,我將使用java中的itext庫創建pdf文件來創建pdf文件。因此,文件創建部分已經完成。我為打印該文件所做的工作是生成pdf文件的字節數組。
這是代碼行
FileInputStream fin=new FileInputStream(pdffile);
fileContent=new byte[(int) pdffile.length()];//file content is the byte array for the pdf file.
下一行連接到打印機的藍牙到android設備。
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(PRINTER_MAC_ID);
Method m = mdevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
mBTSocket.connect();
與該套接字(mBTSocket)獲取OutputStream。
然后將字節數組寫入該OutputStream。
os.write(fileContent);
os.flush();
mBTSocket.close();
因此,當我嘗試通過藍牙打印機打印pdf文件內容時,什么也沒有發生,也沒有異常或應用程序崩潰,但是它發出警告getbluetoothservice()且沒有bluetoothmanagercallback調用,並且沒有在紙上打印內容。所以任何人都可以告訴我哪個/我在哪里做錯。我也搜索這個主題,但我只獲得字符串打印而沒有文件的所有東西。 其中一個鏈接講述了一些名為StarIOsdk的sdk,用於Android來打印文件。另外一個問題是android sdk中新的打印方法是在API級別4.4中引入的,但是我們將如何在先前的API級別中進行操作。藍牙熱敏打印機(小尺寸2英寸紙張尺寸)。謝謝。
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(PRINTER_MAC_ID);
Method m = mdevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
mBTSocket.connect();
Thread.sleep(100);
套接字連接后讓線程進入睡眠狀態..這對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.