public class MainActivity extends AppCompatActivity implements SerialListener {
private SerialSDKService serialService;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serialService = SerialSDKService.getInstance(context);
serialService.setListener(this);
serialService.create("/dev/ttyS2", 9600);
serialService.open();
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("api", "/sale");
jsonObject.put("orderAmount", amount);
jsonObject.put("paymentType", "01");
jsonObject.put("merchantSerialNo", UUID.randomUUID());
jsonObject.put("externalAdditionalData", "external data");
String jsonString = jsonObject.toString();
serialService.sendText(jsonString);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
@Override
public void onDataReceived(String jsonString) {
try {
Log.i(TAG, "onDataReceived result: " + new JSONObject(jsonString));
JSONObject jsonObject = new JSONObject(jsonString);
String code = jsonObject.optString("code");
String msg = jsonObject.optString("msg");
if(code.equals("00")) {
JSONObject data = jsonObject.optJSONObject("data");
if (data != null) {
String api = data.optString("api");
String requestId = data.optString("requestId");
String sn = data.optString("sn");
Log.d("JSON", "api = " + api);
if (api.equals("/sale") && requestId.equals("requestId")) {
Log.i(TAG, "onDataReceived: Processing /sale return data");
}
}
}else {
Log.e(TAG, "onDataReceived: " + msg );
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(int errorCode, String errorMessage) {
Log.i(TAG, "onError: " + errorCode + " errorMessage:" + errorMessage);
}
@Override
protected void onDestroy() {
super.onDestroy();
serialService.close();
}
}