jasperlow

Untitled

Jan 14th, 2020
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.scsoft;
  2.  
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5.  
  6. import com.scsoft.libecho5.BarcodeScanner;
  7. import com.scsoft.libecho5.Gpio;
  8. import com.scsoft.module.base.ActivityBaseAppCompat;
  9.  
  10. import java.io.FileDescriptor;
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. import io.reactivex.Observable;
  14. import io.reactivex.ObservableSource;
  15. import io.reactivex.functions.Function;
  16. import io.reactivex.schedulers.Schedulers;
  17.  
  18. public class ActivityEntry extends ActivityBaseAppCompat {
  19.  
  20. FileDescriptor fileDescriptor;
  21. Gpio gpio;
  22. BarcodeScanner scanner;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. // startActivity(new Intent(this, ActivitySplash.class));
  28. // finish();
  29. setContentView(R.layout.activity_splash);
  30. gpio = new Gpio();
  31. scanner = new BarcodeScanner();
  32. if (gpio.BarcodeScanner_Enable() == 0) {
  33. showToastShort("BarcodeScanner_Enable");
  34. }
  35. fileDescriptor = scanner.BarcodeScanner_OpenPort(9600);
  36. if (fileDescriptor != null) {
  37. showToastShort("BarcodeScanner_OpenPort");
  38. }
  39.  
  40. addDisposables(
  41. Observable.interval(500, TimeUnit.MILLISECONDS)
  42. .subscribeOn(Schedulers.io())
  43. .flatMap((Function<Long, ObservableSource<String>>)
  44. aLong -> Observable.just(readBarcode()))
  45. .subscribe(barcode -> {
  46. if (!TextUtils.isEmpty(barcode)) showToastShort(barcode);
  47. }, Throwable::printStackTrace, () -> {
  48. }));
  49.  
  50. }
  51.  
  52. public String readBarcode() {
  53. triggerTRIGPin(true);
  54. final StringBuilder strRead = new StringBuilder();
  55. int bytesRead = 0;
  56. do {
  57. final byte[] buf = new byte[2048];
  58. bytesRead = scanner.BarcodeScanner_ReadData(fileDescriptor, buf, buf.length, 10 * 1000);
  59. if ((bytesRead > 0)) strRead.append(new String(buf));
  60. } while (bytesRead > 0);
  61. triggerTRIGPin(false);
  62. return removeEscape(strRead.toString());
  63. }
  64.  
  65. private void triggerTRIGPin(boolean enabled) {
  66. scanner.BarcodeScanner_Trigger(!enabled);
  67. }
  68.  
  69. private String removeEscape(final String str) {
  70. return str.replace("\r\n\u0000", "")
  71. .replaceAll("\u0000", "")
  72. .replaceAll("\r", "")
  73. .replaceAll("\n", "")
  74. .replaceAll("\b", "")
  75. .replace("\\000026", "");
  76. }
  77.  
  78. }
RAW Paste Data