简体   繁体   中英

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.

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.

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.

 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.

and than writing the byte array to that 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. 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.

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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