Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
We have a native android app which you can print invoice an waybill from it via bluetooth. i used default bluetooth adapter. When i hit print button from app, the bluetooth printer prints what i sent with no problem, but when i print again, printing text is corrupted. At the half of the document printer just stop and rewind the paper and print the left of the text that i sent. At that point i switch-off the printer and switch on again. then i press print from app. Again printer works fine in the first print. but when i print second copy, printer fail again. i couldnt understand whats going on. If i have a problem with the code or adapter that i used, i couldnt print any text message, but i have problem only in second copies.

Here is my code:

Java
public BluetoothDevice FindPrinter() {
		BluetoothDevice currentDevice = null;

		try {
			mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			if (mBluetoothAdapter == null) {
				throw new Exception("Bluetooth adaptorü bulunamadı");
			}
			if (!mBluetoothAdapter.isEnabled()) {
				Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
				Activity a = new Activity();
				a.startActivityForResult(enableBluetooth, 0);
			}
			Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
			if (pairedDevices.size() > 0) {
				for (BluetoothDevice device : pairedDevices) {
                                     if(device.getName().equals(SharedPreferenceSettings.getPrinterPort(context))) {
						currentDevice = device;
					}
				}
			}

		} catch (Exception e) {
			Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
		}
		return currentDevice;
	}

public void Print() {

		try {
			int current = 0;
			FormatData();
			int line = 0;
			
			Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
			mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
			mmSocket.connect();
			while (current < Data.length) {
				mmOutputStream = mmSocket.getOutputStream();
				int len = 256;
				if (current + 256 > Data.length) {
					len = Data.length - current;
				}
				byte[] temp = new byte[len];
				System.arraycopy(Data, current, temp, 0, len);
				int currentLine = CountLines(temp);
				line = line + currentLine;
				mmOutputStream.write(temp);
				current += len;
				Thread.sleep(1700);
			}
			mmOutputStream.close();
			if(mmSocket.isConnected())
				mmSocket.close();
		} catch (Exception e) {
			Log.e("Print ERROR", e.getMessage());
		}

	}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900