Well I've edited my Code and now the whole functionality runs smoothly.
So here is the code:
**BarcodeActivtiy.java:**
package com.example.yourApp;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
@SuppressWarnings("All")
public class BarcodeDump extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final String TAG = "BarcodeDump";
private static final String FLASH_STATE = "FLASH_STATE";
private static final String AUTO_FOCUS_STATE = "AUTO_FOCUS_STATE";
private static final String SELECTED_FORMATS = "SELECTED_FORMATS";
private static final String CAMERA_ID = "CAMERA_ID";
private static String EMPTY = "";
private ZXingScannerView mScannerView;
private boolean mFlash;
private boolean mAutoFocus;
private ArrayList<Integer> mSelectedIndices;
private int mCameraId = -1;
private ToggleButton btnFocus;
private ToggleButton btnFlash;
JSONObject barcodeMqtt, RawResult;
private PahoMqttClient pahoMqttClient;
private MqttAndroidClient client;
private MqttAndroidClient mqttAndroidClient;
private String userID;
private String message = "xyz";
private String demomsg = "NEW";
private String product_url = "null";
private Result rawResult;
private String UserID;
private String IEMINo;
private String uuserUUID;
private MqttHelper mqttHelper;
private JSONObject jsonObject;
private String productURL;
private String urlprod = "xy";
private String muserID = "xy";
StringBuilder servermessage = new StringBuilder(" ");
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} catch (NullPointerException e) {
System.out.print(e.getMessage());
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
//servermessage = new StringBuilder("hello");
userID = getIntent().getStringExtra("userid");
String brokermsg = getIntent().getStringExtra("brokermsg");
String prodScan = getIntent().getStringExtra("productscan");
String userUUID = getIntent().getStringExtra("userUUID");
String gifmsg = getIntent().getStringExtra("gifmsg");
muserID = getIntent().getStringExtra("muserID");
IEMINo = MqttConstant.getIMEI(BarcodeDump.this);
try {
Log.e("brokeeer", brokermsg);
Log.e("prodscan", prodScan);
Log.e("userUUID", userUUID);
Log.e("gifmsg", gifmsg);
Log.e("muserID", muserID);
//Log.e("url---product", mqttmsg);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("iemino", IEMINo);
message = brokermsg;
uuserUUID = userUUID;
pahoMqttClient = new PahoMqttClient();
if (savedInstanceState != null) {
mFlash = savedInstanceState.getBoolean(FLASH_STATE, false);
mAutoFocus = savedInstanceState.getBoolean(AUTO_FOCUS_STATE, true);
mSelectedIndices = savedInstanceState.getIntegerArrayList(SELECTED_FORMATS);
mCameraId = savedInstanceState.getInt(CAMERA_ID, -1);
} else {
mFlash = false;
mAutoFocus = true;
mSelectedIndices = null;
mCameraId = -1;
}
setContentView(R.layout.activity_barcode);
btnFocus = findViewById(R.id.focusButton);
btnFlash = findViewById(R.id.flashButton);
ViewGroup contentFrame = findViewById(R.id.content_frame);
mScannerView = new ZXingScannerView(this);
mScannerView.setSquareViewFinder(true);
mScannerView.setIsBorderCornerRounded(true);
mScannerView.setBorderCornerRadius(100);
mScannerView.setBorderColor(R.color.colorPrimary);
mScannerView.setLaserEnabled(false);
UserID = UUID.randomUUID().toString();
setupFormats();
contentFrame.addView(mScannerView);
btnFocus.setOnClickListener(v -> {
mAutoFocus = !mAutoFocus;
mScannerView.setAutoFocus(mAutoFocus);
});
btnFlash.setOnClickListener(v -> {
mFlash = !mFlash;
mScannerView.setFlash(mFlash);
});
client = pahoMqttClient.getMqttClient(BarcodeDump.this, Constants.MQTT_BROKER_URL, Constants.CLIENT_ID, MqttConstant.getIMEI(BarcodeDump.this));
mqttAndroidClient = pahoMqttClient.getMqttClient(BarcodeDump.this, Constants.MQTT_BROKER_URL, Constants.CLIENT_ID, MqttConstant.getIMEI(BarcodeDump.this));
mqttHelper = new MqttHelper(getApplicationContext());
mqttHelper.deviceIEMI(IEMINo);
try {
mqttmessage(message);
} catch (MqttException e) {
e.printStackTrace();
}
}
//----------------------------------------MQTT Server Message Method with Thread ---------------------------//
public void mqttmessage(String str) throws MqttException {
new Handler().postDelayed(() -> {
try {
mqttAndroidClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean b, String s) {
}
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws JSONException {
//setMessageNotification(s, new String(mqttMessage.getPayload()));
Log.e("barcode_dump", new String(mqttMessage.getPayload()));
Log.e("barcode_dump_topic", s);
if (s.contains("product_url") && mqttMessage.toString().contains("product_url")) {
product_url = mqttMessage.toString();
Log.e("product_url", product_url);
urlprod = product_url;
} else {
product_url = "This Product may not exist in the DATABASE";
}
servermessage.append(mqttMessage.toString());
try {
jsonObject = new JSONObject(mqttMessage.toString());
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (s.contains("old_user")) {
Log.e("BarcodeDump----Jsonobject", jsonObject.getString("userUUID"));
} else {
Log.e("else", "else---block");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
try {
mqttAndroidClient.subscribe("product_url/#", 1);
mqttAndroidClient.subscribe("new_user/#", 1);
} catch (MqttException ex) {
ex.printStackTrace();
}
}, 1500);
}
private void setDefaultFragment(Fragment defaultFragment) {
this.replaceFragment(defaultFragment);
}
public void replaceFragment(Fragment destFragment) {
//First get FragmentManager object
FragmentManager fragmentManager = this.getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Replace the layout holder with the required Fragment object.
fragmentTransaction.replace(R.id.content_frame, destFragment);
// Commit the Fragment replace action.
fragmentTransaction.commit();
}
@Override
protected void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera(CameraSource.CAMERA_FACING_BACK);
mScannerView.setFlash(mFlash);
mScannerView.setAutoFocus(mAutoFocus);
}
@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
//-----Barcode / QR Code Result Manipulation------------//
@Override
public void handleResult(Result rawResult) {
Log.d("rawresult", rawResult.toString());
String product_scanned = "www.google.com";
try {
Log.e("urlprod", urlprod);
} catch (Exception e) {
e.printStackTrace();
}
if (rawResult.getBarcodeFormat() == BarcodeFormat.QR_CODE) {
try {
RawResult = new JSONObject();
try {
RawResult.put("code", rawResult.toString());
RawResult.put("code_type", "qr");
RawResult.put("store_id", "BPR_50622052018");
RawResult.put("device_id", IEMINo);
Log.d("jsonresult", RawResult.toString());
} catch (JSONException e) {
e.printStackTrace();
}
product_scanned = getBarcodeMqttObject(false, rawResult.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
RawResult = new JSONObject();
try {
RawResult.put("code", rawResult.toString());
RawResult.put("code_type", "bar");
RawResult.put("store_id", "BPR_50622052018");
RawResult.put("device_id", IEMINo);
Log.d("jsonresult", RawResult.toString());
} catch (JSONException e) {
e.printStackTrace();
}
product_scanned = getBarcodeMqttObject(true, rawResult.toString());
try {
pahoMqttClient.publishMessage(client, RawResult.toString(), 0, MqttConstant.PRODUCT_CODE + IEMINo);
} catch (MqttException | UnsupportedEncodingException x) {
x.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//-----------------------------------Based on Mqtt Message which Activity must Open------------------------//
try {
if (message.equals("NEW USER")) {
Log.e("new-user-id", UserID);
// Toast.makeText(this, "NEW USER!!!", Toast.LENGTH_LONG).show();
Intent a = new Intent(BarcodeDump.this, FormAct.class);
a.putExtra("productURL", rawResult.toString());
a.putExtra("RawResult", RawResult.toString());
a.putExtra("product-url", urlprod);
a.putExtra("productscan", product_scanned);
a.putExtra("userid", UserID);
a.putExtra("message", message);
startActivity(a);
finish();
} else if (message.equals("error_no_face")) {
Log.e("new-user-id", UserID);
// Toast.makeText(this, "ErrorFace", Toast.LENGTH_LONG).show();
Intent c = new Intent(BarcodeDump.this, FormAct.class);
c.putExtra("productURL", rawResult.toString());
c.putExtra("product-url", urlprod);
c.putExtra("RawResult", RawResult.toString());
c.putExtra("productscan", product_scanned);
c.putExtra("userid", UserID);
c.putExtra("message", message);
startActivity(c);
finish();
} else {
try {
pahoMqttClient.publishMessage(client, product_scanned, 0, MqttConstant.PUBLISH_TOPIC + IEMINo);
} catch (MqttException | UnsupportedEncodingException x) {
x.printStackTrace();
}
// Toast.makeText(this, "Else Block", Toast.LENGTH_LONG).show();
Intent b = new Intent(BarcodeDump.this, ResultActivity.class);
b.putExtra("productURL", rawResult.toString());
b.putExtra("product-url", urlprod);
b.putExtra("RawResult", RawResult.toString());
b.putExtra("productscan", product_scanned);
b.putExtra("userid", userID);
startActivity(b);
finish();
}
} catch (Exception e) {
// Toast.makeText(this, "Welcome To BPRISE STORE!!!!", Toast.LENGTH_LONG).show();
try {
pahoMqttClient.publishMessage(client, product_scanned, 0, MqttConstant.PUBLISH_TOPIC + IEMINo);
} catch (MqttException | UnsupportedEncodingException x) {
x.printStackTrace();
}
Intent b1 = new Intent(BarcodeDump.this, ResultActivity.class);
b1.putExtra("productURL", rawResult.toString());
b1.putExtra("product-url", urlprod);
b1.putExtra("RawResult", RawResult.toString());
b1.putExtra("productscan", product_scanned);
b1.putExtra("userid", userID);
startActivity(b1);
finish();
}
}
//------------------------------------Getting the Bar/QR Code Object as an JSON----------------------------//
@NotNull
@SuppressLint("SimpleDateFormat")
private String getBarcodeMqttObject(Boolean isBarcode, String result) {
try {
Date currentDate = new Date();
barcodeMqtt = new JSONObject();
Log.e("userid", userID);
Log.e("new-user : id", UserID);
Log.e("b-obj-museriD", muserID);
barcodeMqtt.put("device_id", MqttConstant.getIMEI(BarcodeDump.this));
if (!muserID.equals(EMPTY)) {
UserID = muserID;
} else {
Log.e("user--id--issue", "OOPS ID ISSUE");
}
try {
if (message.equals("NEW USER")) {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_UUID, "None");
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_ID, UserID);
} else if (message.equals("error_no_face")) {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_UUID, "None");
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_ID, UserID);
} else if (message.contains("userUUID")) {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_UUID, uuserUUID);
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_ID, UserID);
} else {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_UUID, "None");
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_ID, userID);
}
} catch (Exception e) {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.USER_ID, userID);
e.printStackTrace();
}
if (isBarcode) {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.BARCODE, result);
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.QRCODE, "None");
} else {
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.BARCODE, "None");
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.QRCODE, result);
}
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.STORE, "Chakala");
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.SCANNED_DATE, new SimpleDateFormat("dd/MM/YYYY").format(currentDate));
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.SCANNED_TIME, new SimpleDateFormat("hh:mm:ss").format(currentDate));
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.DEVICE_IEMI, "None");//AppConstants.getIMEI(BarcodeActivity.this)
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.DEVICEID_UUID, AppConstants.getDeviceUniqueID(BarcodeDump.this));
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.APP_VERSION, AppConstants.appVersion);
barcodeMqtt.put(MqttConstants.PRODUCT_SCANNED.OS, "Android " + AppConstants.version);
// barcodeMqtt.put("UserID",userID);
Log.d("barcode-dump", barcodeMqtt.toString());
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
return barcodeMqtt.toString();
}
//------------------------------------Setting all Formats of Bar-code for the Method---------------------------//
public void setupFormats() {
List<BarcodeFormat> formats = new ArrayList<>();
if (mSelectedIndices == null || mSelectedIndices.isEmpty()) {
mSelectedIndices = new ArrayList<>();
for (int i = 0; i < ZXingScannerView.ALL_FORMATS.size(); i++) {
mSelectedIndices.add(i);
}
}
for (int index : mSelectedIndices) {
formats.add(ZXingScannerView.ALL_FORMATS.get(index));
}
if (mScannerView != null) {
mScannerView.setFormats(formats);
}
}
}