private ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),result -> {
try {
if (result.getContents() != null) {
String data=result.getContents();
(new BarcodeSender(getApplicationContext(),String.valueOf(HOST_ADDR.getText()),3946)).execute(data);
}
}catch (Exception ex){
Toast.makeText(getApplicationContext(),"BARCODE "+ex.toString(),Toast.LENGTH_LONG).show();}
statusText.setTextColor(Color.rgb(255,0,0));
});
I use ZXing package with Android/Java in Android Studio. I can read barcode data but cant send TCP Server.
BarcodeSender.java
public class BarcodeSender extends AsyncTask<String, Void, Void> {
Socket socket;
PrintWriter printWriter;
String adr;
int port;
Context context;
public BarcodeSender(Context context, String Adress, int PORT) {
this.adr = Adress;
this.port = PORT;
this.context=context;
Toast.makeText(this.context, adr+":"+port, Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(String... voids) {
String mess = voids[0];
try {
Toast.makeText(this.context, mess, Toast.LENGTH_SHORT).show();
Toast.makeText(this.context, this.adr+":"+this.port, Toast.LENGTH_SHORT).show();
Socket socket = new Socket(this.adr,this.port);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF(mess);
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception exs) {
Toast.makeText(this.context,"MSG_SENDER "+exs.toString(),Toast.LENGTH_LONG).show();
}
return null;
}
}
Toast messages cant showing because my app is shutdown.