O. Guys! I have successfully tested the next code from DeveloperDemo_Android that came with my zebra imz220 for testing purposes, but this code, creates a file with some example text to print. I will like to make some modification to this program, to test if, instead of creating a file and send it to printer, i can use an already created file located on my mobile folder @: '/storage/emulated/0/Download/' ?
i think i don't need to call createDemoFile, instead of doing so, i think i need to assign the filepath to my file located at my mobile, for example :
File filepath = "/storage/emulated/0/Download/TEST.LBL"; of course, filepath is a File variable, so, i am stuck her,
thanks for the tips and time,
package com.zebra.android.devdemo.sendfile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import com.zebra.android.devdemo.ConnectionScreen;
import com.zebra.android.devdemo.util.SettingsHelper;
import com.zebra.android.devdemo.util.UIHelper;
import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import com.zebra.sdk.printer.PrinterLanguage;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
public class SendFileDemo extends ConnectionScreen {
private UIHelper helper = new UIHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testButton.setText("Send Test File");
}
@Override
public void performTest() {
new Thread(new Runnable() {
public void run() {
Looper.prepare();
sendFile();
Looper.loop();
Looper.myLooper().quit();
}
}).start();
}
private void sendFile() {
Connection connection = null;
if (isBluetoothSelected() == true) {
connection = new BluetoothConnection(getMacAddressFieldText());
} else {
try {
int port = Integer.parseInt(getTcpPortNumber());
connection = new TcpConnection(getTcpAddress(), port);
} catch (NumberFormatException e) {
helper.showErrorDialogOnGuiThread("Port number is invalid");
return;
}
}
try {
helper.showLoadingDialog("Sending file to printer ...");
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
testSendFile(printer);
connection.close();
} catch (ConnectionException e) {
helper.showErrorDialogOnGuiThread(e.getMessage());
} catch (ZebraPrinterLanguageUnknownException e) {
helper.showErrorDialogOnGuiThread(e.getMessage());
} finally {
helper.dismissLoadingDialog();
}
}
private void testSendFile(ZebraPrinter printer) {
try {
File filepath = getFileStreamPath("TEST.LBL");
createDemoFile(printer, "TEST.LBL");
printer.sendFileContents(filepath.getAbsolutePath());
SettingsHelper.saveBluetoothAddress(this, getMacAddressFieldText());
SettingsHelper.saveIp(this, getTcpAddress());
SettingsHelper.savePort(this, getTcpPortNumber());
} catch (ConnectionException e1) {
helper.showErrorDialogOnGuiThread("Error sending file to printer");
} catch (IOException e) {
helper.showErrorDialogOnGuiThread("Error creating file");
}
}
private void createDemoFile(ZebraPrinter printer, String fileName) throws IOException {
FileOutputStream os = this.openFileOutput(fileName, Context.MODE_PRIVATE);
byte[] configLabel = null;
PrinterLanguage pl = printer.getPrinterControlLanguage();
if (pl == PrinterLanguage.ZPL) {
configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
} else if (pl == PrinterLanguage.CPCL) {
String cpclConfigLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n";
configLabel = cpclConfigLabel.getBytes();
}
os.write(configLabel);
os.flush();
os.close();
}
}
1 comments:
great post!
is the any chance to get the UIHelper code?
best regards.
Post a Comment