How can I get text data from Handheld QR code scanner?

924 Views Asked by At

I am using honeywell barcode scanner(HF680). And I connected it to my Android Device(Desktop). I need to get the scanned data as text. It works like keyboard. So, It seems like I can't get the data internally like Camera Scanning.

How can I approach this?

I don't think Honeywell's supporting SDK nor Document. I can't find it.

3

There are 3 best solutions below

1
On BEST ANSWER

It depends on the port you use. HF680 provides two different ports. If you use USB then, you can't use serial input. But it acts as keyboard. So, you can onKeyDown method.

    val barcode = StringBuilder("")

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        // TODO: add up the chars
        barcode.append(...)
        return super.onKeyDown(keyCode, event)
    }

And generally, scanners provides some feature that indicates the end such as '\n' or Enter key or special characters. Otherwise, you can set a receiving time so that after a few milliseconds it wouldn't append the string.

6
On

The easiest way is probably to use an EditText. The scanner input will go into it when it is focused. You can then read it from there.

0
On

Its good to use dispatchKeyEvent than onKeyDown.
Because, dispatchKeyEvent() will pass the event to onKeyListener if there is activity. This is when onKey() is called.

private var barcode = ""
override fun dispatchKeyEvent(event: android.view.KeyEvent): Boolean {
    
    val c = event.unicodeChar

    //accept 0-9, A-Z, a-z, " " and "enter"
    if (c in 48..57 || c in 65..90 || c in 97..122 || c == 10 || c == 32) {
      if(event.action == 0) {
        if (c in 48..57 || c in 65..90 || c in 97..122 || c == 32) {
          barcode += c.toChar()
        } else {
          if (barcode != "") {
            val bar = barcode
            barcode = ""
            Toast.makeText(this, "Barcode: $bar", Toast.LENGTH_SHORT).show()
            Log.i(TAG, "Barcode: $bar")
          }
        }
      }
    }
    return true
}