2015-02-04 2 views
0

입력 된 정수를 기준으로 여러 번 인쇄하려고합니다. 한 번에 한 번씩 인쇄 할 수 있지만 여러 번 인쇄하려고하면 결과는 항상 1 회만 인쇄되며 때로는 인쇄되지 않습니다.안드로이드에서 Bluetooth 프린터를 통해 여러 번 인쇄하기

여기

//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

printNow 기능은 위의 MainActivity.java에 내부에 모든 코드를
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); 
      } 
     } 
    }; 

를 호출 할 때 내 코드입니다. 루프를 넣어야하는 곳을 알아낼 수 없으므로 용지를 계속 인쇄 할 수 있습니다.

도와주세요!

답변

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

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

을 chnage

관련 문제