daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.andreis.daniele.myApp;
  2.  
  3. import android.util.Log;
  4.  
  5. /**
  6.  * Created by daniele
  7.  */
  8.  
  9. public class UsbDecoder {
  10.     private String rawData = null;
  11.     private String field3 = null;
  12.     private String field2 = null;
  13.     private String field1 = null;
  14.     private boolean isComplete = false;
  15.     private boolean isStart = false;
  16.     private int position = 0;
  17.     private char marker = '!';
  18.  
  19.     public void UsbDecoder(String rawData) {
  20.         this.rawData = rawData;
  21.  
  22.     }
  23.  
  24.     public String getField1() {
  25.         return field1;
  26.     }
  27.  
  28.     public String getField2() {
  29.         return field2;
  30.     }
  31.  
  32.     public String getField3() {
  33.         return field3;
  34.     }
  35.  
  36.     public boolean addChar(Character c) {
  37.         if (isComplete) {
  38.             reset();
  39.         }
  40.  
  41.         int cUnicode = (int) c;
  42.         if (!Character.isWhitespace(cUnicode)) {
  43.             if (c.equals(marker) && (rawData == null || rawData.isEmpty())) {
  44.                 rawData = String.valueOf(c);
  45.                 isStart = true;
  46.             } else if (c.equals(marker) && isStart) {
  47.                 rawData = rawData + c;
  48.                 isComplete = true;
  49.                 decode();
  50.             } else if (isStart && Character.isLetterOrDigit(cUnicode)) {
  51.                 rawData = rawData + c;
  52.             }
  53.         }
  54.         if(Character.isWhitespace(c) && rawData!=null){
  55.             position = rawData.length();
  56.         }
  57.  
  58.         return isComplete;
  59.     }
  60.  
  61.  
  62.     private void reset() {
  63.         rawData = null;
  64.         field1 = null;
  65.         field2 = null;
  66.         field3 = null;
  67.         isComplete = false;
  68.         isStart = false;
  69.     }
  70.  
  71.  
  72.     private void decode() {
  73.         if (rawData.length() > 19) {
  74.             this.field1 = rawData.substring(1, 17);
  75.             if (position > 17) {
  76.                 this.field2 = rawData.substring(17, position);
  77.                 this.field3 = rawData.substring(position, rawData.length() - 1);
  78.             }
  79.         }
  80.     }
  81.  
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top