Allow Bluetooth keyboard input even if the screen is locked

I am developing an application that allows you to scan user barcodes using an external barcode scanner connected via bluetooth. The barcode scanner works like a keyboard, i.e. Android believes that the scanned barcodes were entered on the keyboard.

The application works fine while it remains.

As soon as the screen turns off, I can continue to scan barcodes, but the scanned text no longer reaches the application, but rather causes actions on the lock screen.

Is there a way to allow input from an external keyboard into the application, although the screen is off?

Alternatively, I will have to force the screen to remain on, but this is not bulletproof, since the user may accidentally lock the screen.

UPDATE

I took a small step in the right direction using:

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

This will show the application without a lock screen (insecure) when processing new keyboard input. Unfortunately, the first letter is missing. This should be because the first letter wakes up the screen and the rest of the input actually reaches the value of EditText .

+6
source share
2 answers

This works for me in a similar situation. Just hold the screen while your application is in the foreground.

 getWindow().addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ); 
+2
source

if you lose only the first number, then you can ... restore it using Check Digit .

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

+1
source

Source: https://habr.com/ru/post/983322/


All Articles