初始化框架,完成扫描回调,语音接口
This commit is contained in:
118
app/src/main/java/chaoran/business/adapter/RockChipAdapter.java
Normal file
118
app/src/main/java/chaoran/business/adapter/RockChipAdapter.java
Normal file
@ -0,0 +1,118 @@
|
||||
package chaoran.business.adapter;
|
||||
|
||||
/*
|
||||
**********************************************
|
||||
* DATE PERSON REASON
|
||||
* 2021-02-01 FXY Created
|
||||
**********************************************
|
||||
*/
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.media.AudioManager;
|
||||
import android.media.SoundPool;
|
||||
import android.os.Vibrator;
|
||||
import chaoran.business.R;
|
||||
import chaoran.business.activity.ResultListener;
|
||||
import chaoran.business.strategy.Strategy;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* 瑞芯适配器
|
||||
*/
|
||||
public class RockChipAdapter implements Adapter {
|
||||
|
||||
private Strategy strategy;
|
||||
|
||||
private ResultListener resultListener;
|
||||
|
||||
private Context context;
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
strategy.executeStrategy(resultListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
strategy.exclusiveStrategy();
|
||||
}
|
||||
|
||||
public RockChipAdapter(Context context, ResultListener resultListener) {
|
||||
this.resultListener = resultListener;
|
||||
this.context = context;
|
||||
this.strategy = new Reader(new File("/dev/ttyS1"), 115200, 0);
|
||||
}
|
||||
|
||||
public class Reader implements Strategy {
|
||||
private FileDescriptor mFd;
|
||||
private FileInputStream mFileInputStream;
|
||||
private boolean running = true;
|
||||
|
||||
public Reader(File device, int baudrate, int flags) {
|
||||
|
||||
if (!device.canRead() || !device.canWrite()) {
|
||||
try {
|
||||
Process su;
|
||||
su = Runtime.getRuntime().exec("/system/bin/su");
|
||||
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";
|
||||
su.getOutputStream().write(cmd.getBytes());
|
||||
|
||||
if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
|
||||
System.out.println("获取su命令权限失败,系统或许未root!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("获取root权限失败");
|
||||
}
|
||||
}
|
||||
mFd = open(device.getAbsolutePath(), baudrate, flags);
|
||||
if (mFd == null) {
|
||||
System.out.println("获取文件描述符失败!");
|
||||
}
|
||||
mFileInputStream = new FileInputStream(mFd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeStrategy(ResultListener resultListener) {
|
||||
new Thread(() -> {
|
||||
while (running) {
|
||||
String data = data();
|
||||
if (!data.equals("")) {
|
||||
resultListener.result(data);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exclusiveStrategy() {
|
||||
running = false;
|
||||
close();
|
||||
}
|
||||
|
||||
private String data() {
|
||||
if (mFileInputStream == null) return "";
|
||||
try {
|
||||
int size = mFileInputStream.available();
|
||||
byte[] buffer = new byte[size];
|
||||
size = mFileInputStream.read(buffer);
|
||||
if (size > 0) {
|
||||
return new String(buffer);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
System.loadLibrary("rockchip");
|
||||
}
|
||||
|
||||
private native static FileDescriptor open(String path, int baudrate, int flags);
|
||||
|
||||
public native void close();
|
||||
}
|
||||
Reference in New Issue
Block a user