gpt4 book ai didi

java - 通过 Android 中的蓝牙打印机多次打印

转载 作者:行者123 更新时间:2023-11-30 11:08:57 30 4
gpt4 key购买 nike

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

主 Activity .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);
}
}

关于java - 通过 Android 中的蓝牙打印机多次打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28319957/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com