0
votes

I'm building a basic price checker app that scans a barcode and displays information for the product and am trying to run it on an android-powered tablet that comes with a built-in barcode scanner.

The scanner works and if I put a textbox on the app and focus to it, the barcode I scan gets written onto it just fine - however I have been unable to catch the input without having the app focus on a textbox (the app should have no input areas, only images and textview labels).

The scanner shows up as an HID keyboard on the input android settings.

Almsot all the posts I find here are about using the camera to scan barcodes (built my original prototype using this but performance was subpar). One old post here gave me a hint about overriding the dispatchKeyEvent as so

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getCharacters() != null && !event.getCharacters().isEmpty()) {
        isRunning = true;
        Log.d(TAG, "Starting");

        String barcode = event.getCharacters();
        new myImageTask().execute(barcode);
    }
    return super.dispatchKeyEvent(event);
}

However it doesn't seem to be catching any input.

I looked at overriding KeyUp and KeyDown events but they seem to be explicitly built for catching single key events.

Is there another event I could use to catch and read the scanner's full input or should I just chain the KeyDown event to buffer each individual key into a static variable and, after receiving a special input termination character and run my task on the result?

1
Just in case: Does the tablet with built-in scanner have a SDK so that you can access the barcode data directly? - Morrison Chang
The supplier did not provide any sdk or information regarding one. The scanner component shows up as Honeywell brand so perhaps I could check there if there is no wya of reading their input from core Android level. - ConnorU
If unable to find SDK (usual search on model number or image of similar device if rebadged model) you could try to have a 1x1 edittext which would hold focus and capture hid input. - Morrison Chang

1 Answers

0
votes

See if the code below can be of help to you:

barcodeEditText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                            saveToDBMethod();
                            barcodeEditText.setText("");
                            barcodeEditText.requestFocus();
                        return true;
                    default:
                        break;
                }
            }

            return false;
        }
    });