7

I'm developing an application that lets the user scan barcodes using an external barcode scanner connected via bluetooth. The barcode scanner acts as a keyboard, i.e. Android thinks that the scanned barcodes have been typed on a keyboard.

The app is working fine as it is, as long as the screen stays on.

Once the screen turns off, I can continue scanning barcodes, but the scanned text doesn't reach the app anymore, but rather invokes actions on the lock screen.

Is there a way to allow input from an external keyboard to the app although the screen has been turned off?

Alternatively I will have to force the screen to stay turned on, but this isn't bullet-proof, as the user might accidentally lock the screen.

UPDATE

I've made a small step in the right direction by using:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

This will show the app without the (non-secure) lock screen when a new input from the keyboard is processed. Unfortunately, the first letter is missing. This has to be caused by the first letter waking the screen and the rest of the input actually reaching the EditText.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • add this code to your scanner activity `getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);` and add this permission in manifest ``. you are done. – Amrut Bidri Mar 15 '15 at 10:29
  • 2
    Depending on the version, you might developp a widget to add on the lock screen. This could listen to the keyboad (BT scanner) and send the data to your activity. – AxelH Mar 16 '15 at 08:44

2 Answers2

3

This works for me in a similar situation. Just keep screen on while your application in foreground.

getWindow().addFlags(
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
);
Spooky
  • 2,966
  • 8
  • 27
  • 41
Apar Amin
  • 653
  • 12
  • 22
  • When the user accidentally presses the power button of his device, will this still forward the input events to the app? – Baz Mar 14 '15 at 08:23
  • I guess there won't be a bullet-proof approach. What happens if another activity places itself in the foreground? Wouldn't it be enough to echo a confirmation if a valid code was received? – sstn Mar 16 '15 at 09:09
  • @sstn The app already does that, the user can disable this feature though. The difference between locking the screen and switching to another app is that locking the screen happens more easily (because it's a hardware button that is clicked easily). I guess I'm out of luck here... – Baz Mar 16 '15 at 09:45
  • @Baz it won't affect in my case you can try it out, sstn explanation to your prob: "my solutions might not sounds bulletproof but android devloper WS it self stated it use those flag instead of Wakeup lock and i dnt think Wakeup lock required in this situation another case android framework have highest right for phone call if any call arrive during any process e.g Payment on Amazon and if user uses mobile data then!!" we just can hope for best and in this case i found it most useful solution by adding those flag anyways thanks for surfacing your view i try to find better one next time. – Apar Amin Mar 16 '15 at 09:47
  • @AparAmin Since this answer was the most helpful so far, you get the bounty, congrats. – Baz Mar 16 '15 at 10:01
1

if you are only losing the first number, then you can... recover that using the Check Digit.

I know it is a hack but it should solve this problem.

kaho
  • 4,746
  • 1
  • 16
  • 24
  • I'm missing the first character of the decoded barcode (the final string). I don't have access to what the barcode scanner actually read in the first place, just what it returns as the keyboard. I also don't know what kind of barcode it is that I receive. So I'm afraid, this is not an option. – Baz Mar 04 '15 at 17:09