Skip to content

Commit d9c6779

Browse files
committed
feat(version update): 扫码枪 2.0 版本
1 parent 72483a4 commit d9c6779

File tree

10 files changed

+78
-90
lines changed

10 files changed

+78
-90
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# macOS
22
.DS_Store
3+
.vscode

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import scanGun from 'react-native-external-scan-gun';
4444
useEffect(() => {
4545
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
4646
const eventListener = eventEmitter.addListener(
47-
scanGun.onScanCodeRecevieData,
47+
scanGun.onScanCodeReceiveData,
4848
code => {
4949
// TODO: 扫码之后的回调
5050
},

README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import scanGun from 'react-native-external-scan-gun';
4646
useEffect(() => {
4747
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
4848
const eventListener = eventEmitter.addListener(
49-
scanGun.onScanCodeRecevieData,
49+
scanGun.onScanCodeReceiveData,
5050
code => {
5151
// TODO: Callback after code scanning
5252
},

android/src/main/java/com/afei/scangun/ScanGunManager.java

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,145 +7,141 @@
77
public class ScanGunManager {
88

99
private final static long MESSAGE_DELAY = 500; // 延迟500ms,判断扫码是否完成。
10-
private StringBuffer mStringBufferResult; // 扫码内容
11-
private boolean mCaps; // 大小写区分
10+
private StringBuffer stringBufferResult; // 扫码内容
1211

1312
private ScanCodeCallback scanCodeCallback;
1413

1514
public interface OnScanCodeListener {
1615
void onScanCode(String value);
1716
}
1817

19-
2018
// 类初始化时,不初始化这个对象(延时加载,真正用的时候再创建)
2119
private static ScanGunManager instance;
2220

2321
// 构造器私有化
2422
private ScanGunManager(){}
2523

26-
// 方法同步,调用效率低
24+
// 限制线程同时访问 jvm 中该类的所有实例同时访问对应的代码块
2725
public static synchronized ScanGunManager getInstance(){
28-
if(instance==null){
26+
if (instance == null) {
2927
instance=new ScanGunManager();
30-
instance.mStringBufferResult = new StringBuffer();
28+
instance.stringBufferResult = new StringBuffer();
3129
}
3230
return instance;
3331
}
3432

35-
3633
public void setReceiveCallback(ScanCodeCallback scanCodeCallback) {
3734
this.scanCodeCallback = scanCodeCallback;
3835
}
3936

4037
public void clearScanBarCodeText(){
41-
if (mStringBufferResult!= null){
42-
mStringBufferResult.delete(0,mStringBufferResult.length());
38+
if (stringBufferResult != null){
39+
stringBufferResult.delete(0, stringBufferResult.length());
4340
}
4441
}
4542

4643
// 外设事件解析
4744
public void analysisKeyEvent(KeyEvent event) {
4845
int keyCode = event.getKeyCode();
49-
String devicesName = event.getDevice().getName();
50-
Log.d(TAG, "设备名称:" + devicesName);
51-
// 字母大小写判断
52-
checkLetterStatus(event);
53-
if (event.getAction() == KeyEvent.ACTION_DOWN) {
54-
char aChar = getInputCode(event);;
55-
if (aChar != 0) {
56-
if (mStringBufferResult!=null){
57-
mStringBufferResult.append(aChar);
58-
}
59-
}
46+
boolean isShiftPressed = event.isShiftPressed();
47+
// 对按下的键进行操作,过滤 shift
48+
if (
49+
event.getAction() == KeyEvent.ACTION_DOWN
50+
&& keyCode != KeyEvent.META_SHIFT_ON
51+
&& keyCode != KeyEvent.KEYCODE_SHIFT_LEFT
52+
&& keyCode != KeyEvent.KEYCODE_SHIFT_RIGHT
53+
) {
6054
if (keyCode == KeyEvent.KEYCODE_ENTER) {
61-
String barCode = mStringBufferResult.toString();
55+
// 输入结束,返回字符串
56+
String barCode = stringBufferResult.toString();
6257
if (scanCodeCallback != null) {
6358
scanCodeCallback.onScanCode(barCode);
6459
}
65-
mStringBufferResult.delete(0,mStringBufferResult.length());
60+
stringBufferResult.delete(0, stringBufferResult.length());
61+
} else {
62+
// 处理输入的字符串
63+
Log.d(TAG, "event:" + event + "\nkeyCode:" + event.getKeyCode() + "\nisShiftPressed:" + event.isShiftPressed());
64+
char inputCode = getInputCode(keyCode, isShiftPressed);;
65+
if (inputCode != 0) {
66+
if (stringBufferResult != null){
67+
stringBufferResult.append(inputCode);
68+
}
69+
}
6670
}
6771
}
6872
}
6973

70-
//检查shift键
71-
private void checkLetterStatus(KeyEvent event) {
72-
int keyCode = event.getKeyCode();
73-
if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
74-
mCaps = event.getAction() == KeyEvent.ACTION_DOWN;
75-
}
76-
}
77-
78-
7974
//获取扫描内容
80-
private char getInputCode(KeyEvent event) {
81-
int keyCode = event.getKeyCode();
82-
char aChar;
75+
private char getInputCode(int keyCode, boolean isShiftPressed) {
76+
char inputCode;
8377

8478
if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
85-
//字母
86-
aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
87-
} else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
88-
//数字
89-
aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
79+
// 大小写字母
80+
inputCode = (char) ((isShiftPressed ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
9081
} else {
91-
//其他符号
92-
aChar = keyValue(mCaps, keyCode);
82+
// 数字及其他符号(限英文字符)
83+
inputCode = keyValue(keyCode, isShiftPressed);
9384
}
94-
return aChar;
85+
Log.d(TAG, "result keyCode:" + inputCode + "keyCode=" +keyCode);
86+
87+
return inputCode;
9588
}
9689

9790
/**
9891
* 按键对应的char表
9992
*/
100-
private char keyValue(boolean caps, int keyCode) {
93+
private char keyValue(int keyCode, boolean isShiftPressed) {
10194
switch (keyCode) {
10295
case KeyEvent.KEYCODE_0:
103-
return caps ? ')' : '0';
96+
return isShiftPressed ? ')' : '0';
10497
case KeyEvent.KEYCODE_1:
105-
return caps ? '!' : '1';
98+
return isShiftPressed ? '!' : '1';
10699
case KeyEvent.KEYCODE_2:
107-
return caps ? '@' : '2';
100+
return isShiftPressed ? '@' : '2';
108101
case KeyEvent.KEYCODE_3:
109-
return caps ? '#' : '3';
102+
return isShiftPressed ? '#' : '3';
110103
case KeyEvent.KEYCODE_4:
111-
return caps ? '$' : '4';
104+
return isShiftPressed ? '$' : '4';
112105
case KeyEvent.KEYCODE_5:
113-
return caps ? '%' : '5';
106+
return isShiftPressed ? '%' : '5';
114107
case KeyEvent.KEYCODE_6:
115-
return caps ? '^' : '6';
108+
return isShiftPressed ? '^' : '6';
116109
case KeyEvent.KEYCODE_7:
117-
return caps ? '&' : '7';
110+
return isShiftPressed ? '&' : '7';
118111
case KeyEvent.KEYCODE_8:
119-
return caps ? '*' : '8';
112+
return isShiftPressed ? '*' : '8';
120113
case KeyEvent.KEYCODE_9:
121-
return caps ? '(' : '9';
114+
return isShiftPressed ? '(' : '9';
122115
case KeyEvent.KEYCODE_NUMPAD_SUBTRACT:
123-
return '-';
116+
return isShiftPressed ? '_' : '-';
124117
case KeyEvent.KEYCODE_MINUS:
125-
return '_';
118+
return isShiftPressed ? '_' : '-';
126119
case KeyEvent.KEYCODE_EQUALS:
127-
return '=';
120+
return isShiftPressed ? '+' : '=';
128121
case KeyEvent.KEYCODE_NUMPAD_ADD:
129-
return '+';
122+
return isShiftPressed ? '+' : '=';
130123
case KeyEvent.KEYCODE_GRAVE:
131-
return caps ? '~' : '`';
124+
return isShiftPressed ? '~' : '`';
132125
case KeyEvent.KEYCODE_BACKSLASH:
133-
return caps ? '|' : '\\';
126+
return isShiftPressed ? '|' : '\\';
134127
case KeyEvent.KEYCODE_LEFT_BRACKET:
135-
return caps ? '{' : '[';
128+
return isShiftPressed ? '{' : '[';
136129
case KeyEvent.KEYCODE_RIGHT_BRACKET:
137-
return caps ? '}' : ']';
130+
return isShiftPressed ? '}' : ']';
138131
case KeyEvent.KEYCODE_SEMICOLON:
139-
return caps ? ':' : ';';
132+
return isShiftPressed ? ':' : ';';
140133
case KeyEvent.KEYCODE_APOSTROPHE:
141-
return caps ? '"' : '\'';
134+
return isShiftPressed ? '"' : '\'';
142135
case KeyEvent.KEYCODE_COMMA:
143-
return caps ? '<' : ',';
136+
return isShiftPressed ? '<' : ',';
144137
case KeyEvent.KEYCODE_PERIOD:
145-
return caps ? '>' : '.';
138+
return isShiftPressed ? '>' : '.';
146139
case KeyEvent.KEYCODE_SLASH:
147-
return caps ? '?' : '/';
140+
return isShiftPressed ? '?' : '/';
141+
case KeyEvent.KEYCODE_SPACE:
142+
return ' ';
148143
default:
144+
Log.d(TAG, "键码为" + keyCode + "的按键未做处理或无法识别");
149145
return 0;
150146
}
151147
}

android/src/main/java/com/afei/scangun/ScanGunModule.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@
1212
import java.util.Map;
1313

1414
public class ScanGunModule extends ReactContextBaseJavaModule {
15-
1615
private final ReactApplicationContext reactContext;
17-
private static final String onScanCodeRecevieData = "onScanCodeRecevieData";
18-
16+
private static final String onScanCodeReceiveData = "onScanCodeReceiveData";
1917

2018
public ScanGunModule(ReactApplicationContext reactContext) {
21-
2219
super(reactContext);
2320
this.reactContext = reactContext;
2421
ScanGunManager.getInstance().setReceiveCallback(new ScanCodeCallback() {
2522
@Override
2623
public void onScanCode(String value) {
27-
ScanGunModule.this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(onScanCodeRecevieData,value);
24+
ScanGunModule.this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(onScanCodeReceiveData,value);
2825
}
2926
});
30-
3127
}
3228

3329
@Override
@@ -44,8 +40,7 @@ public void clearScanBarCodeText(){
4440
@Override
4541
public Map<String, Object> getConstants() {
4642
final Map<String, Object> constants = new HashMap<>();
47-
constants.put("onScanCodeRecevieData", onScanCodeRecevieData);
43+
constants.put("onScanCodeReceiveData", onScanCodeReceiveData);
4844
return constants;
4945
}
50-
5146
}

example/ScanGunExample/.expo-shared/assets.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

example/ScanGunExample/App.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function App() {
1111
useEffect(() => {
1212
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
1313
const eventListener = eventEmitter.addListener(
14-
scanGun.onScanCodeRecevieData,
14+
scanGun.onScanCodeReceiveData,
1515
code => {
1616
setScanCode(code);
1717
},
@@ -22,9 +22,9 @@ export default function App() {
2222
}, []);
2323
return (
2424
<View style={styles.container}>
25-
<Text style={styles.label}>本次扫码结果为:</Text>
26-
<Text style={styles.codeText}>{scanCode}</Text>
2725
<StatusBar style="auto" />
26+
<Text style={styles.label}>本次扫码结果为:</Text>
27+
<Text android_hyphenationFrequency="full" style={styles.codeText}>{scanCode}</Text>
2828
</View>
2929
);
3030
}
@@ -42,6 +42,6 @@ const styles = StyleSheet.create({
4242
},
4343
codeText: {
4444
fontWeight: 'bold',
45-
color: '#108EE9'
45+
color: '#108EE9',
4646
}
4747
});

example/ScanGunExample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"react": "16.13.1",
1616
"react-dom": "16.13.1",
1717
"react-native": "~0.63.4",
18-
"react-native-external-scan-gun": "^1.0.1",
18+
"react-native-external-scan-gun": "^2.0.0",
1919
"react-native-gesture-handler": "~1.10.2",
2020
"react-native-reanimated": "~2.2.0",
2121
"react-native-screens": "~3.4.0",

example/ScanGunExample/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4848,10 +4848,10 @@ react-is@^17.0.1:
48484848
resolved "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
48494849
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
48504850

4851-
react-native-external-scan-gun@^1.0.1:
4852-
version "1.0.1"
4853-
resolved "https://registry.npmjs.org/react-native-external-scan-gun/-/react-native-external-scan-gun-1.0.1.tgz#a88a30ad04818c0a744fe431526e8545e3cdeea7"
4854-
integrity sha512-GG3dJdFdZNLwpFKfXPii9TJWP/sKUAFlvzRsV9+0OzR5qFOy5sZuBWx1w0LzLOqMRQxTIW1JrM0s2OVNEfid2A==
4851+
react-native-external-scan-gun@^2.0.0:
4852+
version "2.0.0"
4853+
resolved "https://registry.npmjs.org/react-native-external-scan-gun/-/react-native-external-scan-gun-2.0.0.tgz#73da03b218fedec54b6e3485cabd697b67a6b9d3"
4854+
integrity sha512-GaHjPRNnJjwcDSLHbb0bRAQsttkKZHPtKU7rfZiitR4v0Pt+BcWa5psxtrBAO6fOZJ80Ke96aV7ZFJxij8XVLg==
48554855

48564856
react-native-gesture-handler@~1.10.2:
48574857
version "1.10.3"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-external-scan-gun",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "基于 react-native 开发的安卓系统外接设备插件",
55
"main": "index.js",
66
"keywords": [

0 commit comments

Comments
 (0)