I want to scan a Barcode in my app. I could start the scan but I do not know how to detect the scanned value. My code looks like
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.pick, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// scan
this.getActivity().findViewById(R.id.scan).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Start the scanner like in the example: https://developer.motorolasolutions.com/docs/DOC-1874#PROGRAMMING%20INTERFACE Intent intent = new Intent();
intent.setAction("com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER");
intent.putExtra("com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER", "TOGGLE_SCANNING");
view.getContext().sendOrderedBroadcast(intent, null, receiver, null, 0, null, null);
}
});
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// How to detect the scanned value???
}
}
}
I could solve it. This is the code.
Intent intent = new Intent();
intent.setAction("com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER");
intent.putExtra("com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER", "TOGGLE_SCANNING");
getActivity().sendBroadcast(intent);
getActivity().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String code = intent.getExtras().getString("com.motorolasolutions.emdk.datawedge.data_string");
// do something
getActivity().unregisterReceiver(this);
}
}
, new IntentFilter("MyIntentAction"));
The profile in the DataWedge
has to enable the output via intent and disable the keystroke output. As intent action is to set a String
which is the same as "MyIntentAction"
. Then I get the scanned barcode directly in my Fragment after scanning.