Pdf到蓝牙打印机问题

时间:2015-03-14 15:26:37

标签: android pdf printing bluetooth

我正在尝试将PDF文件发送到蓝牙打印机。我已成功建立连接,但打印机没有以正确的格式/格式打印文件。

以下是我正在使用的代码:

public ConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;


            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {

                Log.d("UUID Before Socket is created", MY_UUID.toString());

                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 

            } catch (Exception e) {
                e.printStackTrace();
            }
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            myBluetoothAdapter.cancelDiscovery();

            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception



                mmSocket.connect();
                socketConnected = true;

            } catch (Exception connectException) {
                // Unable to connect; close the socket and get out
                connectException.printStackTrace();
                Log.i("Connection Message", "Couldn't connect at first go.");
                try {
                    mmSocket =(BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1);
                    mmSocket.connect();
                    socketConnected = true;
                    Log.i("Connection Message", "Connected using second method.");
                } catch (Exception closeException) {
                    closeException.printStackTrace();
                    try {
                        mmSocket.close();
                        socketConnected = false;
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }

                return;
            }

            try{
                tmpOut = mmSocket.getOutputStream();

                File file = new File(filePath);

                Uri uri = Uri.fromFile(file);

                BufferedInputStream bis = new BufferedInputStream(getContentResolver().openInputStream(uri));
                //OutputStream os = tmpOut;
            try {
                int bufferSize = 1024;
                byte[] buffer = new byte[bufferSize];

                // we need to know how may bytes were read to write them to the byteBuffer
                int len = 0;
                while ((len = bis.read(buffer)) != -1) {
                    tmpOut.write(buffer, 0, len);
                }

            } finally {
                bis.close();
                tmpOut.flush();
                tmpOut.close();
                mmSocket.close();
            }

            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }

        }

这是pdf文件的输出。

Pdf printed by bluetooth printer

P.s pdf是意大利语。

0 个答案:

没有答案