Reputation: 3253
I'm reading barcodes/cards with external device connected to android device by USB.
External device sends it's input as KeyEvent to Activity and I can read it in dispatchKeyEvent()
method inside Activity
like this
@Override public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
externalDeviceService.onNewInputFromExternalDevice(((char) event.getUnicodeChar()));
}
return super.dispatchKeyEvent(event);
}
But for example when there is focus on some EditText
, it types external device input to that input field instead of just delegating to service.
Is there a way to distinguish between input from android device(soft keyboard) and from external device(RFID card reader or barcode scanner)?
Obvious solution would be keyEvent.getDevice().getName()
, and recognize external device to handle event appropriately, but that binds me to specific devices which is huge limitation. Maybe there is a way to check if KeyEvent
comes from android soft keyboard?
Any insight on this would be appreciated.
Upvotes: 0
Views: 1481
Reputation: 3253
For anyone interested in the future, I send KeyEvents from dispatchKeyEvent()
to some outside service, if next character appears in specified amount of time after previous, it means that it comes from scanner/rfid. In other cases I send KeyEvent back to Activity and call super.dispatchKeyEvent()
@Edit
Another way is getting list of USB Connected devices from UsbManager
. Use that list to determine if KeyEvent parent is inside that list to distinguish. Caveat is that, if you connect hardware keyboard, it treats KeyEvents as it comes from Barcode/RFID card Scanner
Upvotes: 1