I'm emplementing a card reader and I need to use regex in android. Following wikipedia the regex for track1 is:
^%([A-Z])([0-9]{1,19})\^([^\^]{2,26})\^([0-9]{4}|\^)([0-9]{3}|\^)([^\?]+)\?$
Tried here: http://www.regexplanet.com/advanced/java/index.html with the following example:
%B6011898748579348^DOE/ JOHN ^37829821000123456789?
and it worked but not in my aplication.
String re = "%([A-Z])([0-9]{1,19})\\^([^\\^]{2,26})\\^([0-9]{4}|\\^)([0-9]{3}|\\^)([^\\?]+)\\?";
Pattern p = Pattern.compile(re);
String teste = "%B6011898748579348^DOE/ JOHN ^37829821000123456789?";
Matcher m = p.matcher(teste);
Log.d(TAG,"1111: "+m.groupCount());
int i=0;
for(i=0;i<m.groupCount();i++){
try{
Log.d(TAG, "GROUP"+Integer.toString(i)+" - "+m.group(i));
}catch (IllegalStateException e){
Log.d(TAG, e.toString());
}
}
Teste with ^ and $ and multiline but none worked :s
the result is always:
1111: 6
java.lang.IllegalStateException: No successful match so far
java.lang.IllegalStateException: No suc...
...
You need to use m.find() first. Also you should iterate including last group. Try this way
...
if(m.find()){
Log.d(TAG,"1111: " + m.groupCount());
//change '<' into '<=' to include group 6
for(int i=0; i<=m.groupCount(); i++){
try{
Log.d(TAG, "GROUP"+Integer.toString(i)+" - "+m.group(i));
}catch (IllegalStateException e){
Log.d(TAG, e.toString());
}
}
}
Related
I've got the old project in Java and for sure I met there with some small problem.
here is the code:
public boolean printerPrint(List<String> list) {
if (btMan != null) {
try {
// Select character code table (ESC t n) - n = 16(0x10)
btMan.getOutputStream().write(0x1B);
btMan.getOutputStream().write(0x74);
btMan.getOutputStream().write(0x10);
for (String s : list) {
byte[] byteLine = (s).getBytes(ISO_8859_1);
String line = Util.byte2HexStr(byteLine) + " 0A";
btMan.SendData(line);
}
btMan.SendData("0A 0A 0A");
} catch (Exception e) {
Timber.e(e, "printing stop failed");
}
}
return true;
}
Here is the screenshot from the app, this bill should be the same after print.
Here is the bill after print:
Some of the letters are not printed (š, ě, č, ř, ž), but they have been replaced by "?".
I tried to use ISO8859-2(the same problem as in ISO-8859-1, but instead of replacement by "?", they have been replaced by some letters which aren't correct), UTF8/UTF-8(especially with this it doesn't work at all).
If someone ever met with that, any advice is good.
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 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.
This question already has answers here:
How can I get logcat on my device to show logs from all processes
(3 answers)
Closed 9 years ago.
As title, I can get orhter log ex.
Runtime.getRuntime().exec("logcat -d|);
try {
Log.e("test", "1");
Process proc = Runtime.getRuntime().exec(
"logcat -b radio -v time -s GSM");
Log.e("test", "2");
BufferedReader reader = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
Log.e("test", "3");
String line;
ArrayList list = new ArrayList();
if (reader.readLine() != null) {
for (int i = 0; i < 50; i++) {
line = reader.readLine();
list.add(line);
}
}
Log.e("test", "4");
for (int i = 0; i < list.size(); i++) {
Log.e("a123456", list.get(i).toString());
}
} catch (Exception t) {
Log.e("test", "5");
Log.e("test", t.getMessage());
}
test log 1 2 3
can be print . but can't get any Exception log.I think it must be block
why?
ps: I use Sony phone jelly bean eng
I was having the same issue too. Based on my research it's most likely due to Jellybean no longer allowing any 3rd party app to view logcat logs except for its own, as described here and here.
I am making an app in which i am stuck in this exception - String index out of bound and i get error at the following line.
String netwrkk = mTelephonyMgr.getNetworkOperator();
System.out.println("hello");
if (netwrkk != null) {
System.out.println("hello2");
// int mcc = Integer.parseInt(netwrkk.substring(0, 3)); // exception at this line
System.out.println("hello3");
int mnc = Integer.parseInt(netwrkk.substring(3));// exception at this line
System.out.println("hello4");
mccc1=(TextView)findViewById(R.id.mccc1);
System.out.println("netwrk:"+netwrkk);
mccc1.setText(Integer.toString(mcc));
mncc1=(TextView)findViewById(R.id.mncc1);
System.out.println("netwrk:"+netwrkk);
mncc1.setText(Integer.toString(mnc));
System.out.println("mccc: "+mcc);
try{
System.out.println("bearing : "+locn.getBearing());
}catch (Exception e) {
System.out.println(e);
}System.out.println("mnc"+mnc);
}
You should check if netwrkk has the length of 4 or more. If not, the substring(0, 3) will fail.
Also make yourself familiar with the Log class in android. You shouldn't use System.out.println()