0

I am trying to receive the scanned barcode result from a device paired via (Bluetooth/USB) to an android device. so many topics said :

most plug-in barcode scanners (that I've seen) are made as HID profile devices so whatever they are plugged into should see them as a Keyboard basically.

source

So I am using this code to receive the result of the scan:

 @Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if (viewModel.onTriggerScan()) {

        //1
        char pressedKey = (char) event.getUnicodeChar();
        viewModel.addCharToCode(pressedKey);
        //2
        String fullCode = event.getCharacters();
        viewModel.fullCode(fullCode);

        //check if the scan is done, received all the chars
        if (event.getAction() == EditorInfo.IME_ACTION_DONE) {
            //does this work ?
            viewModel.gotAllChars();
            //3
            String fullCode2 = event.getCharacters();
            viewModel.fullCode(fullCode2);
        }

        return true;

    } else
        return super.dispatchKeyEvent(event);

}

Note: I don't have a barcode scanner device for the test.

which code will receive the result ?? (1 or 2 or 3 ?)

  • I have had plenty of experience with bluetooth barcode scanners. While scanners are typically set to HID out of the box, devices can be changed over to Serial Port Profile (SPP) Mode which will allow you to connect to the device and listen for information via normal bluetooth connection. – tyczj Oct 25 '21 at 21:45
  • @tyczj but your implementation does not create a problem of device type(brand)? each device will send results in a different form??, so I need to deal with the device type(brand), I am avoiding dealing with devices brands because it is like cancer. – Monsef Khalfaoui Oct 25 '21 at 23:50
  • Not sure what you mean by they will send the result in a different form, every device I have tried will send the same value. What may differ (but is still configurable by the scanner) is the value terminator. Most send a `\n` or `\r` to indicate the end of the value but some of the higher end scanners dont send anything but you can configure the scanner to put a postfix value to indicate the end of value. Where your implementation fails is if the user needs to use the keyboard and scanner at the same time – tyczj Oct 26 '21 at 22:32
  • @tyczj I appreciate your explanation, and I was meaning by different form: the configuration of the scanner, each scanner receive/has different command. The scanner will send char by char or full code and different values of a terminator. please could you tell me if I can do the configuration for the scanner connected via usb?? – Monsef Khalfaoui Oct 27 '21 at 08:53
  • I have only done stuff with scanners through Bluetooth so I dont know but I would assume the configurations would also apply to USB too since you are configuring how the scanner presents its values not the connection – tyczj Oct 28 '21 at 02:05

2 Answers2

0

You won't ever see an IME_ACTION_DONE, that's something that's Android only and an external keyboard would never generate.

After that, it's really up to how the scanner works. You may get a full key up and key down for each character. You may not, and may receive multiple characters per event. You may see it finish with a terminator (like \n) you may not- depends on how the scanner is configured. Unless you can configure it yourself or tell the user how to configure it, you need to be prepared for either (which means treating the data as done either after seeing the terminator, or after a second or two once new data stops coming in.

Really you need to buy a configurable scanner model and try it in multiple modes and make ever mode works. Expect it to take a few days in your schedule.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Workaround solution but it works 100%. the solution is based on clone edittext (hidden from the UI), this edit text just receives the result on it, adds a listener, and when the result arrives gets it and clears the edittext field. An important step, when you try to receive the result(trigger scan) make sure that edittext has the focus otherwise you wil not get the result.

Quick steps:

1- create editText (any text field that receives inputs) in your layout

2- set its visibility to "gone" and clear it.

3- add onValueChangeListener to your edittext.

4- focus your edittext when you start trigger the scan

5- each time you the listener call, get the result and clear edittext

Note: never miss to focus your edittext whenever you start trigger scan.

Note: this method work(99%) for all external scan device and any barcode type.