androidflutterbarcode-scanner

Flutter - Weird behavior when reading RawKeyEvents after opening soft keyboard


I'm developing a Flutter app that will end up running on an Android based scanning device. As with many hardware scanners, the device rapidly fires a number of keyboard events upon scanning a barcode. I've resorted to a Flutter package that seemed ideal for my usecase: https://pub.dev/packages/flutter_barcode_listener - Basically all this does is listen for RawKeyEvents using Flutter's built-in RawKeyboard class, translate those to usable characters, fill an array with those characters and upon receiving a linefeed (\n), join the array into a string and return it.

This works fine until a soft keyboard is opened within the app, as soon as this has happened the only RawKeyEvent the listener receives after scanning a barcode is a linefeed and nothing else. As a result an empty string is returned after scanning a barcode. Weirdly enough, causing the app to lose focus (e.g. by minimizing it with the home button or opening the recent app overview) will restore functionality to the barcode listener, and the proper RawKeyEvents will be generated once more.

This is definitely an issue on Flutter's end, as I confirmed the scanner still reads out data through ADB logging. Any pointers as to what may be causing this are very welcome.

I'd also like to add that ADB logging tells me the following after scanning a barcode:

3104  9582 D ScanDataOutput: softkeyboard is not running (2)
3104  9582 D ScanDataOutput: SSI(terminator):<LF>
3104  9582 D ScanDataOutput: LF, 4
3104  9582 E ScanDataOutput: ERROR! terminator = <LF>!
3104  9582 E ScanDataOutput: ERROR! terminator :
3104  9582 E ScanDataOutput: dataResult:

Solution

  • UPDATE 01/08/24

    The Flutter bug mentioned in my post below has been solved as of Flutter v.3.22.0 via the following PR https://github.com/flutter/engine/pull/49829. I've been able to confirm any software keyboard now behaves properly, and no longer causes broken RawKeyEvent handling.


    Turns out this was an issue with the hardware scanner I was using. The scanner wasn't configured to explicitly use KeyEvents as an output method. This lead me to another issue where Flutter's RawKeyBoard library would cause the aforementioned softkeyboard to activate upon receiving a KeyEvent. This caused undesirable behavior where scanning a barcode without being focused on a TextInput would still cause the keyboard to open.

    If anyone is experiencing similar issues on Android, please refer to the following github thread. Unfortunately, this is a bug with Flutter itself and can only be circumvented by using certain third party software keyboards that are not affected by this bug for now.