最进接串口扫码器,参考 github上开源的串口通讯库https://github.com/cepr/android-serialport-api实现扫码器通讯。

1.集成

Android Studio使用 cmake编译,将SerialPort.c/SerialPort.h两个文件拷贝到 cpp 文件夹下,SerialPort.java拷贝到 android.serialport包下,记得包名要和SerialPort.h中的方法名中的包名一直。了解NDK开发的你懂得。

CMakeLists.txt

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.cmake_minimum_required(VERSION 3.4.1)# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.add_library( # Specifies the name of the library.serial_port# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).src/main/cpp/SerialPort.c)find_library( # Sets the name of the path variable.log-lib# Specifies the name of the NDK library that# you want CMake to locate.log)# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.serial_port# Links the target library to the log library# included in the NDK.${log-lib})
public class SerialPort {private static final String TAG = "SerialPort";private static final String DEFAULT_SU_PATH = "/system/bin/su";private static String sSuPath = DEFAULT_SU_PATH;static {System.loadLibrary("serial_port");}/** Do not remove or rename the field mFd: it is used by native method close();*/private FileDescriptor mFd;private FileInputStream mFileInputStream;private FileOutputStream mFileOutputStream;public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {/* Check access permission */if (!device.canRead() || !device.canWrite()) {try {/* Missing read/write permission, trying to chmod the file */Process su;su = Runtime.getRuntime().exec(sSuPath);String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";su.getOutputStream().write(cmd.getBytes());if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {throw new SecurityException();}} catch (Exception e) {e.printStackTrace();throw new SecurityException();}}mFd = open(device.getAbsolutePath(), baudrate, flags);if (mFd == null) {Log.e(TAG, "native open returns null");throw new IOException();}mFileInputStream = new FileInputStream(mFd);mFileOutputStream = new FileOutputStream(mFd);}public SerialPort(String devicePath, int baudrate, int flags)throws SecurityException, IOException {this(new File(devicePath), baudrate, flags);}public SerialPort(File device, int baudrate) throws SecurityException, IOException {this(device, baudrate, 0);}public SerialPort(String devicePath, int baudrate) throws SecurityException, IOException {this(new File(devicePath), baudrate, 0);}/*** Set the su binary path, the default su binary path is {@link #DEFAULT_SU_PATH}** @param suPath su binary path*/public static void setSuPath(String suPath) {if (suPath == null) {return;}sSuPath = suPath;}// JNIprivate native static FileDescriptor open(String path, int baudrate, int flags);// Getters and setterspublic InputStream getInputStream() {return mFileInputStream;}public OutputStream getOutputStream() {return mFileOutputStream;}public native void close();
}

2.使用

public SerialPort getSerialPort()throws SecurityException, IOException, InvalidParameterException {if (mSerialPort == null) {//串口:/dev/ttyS4,波特率:115200mSerialPort = new SerialPort(new File("/dev/ttyS4"), 115200);}return mSerialPort;}public void closeSerialPort() {if (mSerialPort != null) {mSerialPort.close();mSerialPort = null;}}private void openSerialPort() {try {mSerialPort =getSerialPort();mInputStream = mSerialPort.getInputStream();mReadThread = new ReadThread();mReadThread.start();} catch (SecurityException e) {e.printStackTrace();ToastCompat.makeText(this, R.string.error_security, Toast.LENGTH_SHORT).show();} catch (IOException e) {e.printStackTrace();ToastCompat.makeText(this, R.string.error_unknown, Toast.LENGTH_SHORT).show();} catch (InvalidParameterException e) {e.printStackTrace();ToastCompat.makeText(this, R.string.error_configuration, Toast.LENGTH_SHORT).show();}}private class ReadThread extends Thread {public ReadThread() {}@Overridepublic void run() {super.run();while (!isInterrupted()) {int size;try {byte[] buffer = new byte[64];if (mInputStream == null) return;size = mInputStream.read(buffer);if (size > 0) {String goodsCode = new String(buffer, 0, size);EventBus.getDefault().post(new GoodsMsgEvent(goodsCode));}} catch (IOException e) {e.printStackTrace();return;}}}}

Android 扫码器串口通讯相关推荐

  1. C# 扫码器串口对接

    我们公司写web的系统,要嵌入到别人的桌面系统中,现在有业务要做扫码功能. 在某宝上找了一个支持二次开发,并且是usb的(有串口的直接对接,但是一般电脑都没有这个接口放弃,找了一个usb的厂家) 该厂 ...

  2. 三菱PLC与扫码器通讯控制PLC工作

    实现目标: 1.通过扫码器,扫码到的二维码或者一维码 ,控制PLC的输出(对PLC的输出进行控制),本案例中通过扫描二维码控制Y0-Y3的点亮 2.熟悉232串口通讯RS通讯控制模式 硬件配置: 1. ...

  3. Android 串口开发,发送串口命令,读卡,反扫码,USB通讯,实现demo。——持续更新

    应用场景:APP发送串口命令到打印机,打印相应数据小票 // 串口 implementation 'com.github.licheedev.Android-SerialPort-API:serial ...

  4. Android 连接蓝牙扫码器 无输入框

    Android 的APP 需要集成一个蓝牙扫码器, 特别的是,需要扫码的地方是没有输入框的(EditText),不能通过直觉上理解的通过对EditText输入事件进行监听处理,取得扫码结果.并且设备也 ...

  5. 扫码器:壹码通(EMT 6621)二维码带多个回车换行处理

    摘要:二维码运用越来越广泛了,目前在医院中一个二维码可以串联多个系统,二维码的内容也可以设置一些特殊字符去达成系统便捷性.本次遇到为二维码中开头内置了回车和空格,在程序判断为回车(KEY_ENTER) ...

  6. 虹科HK-NT 50网关让扫码器轻松连接工业PLC

    扫码器或扫码枪是一种读取条形码或二维码信息的机器.它利用发射出红外线光源,然后根据反射的结果,利用芯片来译码,最后再返回条形码或二维码所代表的正确字符.传统的扫码器主要应用在快递公司\仓储物流\仓库盘 ...

  7. 商品扫码器驱动如何设置 商品扫码器驱动设置步骤

    商品扫码器驱动如何设置?商品扫码器驱动设置步骤,商品扫码器一般分为三种接口,分别为USB接口.串口和键盘口.只需要选择适合使用的接口,也就是说电脑或其他设备上有什么接口,就选择什么接口就对了,就大多数 ...

  8. C#光功率计_扫码器导入excel

    公司要求扫码器读入编码,然后光功率计取出数值,等完成后把两个数据导入excel 用了将近一个月 时间  以前在学校一直想学一款上位机的,但是C#,LABVIEW,QT,VC,deliphi 选择太多了 ...

  9. C# 控制新大陆扫码器自动扫码读取序列号

    在开始代码前先要准备好两件事: 首先,要安装新大陆的相应驱动,使得COM口能识别到扫码器. 其次,将扫码器切换到代码控制的自动扫码模式.这个找厂家要一下就行,他们会给你一串码,用扫码器扫一下这些码就能 ...

最新文章

  1. 移动端滚动穿透问题完美解决方案
  2. GoogLeNet(从Inception v1到v4的演进)
  3. Python hashlib 无法打印
  4. CTFshow php特性 web145
  5. Qt修炼手册8_常用的容器类QVector和QList
  6. snb处理器hd3000显卡专用extra_一文看懂显卡的GPU,流处理器,显存,位宽,核心频率是啥?...
  7. HTML5调用手机前置摄像头或后置摄像头拍照,canvas显示,经过Android测试
  8. matlab 实现 stacked Autoencoder 解决图像分类问题
  9. android通知图标变白色,android 7.0通知图标出现白色方块
  10. 使用docker-compose的Spring会话演示
  11. 基础知识很扎实 - 但是面试就是做不出来, 怎么办? (长, 慎入)
  12. 荷兰国旗问题(分三块)
  13. nodejs-express
  14. mysql 镜像备份_手动构建percona-xtrabackup Docker镜像,并实现mysql数据备份
  15. SQLi LABS Less-36
  16. 网络与信息安全基础知识专栏
  17. SSM框架原理流程及使用方法
  18. SE11字段过多时怎么快速建表
  19. JEECMS——源码下载和安转运行
  20. SCAR:Scalable Consensus Algorithm一种可伸缩共识算法

热门文章

  1. 码农接单神器!超赚钱的副业来了
  2. 两种方式完成批处理的优缺点
  3. 取消远程计算机控制,[远程关闭局域网电脑的方法]局域网内远程控制电脑
  4. 前端渲染CSR和SSR的结合使用分析
  5. instagram akp_如何备份您的社交媒体帐户-Facebook,Twitter,Google +和Instagram
  6. instagram akp_如何阻止特定的人看到您的Instagram故事
  7. 十大事件与Java相关的四十个名字
  8. 【SVF Cloud】项目值过长,分2行表示
  9. iMindMap该怎样用来画思维导图
  10. Hadoop高可用测试失败,standy无法切换成active namenode,Unable to trigger a roll of the active NN