【问题标题】:Printing multiple times via Bluetooth Printer in Android在 Android 中通过蓝牙打印机多次打印
【发布时间】:2015-02-04 11:13:47
【问题描述】:

我正在尝试根据输入的整数多次打印。我现在可以一次打印一次,但是在尝试多次打印后,结果总是只打印 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 中。 我不知道我需要把循环放在哪里,这样它才能继续打印纸张。

请帮忙!

【问题讨论】:

    标签: java android eclipse printing


    【解决方案1】:

    试试这个:

    去掉这里的循环。

    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);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 2014-07-22
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2021-09-27
      • 2011-07-05
      相关资源
      最近更新 更多