I make android app which will send PCL commands to BT printer (HP Officejet 100). Problem is when I send string data(PCL command) printer don't recognized these commands and print all these commands like normal strings. Any idea why printer don't recognize commands? My full code here: CODE
I also try change charset to US-ASCII, UTF-8 but PCL command was not recognized.
Second question: is there any way how I can convert PDF file to PCL or how I can do way when I need print PDF files on this printer?
Now I can print strings but I cannot print pdf or images etc and I find way how do this. THX for any help.
Part of code:
void sendCustomData() throws IOException {
try {
String msg =
"<ESC>%-12345X#PJL COMMENT *Start Job* <CR><LF>\n" +
"#PJL JOB NAME = \"Sample Job #1\" <CR><LF>\n" +
"#PJL SET COPIES = 1 <CR><LF>\n" +
"#PJL SET RET = OFF <CR><LF>\n" +
"#PJL ENTER LANGUAGE = PCL <CR><LF>\n" +
"<ESC>E. . . . PCL job . . . .<ESC>E\n" +
"~<ESC>%-12345X#PJL <CR><LF>\n" +
"#PJL EOJ<CR><LF>\n" +
"<ESC>%-12345X";
mOutputStream.write(msg.getBytes("ASCII"));
tvStatus.setText("Custom data sent");
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeBT();
Toast.makeText(this, "BT conn closed", Toast.LENGTH_SHORT).show();
}
}
You shouldn't be using the string literal "<ESC>" because it is expecting the ASCII/UTF-8 escape character (Decimal 27, or Hex 1B). Rather, you should declare a char variable:
public final static char CHAR_ESC = 0x1B;
and use that instead
String msg = CHAR_ESC + "%-12345X#PJL COMMENT Start Job \n" + ...
CR and LF also should be replaced with ASCII Characters.
Related
I try read data from smart card by NFC and Android.
I have smart card with application. Application has AID(Application ID) A0000006581010
I need make SELECT command and read result.
I write method:
private static final String SAMPLE_LOYALTY_CARD_AID = "F222222222";
private void performTransaction(Intent nfcIntent) {
Tag tagFromIntent = nfcIntent.getParcelableExtra(EXTRA_TAG);
NfcA mNfc = NfcA.get(tagFromIntent);
try {
mNfc.connect();
//I can read ID
byte[] id = mNfc.getTag().getId();
//I tried create SELECT command
byte[] selCommand = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);
//I try send command to card
byte[] result = mNfc.transceive(selCommand);
//I get result == {106, -126}
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
if (mNfc != null) {
try {
mNfc.close();
} catch (IOException e) {
Log.v("tag", "error closing the tag");
}
}
}
}
How can I make SELECT command from concret AID on Smart Card?
SELECT is a standard APDU command, you can find a specification that defines it from Global Platform.
Building it from an AID is quite mechanical:
00A40400 + AID lenght in hexadecimal + AID + 00
For example, for your AID of A0000006581010 the SELECT command is:
00A4040007A000000658101000
I am following the Zebra Android Link_OS SDK sample code for printing a test label on a ZQ510 over Bluetooth, but it won't print in ZPL format.
Here is the code I'm running to print the label:
private void sendZplOverBluetooth(final String theBtMacAddress) {
new Thread(new Runnable() {
public void run() {
try {
// Instantiate connection for given Bluetooth® MAC Address.
Connection thePrinterConn = new BluetoothConnection(theBtMacAddress);
// Initialize
Looper.prepare();
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This example prints "This is a ZPL test." near the top of the label.
String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
// Send the data to printer as a byte array.
thePrinterConn.write(zplData.getBytes());
// Make sure the data got to the printer before closing the connection
Thread.sleep(500);
// Close the connection to release resources.
thePrinterConn.close();
Looper.myLooper().quit();
} catch (Exception e) {
// Handle communications error here.
e.printStackTrace();
}
}
}).start();
}
And here is the result of the print. (I ran it twice, that's why there are two test prints).
Then I read about how it might be in a different mode because for some reason Zebra can't detect their own proprietary language. So I tried to get the settings and see through the Android app. Again using the given Link-OS SDK example code:
private static void displaySettings(Connection c) throws ConnectionException, ZebraPrinterLanguageUnknownException, SettingsException, ZebraIllegalArgumentException {
ZebraPrinter genericPrinter = ZebraPrinterFactory.getInstance(c);
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.createLinkOsPrinter(genericPrinter);
if (linkOsPrinter != null) {
System.out.println("Available Settings for myDevice");
Set<String> availableSettings = linkOsPrinter.getAvailableSettings();
for (String setting : availableSettings) {
System.out.println(setting + ": Range = (" + linkOsPrinter.getSettingRange(setting) + ")");
}
System.out.println("\nCurrent Setting Values for myDevice");
Map<String, String> allSettingValues = linkOsPrinter.getAllSettingValues();
for (String settingName : allSettingValues.keySet()) {
System.out.println(settingName + ":" + allSettingValues.get(settingName));
}
String darknessSettingId = "print.tone";
String newDarknessValue = "10.0";
if (availableSettings.contains(darknessSettingId) &&
linkOsPrinter.isSettingValid(darknessSettingId, newDarknessValue) &&
linkOsPrinter.isSettingReadOnly(darknessSettingId) == false) {
linkOsPrinter.setSetting(darknessSettingId, newDarknessValue);
}
System.out.println("\nNew " + darknessSettingId + " Value = " + linkOsPrinter.getSettingValue(darknessSettingId));
}
}
This time, I get a SettingsException with the description of Operation cannot be performed on raw channel with a printer set to line print mode
How am I able to print ZPL text using a Mac and developing Android correctly? I read about using some Zebra Utility app for changing the mode, but it's only available for Windows, and their Android app doesn't work.
Regardless, if someone was to use the app with a printer in the incorrect mode, they would have to go through all this unnecessary setup that wouldn't be intuitive for just anybody.
Thanks for the help and appreciate any feedback.
You can programmatically set the print mode to ZPL, it's currently in line-mode.
To do so:
BluetoothConnection printerIns= new BluetoothConnection(theBtMacAddress);
ZebraPrinter zPrinterIns = ZebraPrinterFactory.getInstance(printerIns);
//Set printer to ZPL mode
zPrinterIns.sendCommand("! U1 setvar \"device.languages\" \"zpl\"\r\n");
//Feed and calibrate to the media
zPrinterIns.sendCommand("~jc^xa^jus^xz");
In your example code, You are establishing a Bluetooth connection and attempting to send raw data, make use of the ZebraPrinterand BluetoothConnection classes provided by Zebra instead from the com.zebra.sdk.printer namespace.
I corrected your code, it should work now.
new Thread(new Runnable() {
public void run() {
try {
// Instantiate connection for given Bluetooth® MAC Address.
BluetoothConnection thePrinterConn = new BluetoothConnection(theBtMacAddress);
// Initialize
Looper.prepare();
// Open the connection - physical connection is established here.
ZebraPrinter zPrinterIns = ZebraPrinterFactory.getInstance(thePrinterConn);
zPrinterIns.sendCommand("! U1 setvar \"device.languages\" \"zpl\"\r\n");
zPrinterIns.sendCommand("~jc^xa^jus^xz");
Thread.sleep(500);
// Send the data to printer as a byte array.
zPrinterIns.sendCommand("^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ");
// Make sure the data got to the printer before closing the connection
Thread.sleep(500);
// Close the connection to release resources.
thePrinterConn.close();
Looper.myLooper().quit();
} catch (Exception e) {
// Handle communications error here.
e.printStackTrace();
}
}
}).start();
If you don't want to perform this step programmatically like in the Dayan answer and you have acces to a Windows machine (or emulating one), install the Zebra Setup Utilities. Then following the instructions here https://km.zebra.com/kb/index?page=content&id=SO7296 to switch the print mode to ZPL with the command
! U1 setvar "device.languages" "zpl"
I would like to create an application which can connect to a Bluetooth Headset via the Hands Free Protocol (HFP). I followed the Android example and have now a BluetoothSocket with its Input and OutputStream. Below you see my read and write methods (read method is executed by another Thread)
public void read() {
while (true) {
Log.d("ME", "Waiting for data");
try { // read until Exception is thrown
numBytes = inStream.read(dataBuffer);
String str = new String(dataBuffer,0,numBytes);
msgHandler.obtainMessage(numBytes, str).sendToTarget();
} catch (Exception e) {
Log.d("ME", "Input stream was disconnected", e);
break; // BluetoothDevice was disconnected => Exit
}
}
}
public void write(byte[] bytes) {
try {
outStream.write(bytes);
outStream.flush();
Log.e("ME", "Wrote: " + new String(bytes));
} catch (IOException e) {
Log.e("ME", "Error occurred when sending data", e);
}
}
When the connection is opened the Bluetooth headset sends AT+BRSF=191 over the InputStream. I tried to response with +BRSF:20\r but here is my problem. After that the device does not send any other data over the InputStream. It does not come to an Exception - it's more like the device does not know how to responde to my message. Do I send the wrong data? I have all the information from here: (HF = Hands-Free Unit AG = Audio Gateway)
Do you have any ideas what I did wrong? Have I missed something?
EDIT: These are my write calls:
write("+BRSF: 191\r");
write("OK\r");
You were missing the OK response. According to this document, the OK-code consists of a windows-style newline (CR LF), the literal OK and then another newline.
Do note that other commands are terminated by a carriage return only. For more information on the hands-free protocol, you can refer to that very document you linked in your post.
Example code:
public static final String OK = statusCode("OK")
public static final String ERROR = statusCode("ERROR")
public static String statusCode(String code) {
return "\r\n" + code + "\r\n";
}
public static String command(String cmd) {
return cmd + "\r";
}
Now you can use OK and ERROR in your code as constants, and you can use the statusCode method for other status codes.
I am trying to communicate with a USB Accessory (Magnetic Strip Card reader, Model- E-Seek M250), with a Nexus 7 acting as a USBHost.
Use case : When a card is swiped , I need to get the details from the card and convert it to a user readable format.
I have been able to successfully get the device, its interface and the input endpoint.
After that this is what I am doing to get the data:
int receivedBytes = mConnection.bulkTransfer(usbEndpointIN, readBytes, readBytes.length, 3000);
if (receivedBytes > 2) {
dataString = new String(readBytes);
Log.v(Util.TAG, " :: Received Byte Count ::" + receivedBytes);
Log.v(Util.TAG, " :: Final Value Bytes" + readBytes);
Log.v(Util.TAG, " :: Final Value String" + dataString);
}
After several tries, I could not find a way to get the data in user readable format, below is the way the data is shown in the logs.
Can anybody let me know how to convert this data into user readable format?
That reader is not encrypted, so it is probably an encoding issue. Check the documentation for the reader to see what type of encoding they use for the card data and use that encoding when pass the byte array to it. Below is an example if UTF-8 is used.
int receivedBytes = mConnection.bulkTransfer(usbEndpointIN, readBytes, readBytes.length, 3000);
if (receivedBytes > 2) {
String dataString = null;
Log.v(Util.TAG, " :: Received Byte Count ::" + receivedBytes);
Log.v(Util.TAG, " :: Final Value Bytes" + readBytes);
try {
dataString = new String( readBytes, "UTF-8");
Log.v(Util.TAG, " :: Final Value String" + dataString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
I'm trying to set up a log handler to output the Android log to file to external storage. The code below creates the log file, but no output is sent to the file, so something is obviously wrong with how the handler is configured. Or, perhaps this arrangement cannot be expected to work at all?
The function is called in onCreate() from the main activity.
private void logToFile(String path) {
try {
// Get package name
String packageName = MainActivity.class.getPackage().getName();
String logfileName = path + "/" + packageName + ".log";
Logger logger = Logger.getLogger(packageName);
logger.setLevel(Level.FINE);
FileHandler fileTxt = new FileHandler(logfileName);
SimpleFormatter formatterTxt = new SimpleFormatter();
fileTxt.setFormatter(formatterTxt);
logger.addHandler(fileTxt);
Toast.makeText(this, "Logging to " + logfileName, Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
Log.i(TAG, "logging to filesystem enabled");
}
To write to the logger declared above (and, thus, the attached handler which writes to a file), the following should be used instead of Log.i(TAG, "message")
private static final Logger logger = Logger.getLogger(TAG);
public void someFunction() {
logger.info("message")
}
These log messages will also appear in logCat/debugger, with the supplied TAG.
P.S. Java logging makes my head hurt...
I was frustrated at having to use Logger instead of standard Logcat Log.d(), Log.e(), etc. so I started using this Frankenstein's monster solution of reading from Logcat into a LogRecord and saving that using FileHandler.
This means you can limit the log file size easily, and retain your detailed Android logs.
But this isn't going to give you continuous output to file. If you don't mind pressing a button or calling it once a session though, then it shouldn't really matter since Logcat is constantly updated anyway.
(I strongly recommend calling from a non-UI thread.)
FileHandler fh=null;
String name;
if ( 0 == Environment.getExternalStorageState().compareTo(Environment.MEDIA_MOUNTED))
name = Environment.getExternalStorageDirectory().getAbsolutePath();
else
name = Environment.getDataDirectory().getAbsolutePath();
name += "/yourapp/yourapp";
try {
fh = new FileHandler(name, 1024*1024, 7, true); //Limit to 7 x 1MB files.
fh.setFormatter(new SimpleFormatter());
//Try to read Logcat.
try {
//Dumps the entire logcat to std output.
Process processD = Runtime.getRuntime().exec("logcat -v long -d");
BufferedReader bufferedReaderD = new BufferedReader(new InputStreamReader(processD.getInputStream()));
String lineD;
while ((lineD = bufferedReaderD.readLine()) != null){
//Send to the file handler.
fh.publish(new LogRecord(Level.ALL, lineD));
}
//Clear the logcat storage. Don't feel like rewriting old records.
Process processC = Runtime.getRuntime().exec("logcat -c");
} catch (IOException e) {
Log.e(TAG, "Could not get Logcat logs.");
e.printStackTrace();
}
} catch (Exception e) {
Log.e("MyLog", "FileHandler exception", e);
} finally {
if (fh != null)
fh.close();
}