Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.scsoft;
- import android.os.Bundle;
- import android.text.TextUtils;
- import com.scsoft.libecho5.BarcodeScanner;
- import com.scsoft.libecho5.Gpio;
- import com.scsoft.module.base.ActivityBaseAppCompat;
- import java.io.FileDescriptor;
- import java.util.concurrent.TimeUnit;
- import io.reactivex.Observable;
- import io.reactivex.ObservableSource;
- import io.reactivex.functions.Function;
- import io.reactivex.schedulers.Schedulers;
- public class ActivityEntry extends ActivityBaseAppCompat {
- FileDescriptor fileDescriptor;
- Gpio gpio;
- BarcodeScanner scanner;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // startActivity(new Intent(this, ActivitySplash.class));
- // finish();
- setContentView(R.layout.activity_splash);
- gpio = new Gpio();
- scanner = new BarcodeScanner();
- if (gpio.BarcodeScanner_Enable() == 0) {
- showToastShort("BarcodeScanner_Enable");
- }
- fileDescriptor = scanner.BarcodeScanner_OpenPort(9600);
- if (fileDescriptor != null) {
- showToastShort("BarcodeScanner_OpenPort");
- }
- addDisposables(
- Observable.interval(500, TimeUnit.MILLISECONDS)
- .subscribeOn(Schedulers.io())
- .flatMap((Function<Long, ObservableSource<String>>)
- aLong -> Observable.just(readBarcode()))
- .subscribe(barcode -> {
- if (!TextUtils.isEmpty(barcode)) showToastShort(barcode);
- }, Throwable::printStackTrace, () -> {
- }));
- }
- public String readBarcode() {
- triggerTRIGPin(true);
- final StringBuilder strRead = new StringBuilder();
- int bytesRead = 0;
- do {
- final byte[] buf = new byte[2048];
- bytesRead = scanner.BarcodeScanner_ReadData(fileDescriptor, buf, buf.length, 10 * 1000);
- if ((bytesRead > 0)) strRead.append(new String(buf));
- } while (bytesRead > 0);
- triggerTRIGPin(false);
- return removeEscape(strRead.toString());
- }
- private void triggerTRIGPin(boolean enabled) {
- scanner.BarcodeScanner_Trigger(!enabled);
- }
- private String removeEscape(final String str) {
- return str.replace("\r\n\u0000", "")
- .replaceAll("\u0000", "")
- .replaceAll("\r", "")
- .replaceAll("\n", "")
- .replaceAll("\b", "")
- .replace("\\000026", "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement