Java 在Android中通过蓝牙打印机多次打印

Java 在Android中通过蓝牙打印机多次打印,java,android,eclipse,printing,Java,Android,Eclipse,Printing,我试图打印多次的基础上输入的整数。我现在可以一次打印一次,但在尝试打印多次后,结果总是只打印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; fin

我试图打印多次的基础上输入的整数。我现在可以一次打印一次,但在尝试打印多次后,结果总是只打印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
}
上面的所有代码都在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);
            }
        }
public void printBillToDevice(最终字符串地址){
mBTAdapter.cancelDiscovery();
试试{BluetoothDevice mdevice=mBTAdapter.getRemoteDevice(地址);
方法m=mdevice.getClass().getMethod(“createRfcommSocket”,
新类[]{int.Class});
mBTSocket=(BluetoothSocket)m.invoke(mdevice,1);
mBTSocket.connect();
//这将完成代码
int copyPrintIs=Integer.parseInt(copyPrint);
对于(int x=1;x
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);
            }
        }