String setupAudioParameters() throws MTSCRAException {
    mStringLocalConfig = "";

    String strResult = "OK";

    try {
      String strXMLConfig = "";

      // strXMLConfig = getConfigurationLocal

      if (strXMLConfig.length() <= 0) {
        // Get config from web, if possible
        // Otherwise, try to configure manually
        setAudioConfigManual();
      } else {
        mMTSCRA.setConfigurationXML(strXMLConfig);
        mStringLocalConfig = strXMLConfig;
        return strResult;
      }
    } catch (MTSCRAException ex) {
      throw ex;
    }

    return strResult;
  }
  @Override
  public void onDestroy() {
    cordova.getActivity().getApplicationContext().unregisterReceiver(mHeadsetReceiver);
    cordova.getActivity().getApplicationContext().unregisterReceiver(mNoisyAudioStreamReceiver);
    if (mMTSCRA != null) mMTSCRA.closeDevice();

    super.onDestroy();
  }
  private void InitializeData() {
    mMTSCRA.clearBuffers();
    mLongTimerInterval = 0;

    mbAudioConnected = false;
    mIntCurrentVolume = 0;
    mIntCurrentStatus = STATUS_IDLE;
    mIntCurrentDeviceStatus = MagTekSCRA.DEVICE_STATE_DISCONNECTED;

    mStringAudioConfigResult = "";
  }
  private void sendCardData() throws JSONException {
    JSONObject response = new JSONObject();

    response.put("Response.Type", mMTSCRA.getResponseType());
    response.put("Track.Status", mMTSCRA.getTrackDecodeStatus());
    response.put("Card.Status", mMTSCRA.getCardStatus());
    response.put("Encryption.Status", mMTSCRA.getEncryptionStatus());
    response.put("Battery.Level", mMTSCRA.getBatteryLevel());
    //		response.put("Swipe.Count", mMTSCRA.getSwipeCount());
    response.put("Track.Masked", mMTSCRA.getMaskedTracks());
    response.put("MagnePrint.Status", mMTSCRA.getMagnePrintStatus());
    response.put("SessionID", mMTSCRA.getSessionID());
    response.put("Card.SvcCode", mMTSCRA.getCardServiceCode());
    response.put("Card.PANLength", mMTSCRA.getCardPANLength());
    response.put("KSN", mMTSCRA.getKSN());
    response.put("Device.SerialNumber", mMTSCRA.getDeviceSerial());
    response.put("TLV.CARDIIN", mMTSCRA.getTagValue("TLV_CARDIIN", ""));
    response.put("MagTekSN", mMTSCRA.getMagTekDeviceSerial());
    response.put("FirmPartNumber", mMTSCRA.getFirmware());
    response.put("TLV.Version", mMTSCRA.getTLVVersion());
    response.put("DevModelName", mMTSCRA.getDeviceName());
    response.put("MSR.Capability", mMTSCRA.getCapMSR());
    response.put("Tracks.Capability", mMTSCRA.getCapTracks());
    response.put("Encryption.Capability", mMTSCRA.getCapMagStripeEncryption());
    response.put("Card.IIN", mMTSCRA.getCardIIN());
    response.put("Card.Name", mMTSCRA.getCardName());
    response.put("Card.Last4", mMTSCRA.getCardLast4());
    response.put("Card.ExpDate", mMTSCRA.getCardExpDate());
    response.put("Track1.Masked", mMTSCRA.getTrack1Masked());
    response.put("Track2.Masked", mMTSCRA.getTrack2Masked());
    response.put("Track3.Masked", mMTSCRA.getTrack3Masked());
    response.put("Track1", mMTSCRA.getTrack1());
    response.put("Track2", mMTSCRA.getTrack2());
    response.put("Track3", mMTSCRA.getTrack3());
    response.put("MagnePrint", mMTSCRA.getMagnePrint());
    response.put("RawResponse", mMTSCRA.getResponseData());

    mEventListenerCb.success(response);
  }
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
      throws JSONException {
    PluginResult pr =
        new PluginResult(PluginResult.Status.ERROR, "Unhandled execute call: " + action);

    if (action.equals("openDevice")) {
      if (mMTSCRA == null) {
        InitializeDevice();
      }

      if (mbAudioConnected) {
        if (mMTSCRA.getDeviceType() == MagTekSCRA.DEVICE_TYPE_AUDIO) {
          // Thread tSetupAudioParams = new Thread() {
          //	public void run() {
          try {
            mStringAudioConfigResult = setupAudioParameters();
          } catch (Exception ex) {
            mStringAudioConfigResult = "Error:" + ex.getMessage();
          }
          //	}
          // }

          // tSetupAudioParams.start();
        } else {
        }
        mMTSCRA.openDevice();

        pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.isDeviceConnected());
      } else {
        pr = new PluginResult(PluginResult.Status.ERROR, "No reader attached.");
      }
    } else if (action.equals("closeDevice")) {
      mMTSCRA.closeDevice();

      pr = new PluginResult(PluginResult.Status.OK, !mMTSCRA.isDeviceConnected());
    } else if (action.equals("isDeviceConnected")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.isDeviceConnected());
    } else if (action.equals("isDeviceOpened")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.isDeviceConnected());
    } else if (action.equals("clearCardData")) {
      pr = new PluginResult(PluginResult.Status.OK);
    }
    /*		else if(action.equals("setCardData")) {
    			try {
    				;
    			}
    		}
    		else if(action.equals("getTrackDecodeStatus")) {
    			try {
    				;
    			}
    		}
    */
    else if (action.equals("getTrack1")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getTrack1());
    } else if (action.equals("getTrack2")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getTrack2());
    } else if (action.equals("getTrack3")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getTrack3());
    } else if (action.equals("getTrack1Masked")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getTrack1Masked());
    } else if (action.equals("getTrack2Masked")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getTrack2Masked());
    } else if (action.equals("getTrack3Masked")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getTrack3Masked());
    } else if (action.equals("getMagnePrintStatus")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getMagnePrintStatus());
    } else if (action.equals("getMagnePrint")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getMagnePrint());
    } else if (action.equals("getDeviceSerial")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getDeviceSerial());
    } else if (action.equals("getSessionID")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getSessionID());
    }
    /*		else if(action.equals("setDeviceProtocolString")) {
    			try {
    				;
    			}
    		}
    */
    else if (action.equals("listenForEvents")) {
      pr = new PluginResult(PluginResult.Status.NO_RESULT);
      pr.setKeepCallback(true);

      mEventListenerCb = callbackContext;
    } else if (action.equals("getCardName")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getCardName());
    } else if (action.equals("getCardIIN")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getCardIIN());
    } else if (action.equals("getCardLast4")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getCardLast4());
    } else if (action.equals("getCardExpDate")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getCardExpDate());
    } else if (action.equals("getCardServiceCode")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getCardServiceCode());
    } else if (action.equals("getCardStatus")) {
      pr = new PluginResult(PluginResult.Status.OK, mMTSCRA.getCardStatus());
    }
    /*
    		else if(action.equals("setDeviceType")) {
    			try {
    				;
    			}
    		}
    */
    else if (action.equals("setDeviceType")) {;
    }

    callbackContext.sendPluginResult(pr);

    return true;
  }
  void setAudioConfigManual() throws MTSCRAException {
    String model = android.os.Build.MODEL.toUpperCase();

    try {
      if (model.contains("DROID RAZR") || model.toUpperCase().contains("XT910")) {
        mMTSCRA.setConfigurationParams("INPUT_SAMPLE_RATE_IN_HZ=480000,");
      } else if (model.equals("DROID PRO")
          || model.equals("MB508")
          || model.equals("DROIDX")
          || model.equals("DROID2")
          || model.equals("MB525")) {
        mMTSCRA.setConfigurationParams("INPUT_SAMPLE_RATE_IN_HZ=32000,");
      } else if (model.equals("GT-I9300")
          || // S3 GSM Unlocked
          model.equals("SPH-L710")
          || // S3 Sprint
          model.equals("SGH-T999")
          || // S3 T-Mobile
          model.equals("SCH-I535")
          || // S3 Verizon
          model.equals("SCH-R530")
          || // S3 US Cellular
          model.equals("SAMSUNG-SGH-I747")
          || // S3 AT&T
          model.equals("M532")
          || // Fujitsu
          model.equals("GT-N7100")
          || // Notes 2
          model.equals("GT-N7105")
          || // Notes 2
          model.equals("SAMSUNG-SGH-I317")
          || // Notes 2
          model.equals("SCH-I605")
          || // Notes 2
          model.equals("SCH-R950")
          || // Notes 2
          model.equals("SGH-T889")
          || // Notes 2
          model.equals("SPH-L900")
          || // Notes 2
          model.equals("SAMSUNG-SGH-I337")
          || // S4
          model.equals("SAMSUNG-SGH-M919")
          || // S4
          model.equals("SAMSUNG-SCH-I545")
          || // S4
          model.equals("SAMSUNG-SPH-L720")
          || // S4
          model.equals("SAMSUNG-SCH-R970")
          || // S4
          model.equals("SAMSUNG-SCH-R970X")
          || // S4
          model.equals("SAMSUNG-SCH-R970C")
          || // S4
          model.equals("SAMSUNG-SM-G900A")
          || // S5
          model.equals("GT-P3113")) // Galaxy Tab 2, 7.0
      {
        mMTSCRA.setConfigurationParams("INPUT_AUDIO_SOURCE=VRECOG,");
      } else if (model.equals("XT907")) {
        mMTSCRA.setConfigurationParams("INPUT_WAVE_FORM=0,");
      } else {
        // Using Default Settings for device
      }
    } catch (MTSCRAException ex) {
      throw new MTSCRAException(ex.getMessage());
    }
  }