通过Android中的Bluetooth打印机多次打印

柏林茨

我试图根据输入的整数多次打印。我现在可以一次打印一次,但是尝试多次打印后,结果总是只打印1次,有时却不打印。

MainActivity.java

//This is my global variable for printing
BluetoothAdapter mBTAdapter;
BluetoothSocket mBTSocket = null;
Dialog dialogProgress;
String BILL, TRANS_ID;
String PRINTER_MAC_ID;
final String ERROR_MESSAGE = "There has been an error in printing the bill.";


 //The copyPrint is a String Var which will contain the inputted integer and it will convert into Int so it will become copyPrintIs.
int copyPrintIs = Integer.parseInt(copyPrint);


for(int x = 1; x <= copyPrintIs; x++){
    printNow(thePrinted);
    //Call the printNow and repeat calling on it base on inputted integer
    //The thePrinted will contain String Text which will become the result of printing
}

printNow是调用函数时的代码

public void printNow(String thePrintedvalue) {
            try {
                PRINTER_MAC_ID = "00:12:F3:19:4D:D8";
                BILL = thePrintedvalue; //thePrintedvalue will be pass on BILL Var
                mBTAdapter = BluetoothAdapter.getDefaultAdapter();
                dialogProgress = new Dialog(Ticketing.this);
                try {
                    if (mBTAdapter.isDiscovering())
                        mBTAdapter.cancelDiscovery();
                    else
                        mBTAdapter.startDiscovery();
                } catch (Exception e) {
                    Toast.makeText(this, "A: " + e, Toast.LENGTH_LONG).show();
                }
                System.out.println("BT Searching status :"
                        + mBTAdapter.isDiscovering());
                if (mBTAdapter == null) {
                    Toast.makeText(this, "Device has no bluetooth capability",
                            Toast.LENGTH_LONG).show();
                } else {
                    if (!mBTAdapter.isEnabled()) {
                        Intent i = new Intent(
                                BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(i, 0);
                    }
                    // Register the BroadcastReceiver
                    IntentFilter filter = new IntentFilter(
                            BluetoothDevice.ACTION_FOUND);
                    registerReceiver(mReceiver, filter);
                }

            } catch (Exception e) {
                Toast.makeText(this, "B: " + e, Toast.LENGTH_LONG).show();
            }
        }   


    /********/
    public void printBillToDevice(final String address) {
        mBTAdapter.cancelDiscovery();
        try {           BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(address);
            Method m = mdevice.getClass().getMethod("createRfcommSocket",
                    new Class[] { int.class });
            mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
            mBTSocket.connect();
            OutputStream os = mBTSocket.getOutputStream();
            os.flush();
            os.write(BILL.getBytes());
            System.out.println(BILL);
            if (mBTAdapter != null)
                mBTAdapter.cancelDiscovery();
            mBTSocket.close();
            setResult(RESULT_OK);
        } catch (Exception e) {
            Log.e("Class ", "My Exe ", e);
            // Toast.makeText(BluetoothPrint.this, ERROR_MESSAGE,
            // Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            setResult(RESULT_CANCELED);
        }
    }

    /********/
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {

            try {
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent
                            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    System.out.println("***" + device.getName() + " : "
                            + device.getAddress());

                    if (device.getAddress().equalsIgnoreCase(PRINTER_MAC_ID)) {
                        mBTAdapter.cancelDiscovery();
                        dialogProgress.dismiss();
                        Toast.makeText(Ticketing.this,device.getName() + " Printing data", Toast.LENGTH_LONG).show();

                            printBillToDevice(PRINTER_MAC_ID);

                    }
                }
            } catch (Exception e) {
                Log.e("Class  ", "My Exe ", e);
            }
        }
    };

上面的所有代码都位于MainActivity.java中。我无法弄清楚需要在哪里放置循环纸,这样它才能继续打印纸质副本。

请帮忙!

极性

试试这个:

在此处删除循环。

for(int x = 1; x <= copyPrintIs; x++){
    printNow(thePrinted);
    //Call the printNow and repeat calling on it base on inputted integer
    //The thePrinted will contain String Text which will become the result of printing
}

然后更改此代码

public void printBillToDevice(final String address) {
        mBTAdapter.cancelDiscovery();
        try {           BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(address);
            Method m = mdevice.getClass().getMethod("createRfcommSocket",
                    new Class[] { int.class });
            mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
            mBTSocket.connect();
            OutputStream os = mBTSocket.getOutputStream();
            os.flush();
            os.write(BILL.getBytes());
            System.out.println(BILL);
            if (mBTAdapter != null)
                mBTAdapter.cancelDiscovery();
            mBTSocket.close();
            setResult(RESULT_OK);
        } catch (Exception e) {
            Log.e("Class ", "My Exe ", e);
            // Toast.makeText(BluetoothPrint.this, ERROR_MESSAGE,
            // Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            setResult(RESULT_CANCELED);
        }
    }

对此代码

public void printBillToDevice(final String address) {
            mBTAdapter.cancelDiscovery();
            try {           BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(address);
                Method m = mdevice.getClass().getMethod("createRfcommSocket",
                        new Class[] { int.class });
                mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
                mBTSocket.connect();

                //this will do the code
                int copyPrintIs = Integer.parseInt(copyPrint);
                for (int x = 1; x <= copyPrintIs; x++) {
                    OutputStream os = mBTSocket.getOutputStream();
                    os.flush();
                    os.write(BILL.getBytes());
                    System.out.println(BILL);
                    SystemClock.sleep(4000);//This will pause every 4 seconds after printing once and the continue and pause again  
                }
                copyPrint = "1";//This will change the copyPrint back to 1 value

                if (mBTAdapter != null)
                    mBTAdapter.cancelDiscovery();
                mBTSocket.close();
                setResult(RESULT_OK);
            } catch (Exception e) {
                Log.e("Class ", "My Exe ", e);
                // Toast.makeText(BluetoothPrint.this, ERROR_MESSAGE,
                // Toast.LENGTH_SHORT).show();
                e.printStackTrace();
                setResult(RESULT_CANCELED);
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过Android中的Bluetooth打印机多次打印

来自分类Dev

设置/设备/打印机中的打印机加倍

来自分类Dev

在Awesomium中打印到打印机?

来自分类Dev

如何通过Android中的蓝牙连接移动设备和打印机?

来自分类Dev

如何从Android手机向Bluetooth打印机打印带有文本的图像?

来自分类Dev

如何通过终端安装打印机?

来自分类Dev

如何通过终端安装打印机?

来自分类Dev

Android POS打印机ESC / POS

来自分类Dev

软件打印机

来自分类Dev

使用android框架通过USB使用POS打印机打印

来自分类Dev

打印机不打印

来自分类Dev

如何使用JavaPOS通过Epson打印机打印收据?

来自分类Dev

通过USB连接打印打印机的IP地址

来自分类Dev

无法通过wifi打印到Brother打印机

来自分类Dev

是否可以通过VPN打印到LAN打印机?

来自分类Dev

Zebra打印机中的编码日期

来自分类Dev

Java中的打印机声明

来自分类Dev

Java中的热敏打印机命令

来自分类Dev

设置菜单中的双打印机

来自分类Dev

无法在KDE中配置打印机

来自分类Dev

从 Zebra 打印机中删除字体

来自分类Dev

Ubuntu 14.10无法通过SAMBA找到打印机选项Windows打印机

来自分类Dev

Xerox打印机或通过路由器NAT的任何网络打印机

来自分类Dev

Epson ePOS打印机无法打印-Android SDK

来自分类Dev

Android App从蓝牙打印机进行打印

来自分类Dev

使用Android热敏打印机打印条形码

来自分类Dev

Epson ePOS打印机无法打印-Android SDK

来自分类Dev

如何在TSC打印机中打印图像

来自分类Dev

在delphi中打印到非默认打印机