Android扫码器通过USB数据通信而不是串口数据通信!dispatchKeyEvent代替EditText控件

拦截dispatchKeyEvent方法

重写dispatchKeyEvent方法

1
2
3
4
5
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
toolScanner.analysisKeyEvent(event);
return true;
}

相关逻辑处理工具类:UsbTransferDataUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* 扫描事件处理
*/
public class UsbTransferDataUtils {
private final Handler mHandler;
private boolean mCaps;
private IOnScanSuccessListener mOnScanSuccessListener;
private StringBuffer mStringBufferResult;
private Runnable mScanningFishedRunnable;

public UsbTransferDataUtils(IOnScanSuccessListener onScanSuccessListener) {
mOnScanSuccessListener = onScanSuccessListener;
mStringBufferResult = new StringBuffer();
mHandler = new Handler();
mScanningFishedRunnable = new Runnable() {
@Override
public void run() {
performScanSuccess();
}
};
}

public void analysisKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode(); //字母大小写判断
checkLetterStatus(event);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
char aChar = getInputCode(event);
if (aChar != 0) {
mStringBufferResult.append(aChar);
}
if (keyCode == KeyEvent.KEYCODE_ENTER) { //若为回车键,直接返回
mHandler.removeCallbacks(mScanningFishedRunnable);
mHandler.post(mScanningFishedRunnable);
} else { //延迟post,若500ms内,有其他事件
mHandler.removeCallbacks(mScanningFishedRunnable);
mHandler.postDelayed(mScanningFishedRunnable, 500);
}
}
}

private void checkLetterStatus(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
if (event.getAction() == KeyEvent.ACTION_DOWN) { //按着shift键,表示大写
mCaps = true;
} else { //松开shift键,表示小写
mCaps = false;
}
}
}

//获取扫描内容 6921734962300
private char getInputCode(KeyEvent event) {
int keyCode = event.getKeyCode();
char aChar;
if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) { //字母
aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
} else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) { //数字
aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
} else { //其他符号
switch (keyCode) {
case KeyEvent.KEYCODE_PERIOD:
aChar = '.';
break;
case KeyEvent.KEYCODE_MINUS:
aChar = mCaps ? '_' : '-';
break;
case KeyEvent.KEYCODE_SLASH:
aChar = '/';
break;
case KeyEvent.KEYCODE_BACKSLASH:
aChar = mCaps ? '|' : '\\';
break;
default:
aChar = 0;
break;
}
}
return aChar;
}

//接口回调
public interface IOnScanSuccessListener {
void onScanSuccess(String barcode);
}

private void performScanSuccess() {
String barcode = mStringBufferResult.toString();
if (mOnScanSuccessListener != null) mOnScanSuccessListener.onScanSuccess(barcode);
mStringBufferResult.setLength(0);
}
}

处理回调结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 /**
* s扫描事件监听
*
* @param barcode
*/
@Override
public void onScanSuccess(String barcode) {
mealCode = barcode;
Log.e("yezhou", barcode);
if (mealCode.length() > 0) {
//扫码调接口

}
}

自定义Edittext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class ScannerEditText extends AppCompatEditText implements TextWatcher {

private ScannerTextInterface textInterface;

public ScannerEditText(Context context) {
super(context);
}

public ScannerEditText(Context context, AttributeSet attrs) {
super(context, attrs);
addTextChangedListener(this);
}

public void ScannerTextCallBack(ScannerTextInterface anInterface){
this.textInterface = anInterface;
}

public interface ScannerTextInterface{
void getScavengingText(String s);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if (str.length() > 2) {
if (str.endsWith("\n")) {
if (textInterface != null){
textInterface.getScavengingText(str);
}
s.clear();
}
}
}
}

在对应的Activity implements ScannerEditText.ScannerTextInterface接口,重写方法getScavengingText(String s)在这个方法中参数就是我们扫描获取到的结果

问题缺陷:重写dispatchKeyEvent方法后,这个方法依赖Activity,确实可以监听到扫码器通过USB传递过来的数据,但是当这个Activity界面不可见时也就是执行onPause方法后就不会执行该方法。第二种方法也是依赖Activity

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :