Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow input from bluetooth keyboard even if screen locked

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.

like image 718
Baz Avatar asked Mar 04 '15 14:03

Baz


2 Answers

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
);
like image 183
Apar Amin Avatar answered Sep 29 '22 00:09

Apar Amin


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.

like image 20
kaho Avatar answered Sep 28 '22 23:09

kaho