Allow input from bluetooth keyboard even if screen locked
Asked Answered
Z

2

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.

Zambia answered 4/3, 2015 at 14:2 Comment(2)
add this code to your scanner activity getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); and add this permission in manifest <uses-permission android:name="android.permission.WAKE_LOCK" />. you are done.Gardy
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.Prepay
P
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
);
Pavior answered 14/3, 2015 at 5:16 Comment(5)
When the user accidentally presses the power button of his device, will this still forward the input events to the app?Zambia
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?Gilberto
@Gilberto 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...Zambia
@Zambia 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.Pavior
@AparAmin Since this answer was the most helpful so far, you get the bounty, congrats.Zambia
P
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.

Possibility answered 4/3, 2015 at 16:42 Comment(1)
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.Zambia

© 2022 - 2024 — McMap. All rights reserved.