简体   繁体   English

通过蓝牙打印机打印pdf文件

[英]Print pdf file through bluetooth printer

As new to the android bluetooth connecting and printing the content in printer.But now able to connect to bluetooth printer with my android device.So,with my project requirement i have to print the content of the pdf file.Previously with the same project i am able to print the string variable content. 作为android蓝牙的新功能,它可以在打印机中连接和打印内容。但是现在能够使用我的android设备连接到蓝牙打印机。因此,根据我的项目要求,我必须打印pdf文件的内容。能够打印字符串变量内容。

Now with the project requirement change there will be a pdf file priniting task.First I am creating the pdf with the itext library in java for pdf file creation.So,the file creation part is done.And what i did for printing that file is generate the byte array for the pdf file. 现在随着项目需求的变化,将有一个pdf文件优化任务。首先,我将使用java中的itext库创建pdf文件来创建pdf文件。因此,文件创建部分已经完成。我为打印该文件所做的工作是生成pdf文件的字节数组。

here is the line of code 这是代码行

FileInputStream fin=new FileInputStream(pdffile);

fileContent=new byte[(int) pdffile.length()];//file content is the byte array for the pdf file.

Next line connecting to the bluetooth of the printer to the android device. 下一行连接到打印机的蓝牙到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();

with that socket(mBTSocket) getting the OutputStream. 与该套接字(mBTSocket)获取OutputStream。

and than writing the byte array to that OutputStream. 然后将字节数组写入该OutputStream。

os.write(fileContent);
os.flush();
mBTSocket.close();

so when i tried to print the pdf file content through the bluetooth printer,nothing happen there is no exception or application crash but it give warning getbluetoothservice() called with no bluetoothmanagercallback and not content print on the paper.So anyone can tell me which/where im doing wrong.I also search for this topic but all thing i got about string printing only but no file. 因此,当我尝试通过蓝牙打印机打印pdf文件内容时,什么也没有发生,也没有异常或应用程序崩溃,但是它发出警告getbluetoothservice()且没有bluetoothmanagercallback调用,并且没有在纸上打印内容。所以任何人都可以告诉我哪个/我在哪里做错。我也搜索这个主题,但我只获得字符串打印而没有文件的所有东西。 One of the link tell about some sdk named StarIOsdk for android for printing file.And one more problem in the android sdk that the new printing methodlogy is introduced in API level 4.4 but how we will do in prior API level.The printer used here is bluetooth thermal printer(small size 2 inche paper size).Thanks in advance. 其中一个链接讲述了一些名为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);

After Socket Connect put thread to sleep ..This worked for me 套接字连接后让线程进入睡眠状态..这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM