Advertisement
jasperlow

Untitled

Jan 14th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. package com.scsoft;
  2.  
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.text.TextUtils;
  6.  
  7. import com.scsoft.libecho5.BarcodeScanner;
  8. import com.scsoft.libecho5.Gpio;
  9. import com.scsoft.module.base.ActivityBaseAppCompat;
  10.  
  11. import java.io.FileDescriptor;
  12.  
  13. public class ActivityEntry extends ActivityBaseAppCompat {
  14.  
  15. FileDescriptor fileDescriptor;
  16. Gpio gpio;
  17. BarcodeScanner scanner;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. // startActivity(new Intent(this, ActivitySplash.class));
  23. // finish();
  24. setContentView(R.layout.activity_splash);
  25. gpio = new Gpio();
  26. scanner = new BarcodeScanner();
  27. if (gpio.BarcodeScanner_Enable() == 0) {
  28. showToastShort("BarcodeScanner_Enable");
  29. }
  30. fileDescriptor = scanner.BarcodeScanner_OpenPort(9600);
  31. if (fileDescriptor != null) {
  32. showToastShort("BarcodeScanner_OpenPort");
  33. }
  34. new IntervalRead().start();
  35. }
  36.  
  37. public class IntervalRead extends CountDownTimer {
  38.  
  39. public IntervalRead() {
  40. super(5000, 500);
  41. }
  42.  
  43. @Override
  44. public void onTick(long millisUntilFinished) {
  45. String barcode = readBarcode();
  46. if (!TextUtils.isEmpty(barcode)) showToastShort(barcode);
  47. }
  48.  
  49. @Override
  50. public void onFinish() {
  51.  
  52. }
  53. }
  54.  
  55. public String readBarcode() {
  56. triggerTRIGPin(true);
  57. final StringBuilder strRead = new StringBuilder();
  58. int bytesRead = 0;
  59. do {
  60. final byte[] buf = new byte[2048];
  61. bytesRead = scanner.BarcodeScanner_ReadData(fileDescriptor, buf, buf.length, 10 * 1000);
  62. if ((bytesRead > 0)) strRead.append(new String(buf));
  63. } while (bytesRead > 0);
  64. triggerTRIGPin(false);
  65. return removeEscape(strRead.toString());
  66. }
  67.  
  68. private void triggerTRIGPin(boolean enabled) {
  69. scanner.BarcodeScanner_Trigger(!enabled);
  70. }
  71.  
  72. private String removeEscape(final String str) {
  73. return str.replace("\r\n\u0000", "")
  74. .replaceAll("\u0000", "")
  75. .replaceAll("\r", "")
  76. .replaceAll("\n", "")
  77. .replaceAll("\b", "")
  78. .replace("\\000026", "");
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement