我是java的新手,我正在尝试使用Zebra SDK实现打印测试。 这个Zebra SDK(在类的末尾有一个“getConfigLabel”),打印一个常量“TEST”,所以我想发送一个常量的变量。为了得到这个,我明白我必须向课程发送一个参数,但我不清楚如何完成这项工作。 感谢。
这是班级:
/***********************************************
* CONFIDENTIAL AND PROPRIETARY
*
* The source code and other information contained herein is the confidential and the exclusive property of
* ZIH Corp. and is subject to the terms and conditions in your end user license agreement.
* This source code, and any other information contained herein, shall not be copied, reproduced, published,
* displayed or distributed, in whole or in part, in any medium, by any means, for any purpose except as
* expressly permitted under such license agreement.
*
* Copyright ZIH Corp. 2012
*
* ALL RIGHTS RESERVED
***********************************************/
package com.zebra.android.devdemo.connectivity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import com.zebra.android.devdemo.R;
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 ConnectivityDemo extends Activity {
private Connection printerConnection;
private RadioButton btRadioButton;
private ZebraPrinter printer;
private TextView statusField;
private EditText macAddress, ipDNSAddress, portNumber, vtextToPrint;
private Button testButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connection_screen_with_status);
ipDNSAddress = (EditText) this.findViewById(R.id.ipAddressInput);
ipDNSAddress.setText(SettingsHelper.getIp(this));
vtextToPrint = (EditText) this.findViewById(R.id.textToPrint);
portNumber = (EditText) this.findViewById(R.id.portInput);
portNumber.setText(SettingsHelper.getPort(this));
macAddress = (EditText) this.findViewById(R.id.macInput);
macAddress.setText(SettingsHelper.getBluetoothAddress(this));
statusField = (TextView) this.findViewById(R.id.statusText);
btRadioButton = (RadioButton) this.findViewById(R.id.bluetoothRadio);
testButton = (Button) this.findViewById(R.id.testButton);
testButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
enableTestButton(false);
Looper.prepare();
doConnectionTest();
Looper.loop();
Looper.myLooper().quit();
}
}).start();
}
});
RadioGroup radioGroup = (RadioGroup) this.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.bluetoothRadio) {
toggleEditField(macAddress, true);
toggleEditField(portNumber, false);
toggleEditField(ipDNSAddress, false);
} else {
toggleEditField(portNumber, true);
toggleEditField(ipDNSAddress, true);
toggleEditField(macAddress, false);
}
}
});
}
private void toggleEditField(EditText editText, boolean set) {
/*
* Note: Disabled EditText fields may still get focus by some other means, and allow text input.
* See http://code.google.com/p/android/issues/detail?id=2771
*/
editText.setEnabled(set);
editText.setFocusable(set);
editText.setFocusableInTouchMode(set);
}
@Override
protected void onStop() {
super.onStop();
if (printerConnection != null && printerConnection.isConnected()) {
disconnect();
}
}
private void enableTestButton(final boolean enabled) {
runOnUiThread(new Runnable() {
public void run() {
testButton.setEnabled(enabled);
}
});
}
private boolean isBluetoothSelected() {
return btRadioButton.isChecked();
}
public ZebraPrinter connect() {
setStatus("Connecting...", Color.YELLOW);
printerConnection = null;
if (isBluetoothSelected()) {
printerConnection = new BluetoothConnection(getMacAddressFieldText());
SettingsHelper.saveBluetoothAddress(this, getMacAddressFieldText());
} else {
try {
int port = Integer.parseInt(getTcpPortNumber());
printerConnection = new TcpConnection(getTcpAddress(), port);
SettingsHelper.saveIp(this, getTcpAddress());
SettingsHelper.savePort(this, getTcpPortNumber());
} catch (NumberFormatException e) {
setStatus("Port Number Is Invalid", Color.RED);
return null;
}
}
try {
printerConnection.open();
setStatus("Connected", Color.GREEN);
} catch (ConnectionException e) {
setStatus("Comm Error! Disconnecting", Color.RED);
DemoSleeper.sleep(1000);
disconnect();
}
ZebraPrinter printer = null;
if (printerConnection.isConnected()) {
try {
printer = ZebraPrinterFactory.getInstance(printerConnection);
setStatus("Determining Printer Language", Color.YELLOW);
PrinterLanguage pl = printer.getPrinterControlLanguage();
setStatus("Printer Language " + pl, Color.BLUE);
} catch (ConnectionException e) {
setStatus("Unknown Printer Language", Color.RED);
printer = null;
DemoSleeper.sleep(1000);
disconnect();
} catch (ZebraPrinterLanguageUnknownException e) {
setStatus("Unknown Printer Language", Color.RED);
printer = null;
DemoSleeper.sleep(1000);
disconnect();
}
}
return printer;
}
public void disconnect() {
try {
setStatus("Disconnecting", Color.RED);
if (printerConnection != null) {
printerConnection.close();
}
setStatus("Not Connected", Color.RED);
} catch (ConnectionException e) {
setStatus("COMM Error! Disconnected", Color.RED);
} finally {
enableTestButton(true);
}
}
private void setStatus(final String statusMessage, final int color) {
runOnUiThread(new Runnable() {
public void run() {
statusField.setBackgroundColor(color);
statusField.setText(statusMessage);
}
});
DemoSleeper.sleep(200);
}
private String getMacAddressFieldText() {
return macAddress.getText().toString();
}
private String getTcpAddress() {
return ipDNSAddress.getText().toString();
}
private String getTcpPortNumber() {
return portNumber.getText().toString();
}
private void doConnectionTest() {
printer = connect();
if (printer != null) {
sendTestLabel();
} else {
disconnect();
}
}
private void sendTestLabel() {
try {
byte[] configLabel = getConfigLabel();
printerConnection.write(configLabel);
setStatus("Sending Data", Color.BLUE);
DemoSleeper.sleep(500);
if (printerConnection instanceof BluetoothConnection) {
String friendlyName = ((BluetoothConnection) printerConnection).getFriendlyName();
setStatus(friendlyName, Color.MAGENTA);
DemoSleeper.sleep(500);
}
} catch (ConnectionException e) {
setStatus(e.getMessage(), Color.RED);
} finally {
disconnect();
}
}
/*
* Returns the command for a test label depending on the printer control language
* The test label is a box with the word "TEST" inside of it
*
* _________________________
* | |
* | |
* | TEST |
* | |
* | |
* |_______________________|
*
*
*/
private byte[] getConfigLabel() {
PrinterLanguage printerLanguage = printer.getPrinterControlLanguage();
byte[] configLabel = null;
if (printerLanguage == PrinterLanguage.ZPL) {
configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
} else if (printerLanguage == 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();
}
return configLabel;
}
}
答案 0 :(得分:0)
private byte[] getConfigLabel(String parameter) {
.....
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 " + parameter + "r\n" + "PRINT\r\n";
.......
}
这就是你如何将一个参数引入方法。
调用方法getConfigLabel时,请确保传入参数String,否则会导致错误。
byte[] configLabel = getConfigLabel("Test");
答案 1 :(得分:0)
在这一行,距离结尾为9:
configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
你可以用你的变量替换TEST,例如:
configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FD" + text + "^FS^XZ".getBytes();
您还需要在getConfigLabel
方法中的某处声明此变量,或将其传递给它。
我建议将方法签名更改为getConfigLabel(String text)
并从第9行修改第9行。在这种情况下,在methond sendTestLabel
中你可以替换
byte[] configLabel = getConfigLabel();
与
String text = "some text...";
byte[] configLabel = getConfigLabel(text);
BTW,这只是打印东西而不是“TEST”的方式。我建议你只使用它来了解它是如何工作的。如果要进一步自定义此代码,可能需要添加其他方法,以进行精细打印