应用场景:APP发送串口命令到打印机,打印相应数据小票
// 串口
implementation 'com.github.licheedev.Android-SerialPort-API:serialport:1.0.1'
1、获取全部串口地址devicePath
private String[] mDevices;
public void getcuankou(){SerialPortFinder serialPortFinder = new SerialPortFinder();mDevices = serialPortFinder.getAllDevicesPath();for (int c = 0;c<mDevices.length;c++){Log.i("lgq","...... "+mDevices[c]);}
}
2、连接串口,以及关闭串口
/*** 打开串口** @param devicePath* @param baudrateString* @return*/
public SerialPort open(String devicePath, String baudrateString) {if (mSerialPort != null) {close();}try {File device = new File(devicePath);int baurate = Integer.parseInt(baudrateString);mSerialPort = new SerialPort(device, baurate, 0);mReadThread = new SerialReadThread(mSerialPort.getInputStream());mReadThread.start();mOutputStream = mSerialPort.getOutputStream();mWriteThread = new HandlerThread("write-thread");mWriteThread.start();mSendScheduler = AndroidSchedulers.from(mWriteThread.getLooper());return mSerialPort;} catch (Throwable tr) {LogPlus.e(TAG, "打开串口失败", tr);close();return null;}
}/*** 关闭串口*/
public void close() {if (mReadThread != null) {mReadThread.close();}if (mOutputStream != null) {try {mOutputStream.close();} catch (IOException e) {e.printStackTrace();}}if (mWriteThread != null) {mWriteThread.quit();}if (mSerialPort != null) {mSerialPort.close();mSerialPort = null;}
}
Android接收读卡器以及扫码器返回数据实现方法
工具类
public class CardSwipingHelper {private static final String TAG = CardSwipingHelper.class.getSimpleName();private static class SingletonHolder {private static final CardSwipingHelper INSTANCE = new CardSwipingHelper();}private CardSwipingHelper() {}public static final CardSwipingHelper getInstance() {return SingletonHolder.INSTANCE;}private StringBuffer stringBuffer=null ;public boolean dispatchKeyEvent(KeyEvent event) {if (event.getAction() == KeyEvent.ACTION_DOWN) {char pressedKey = (char) event.getUnicodeChar();if(stringBuffer==null){stringBuffer=new StringBuffer();}if (event.getUnicodeChar()==0){return false;}stringBuffer.append(pressedKey);}if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {if (mCardSwipingCallBack != null) {mCardSwipingCallBack.enterCardSwipingCont(replaceBlank(stringBuffer.toString()));}stringBuffer = null;return true;}return false;}private CardSwipingCallBack mCardSwipingCallBack;public void setCardSwipingCallBack(CardSwipingCallBack cardSwipingCallBack) {mCardSwipingCallBack = cardSwipingCallBack;}public interface CardSwipingCallBack {void enterCardSwipingCont(String cont);}public static String replaceBlank(String src) {String dest = "";if (src != null) {Pattern pattern = Pattern.compile("\t|\r|\n|\\s*");Matcher matcher = pattern.matcher(src);dest = matcher.replaceAll("");}return dest;}
}
回调
private CardSwipingHelper.CardSwipingCallBack mCardSwipingCallBack = new CardSwipingHelper.CardSwipingCallBack() {@Overridepublic void enterCardSwipingCont(String cont) {try {String str2 = new String(cont.getBytes("utf-8"), "gbk");LgqLogutil.e("刷卡返回===== "+cont+"...."+str2);tologin(cont);//刷卡登录}catch (Exception e){}}
};//拦截刷卡的情况http://weixin.qq.com/r/Ti3s9NXE2oilrZGX93hchttp://weixin.qq.com/r/Ti3s9NXE2oilrZGX93hc
@Override
public boolean dispatchKeyEvent(KeyEvent event) {if (CardSwipingHelper.getInstance().dispatchKeyEvent(event)) {return true;}return super.dispatchKeyEvent(event);
}
CardSwipingHelper.getInstance().setCardSwipingCallBack(mCardSwipingCallBack);//扫码回调
注意:
EditText回拦截回调
USB连接
action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter"