I'm trying to handle value from a barcode scanner USB via my Android 3.2 tablet, the scanner is succefuly working in the OS but i want to get the value in the program without a edittext, usbmanager host and accessory did not list it with the connected devices via USB.
most plug in barcode scanners (that I've seen) are made as HID profile devices so whatever they are plugged into should see them as a Keyboard basically. I think this is why they do not show up in the USB host APIs list of accessories. You should be able to get raw input from them the same way you would a keyboard inside your Activity by overriding Activity.onKeyDown(int keycode, KeyEvent ke)
Something like this in your activity:
#Override
protected boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("TAG", ""+ keyCode);
//I think you'll have to manually check for the digits and do what you want with them.
//Perhaps store them in a String until an Enter event comes in (barcode scanners i've used can be configured to send an enter keystroke after the code)
return true;
}
you will get the result on Activity keydown event.
For Example:-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
char pressedKey = (char) event.getUnicodeChar();
Barcode += "" + pressedKey;
Toast.makeText(getApplicationContext(), "barcode--->>>" + Barcode, 1)
.show();
return true;
}
Hope this post will help you.
I also had same problem,but when i used onKeyDown or onKeyUp,it was not called every time i mean for every character for barcode.I Used DiapatchKeyEvent,and it worked nice.
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
Log.i("TAG", event.getCharacters());
return true;
}`
Related
I'm building a basic price checker app that scans a barcode and displays information for the product and am trying to run it on an android-powered tablet that comes with a built-in barcode scanner.
The scanner works and if I put a textbox on the app and focus to it, the barcode I scan gets written onto it just fine - however I have been unable to catch the input without having the app focus on a textbox (the app should have no input areas, only images and textview labels).
The scanner shows up as an HID keyboard on the input android settings.
Almsot all the posts I find here are about using the camera to scan barcodes (built my original prototype using this but performance was subpar). One old post here gave me a hint about overriding the dispatchKeyEvent as so
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getCharacters() != null && !event.getCharacters().isEmpty()) {
isRunning = true;
Log.d(TAG, "Starting");
String barcode = event.getCharacters();
new myImageTask().execute(barcode);
}
return super.dispatchKeyEvent(event);
}
However it doesn't seem to be catching any input.
I looked at overriding KeyUp and KeyDown events but they seem to be explicitly built for catching single key events.
Is there another event I could use to catch and read the scanner's full input or should I just chain the KeyDown event to buffer each individual key into a static variable and, after receiving a special input termination character and run my task on the result?
barcodeEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
saveToDBMethod();
barcodeEditText.setText("");
barcodeEditText.requestFocus();
return true;
default:
break;
}
}
return false;
}
});
It may be a hardware configuration trouble. In my case, using Honeywell Android devices with barcode scans, I always have to go to the scan settings and set in barcode reading options the wedge method to keyboard
Edit: KeyEvent.getCharacters() was deprecated in Android 29, so I am not sure the below is a viable long-term solution.
I know this was asked a few years ago, but I am able to capture barcode scans from both a Honeywell and Zebra devices, running Android 11 and 10, respectively, by overriding onKeyMultiple in MainActivity:
#Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
System.out.println("Key Multiple event: " + event.getCharacters());
return super.onKeyMultiple(keyCode, repeatCount, event);
}
Even when I don't have an input in focus, the above fires when I perform a scan. Output:
Key Multiple event: some-barcode
There are a couple of SO posts that have already discussed similar issues, but I find them not much relevant or too complex. I am working with an Android TV which has a remote controller, and it is supposed to be used in an enterprise environment. Users should not be able to have much control, so I have to restrict some of the functions available on the remote.
I created a simple app that overrides onKeyDown() and displays pressed keys using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Read the input and display its code
switch(keyCode) {
case KeyEvent.KEYCODE_HOME:
mTextView.setText("Home");
break;
default:
char c = event.getDisplayLabel();
String code = String.valueOf(keyCode);
String displayText = c + " " + keyCode;
mTextView.setText(displayText);
mTextView.setBackgroundColor(mColor^=Color.GREEN);
}
return true;
}
I am able to capture most of the keys and override their behaviors most notably 131, 132, 133, and 134 (used as Media, TV, Web, and App shortcuts respectively on the remote).
The only problem is the Home button which AOSP source code (KeyEvent.java) says is system-specific:
* This key is handled by the framework and is never delivered to applications. */
public static final int KEYCODE_HOME = 3;
A solution that struck me was to extend KeyEvent and override the method isSystem() to return false when the selected key is KEYCODE_HOME. But, this method is defined final and I cannot override it. Any ideas?
you need to use
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
in your manifest and
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(this, ""+event.getKeyCode(), Toast.LENGTH_SHORT).show();
return false;
return super.dispatchKeyEvent(event);
}
i think control home key is 3
i hope was helpful
I wanted to check programmatically that whether the input given by user is either from soft keyboard or the barcode scanner attached to the android tablet.
There is no edit text in activity where I want to apply this so please avoid providing solutions that are applicable through EditText.
With dispatchKeyEvent you can listen for any KeyEvent from the barcode scanner.
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
// do something on input
return false; // prevent default behaviour
}
Use following code to read input/values from barcode scanner, so onKeyDown you need to override in your activity/dialog
String barcode="";
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
log("Key Down keyCode " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {//if scanner doesn't return enter key code then make sure that any view must not have focus in window
//write your code to process the scanned barcode input
barcode = "";
} else {
Character input = (char) event.getUnicodeChar();
log("Scanner Input " + input);
if (Character.isDigit(input) || Character.isLetter(input)) {
barcode += input;//concat the characters
}
}
return true;
}
I have external bluetooth device, which is basically something like a keyboard.
But instead of letters and numbers this keyboard is sending data in ASCII.
instead of "0" it will send "alt+48"
instead of "M" it will send "alt+77"
And since Android doesn't have ALT he is is ignoring all received data (=ALT is pressed while receiving letters = I will ignore these letters).
Do you have any idea how to filter out pressed ALT? Or make Android accept send letters with pressed ALT?
override dispatchKeyEvent(KeyEvent event)
and use action KeyAction.ACTION_DOWN else you'll get duplicate data
and then set that add that character in your edit text
edittext.setText(edittext.getText().toString+(char)
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN) {
int c = event.getUnicodeChar();
if (IS_NEW_RF_ID) {
rfIdAcTv.setText("");
}
}
return super.dispatchKeyEvent(event);
}
I want to pop up the software keyboard when the user presses the search hardware search key.
At the moment I use the following function with doesn't seem to work for the search key but which does work for the back key:
The logging doesn't get even tiggered through the search key.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
Log.v(TAG, "On back key press");
renderPrevious();
return false;
}
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Log.v(TAG, "On search key press");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
return true;
}
I have no text field but want to handle the input directly myself if that matters.
this method setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) doesnt wakeup the keyboard. Check this link. More to know about it check this example. hope it helps
Edit:
try this showSoftInput method
Maybe this would be helpful:
InputMethodManager inputMgr = (InputMethodManager)getSystemService Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
http://plainoldstan.blogspot.com/2010/09/android-set-focus-and-show-soft.html
"When experimenting I was not actually getting what I wanted until I realized I should have an emulator device with no hardware keyboard:"