diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b94aa26..deccba8 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,12 @@ + + + + + + diff --git a/app/build.gradle b/app/build.gradle index 85c6167..3b97bc1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,5 +42,5 @@ android { dependencies { //加载jar包 - compile fileTree(dir: 'libs', include: ['*.jar']) + implementation fileTree(dir: 'libs', include: ['*.jar']) } \ No newline at end of file diff --git a/app/jni/Android.mk b/app/jni/Android.mk index 1fd95e8..f4ce1c3 100644 --- a/app/jni/Android.mk +++ b/app/jni/Android.mk @@ -18,7 +18,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -TARGET_PLATFORM := android-8 +TARGET_PLATFORM := android-3 LOCAL_MODULE := serial_port LOCAL_SRC_FILES := SerialPort.c LOCAL_LDLIBS := -llog diff --git a/app/jni/Application.mk b/app/jni/Application.mk new file mode 100644 index 0000000..7866257 --- /dev/null +++ b/app/jni/Application.mk @@ -0,0 +1 @@ +APP_ABI := armeabi armeabi-v7a x86 diff --git a/app/jni/SerialPort.c b/app/jni/SerialPort.c index b4926b3..84748a7 100644 --- a/app/jni/SerialPort.c +++ b/app/jni/SerialPort.c @@ -1,5 +1,5 @@ /* - * Copyright 2009 Cedric Priscal + * Copyright 2009-2011 Cedric Priscal * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,52 +22,14 @@ #include #include +#include "SerialPort.h" + #include "android/log.h" static const char *TAG="serial_port"; #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args) #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args) #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args) - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: com_android_barcode_SerialPort - * Method: open - * Signature: (Ljava/lang/String;I)I - */ -JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open - (JNIEnv *, jclass, jstring, jint); - -/* - * Class: com_android_barcode_SerialPort - * Method: write - * Signature: (ILjava/lang/String;I)I - */ -JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_write - (JNIEnv *, jobject, jint, jstring, jint); - -/* - * Class: com_android_barcode_SerialPort - * Method: read - * Signature: (II)[B - */ -JNIEXPORT jbyteArray JNICALL Java_com_android_barcode_SerialPort_read - (JNIEnv *, jobject, jint, jint); - -/* - * Class: com_android_barcode_SerialPort - * Method: close - * Signature: (I)V - */ -JNIEXPORT void JNICALL Java_com_android_barcode_SerialPort_close - (JNIEnv *, jobject, jint); - -#ifdef __cplusplus -} -#endif - static speed_t getBaudrate(jint baudrate) { switch(baudrate) { @@ -107,15 +69,16 @@ static speed_t getBaudrate(jint baudrate) } /* - * Class: cedric_serial_SerialPort + * Class: android_serialport_SerialPort * Method: open - * Signature: (Ljava/lang/String;)V + * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor; */ -JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open - (JNIEnv *env, jobject thiz, jstring path, jint baudrate) +JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open + (JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags) { int fd; speed_t speed; + jobject mFileDescriptor; /* Check arguments */ { @@ -123,6 +86,7 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open if (speed == -1) { /* TODO: throw an exception */ LOGE("Invalid baudrate"); + return NULL; } } @@ -130,8 +94,8 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open { jboolean iscopy; const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy); - LOGD("Opening serial port %s", path_utf); - fd = open(path_utf, O_RDWR | O_SYNC); + LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags); + fd = open(path_utf, O_RDWR | flags); LOGD("open() fd = %d", fd); (*env)->ReleaseStringUTFChars(env, path, path_utf); if (fd == -1) @@ -139,6 +103,7 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open /* Throw an exception */ LOGE("Cannot open port"); /* TODO: throw an exception */ + return NULL; } } @@ -151,136 +116,52 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open LOGE("tcgetattr() failed"); close(fd); /* TODO: throw an exception */ + return NULL; } cfmakeraw(&cfg); cfsetispeed(&cfg, speed); cfsetospeed(&cfg, speed); - /*******************************/ - cfg.c_cflag &= ~CSIZE; - cfg.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG); - cfg.c_oflag &= ~OPOST; - //'8' bit - cfg.c_cflag |= CS8; - //'N' PARENB - cfg.c_cflag &= ~PARENB; - cfg.c_iflag &= ~INPCK; - //'1' STOP - cfg.c_cflag &= ~CSTOPB; - - cfg.c_cc[VTIME] = 15; - cfg.c_cc[VMIN] = 0; - tcflush(fd, TCIFLUSH); - /*********************************/ if (tcsetattr(fd, TCSANOW, &cfg)) { LOGE("tcsetattr() failed"); close(fd); /* TODO: throw an exception */ - } - } - - - return fd; -} -/* - * Class: cedric_serial_SerialPort - * Method: write - * Signature: ()V - */ - -JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_write - (JNIEnv *env, jobject obj, jint fd, jstring str, jint len) -{ - jboolean iscopy; - const char *buff_utf = (*env)->GetStringUTFChars(env, str, &iscopy); - int wlen = 0; - wlen = write(fd, buff_utf, len); - if(wlen > 0) - { - LOGD("Write serial port %s\n", buff_utf); - } - else - { - LOGE("Write failed\n"); - } - (*env)->ReleaseStringUTFChars(env, str, buff_utf); - LOGD("Write finish!\n"); - return wlen; -} -/* - * Class: cedric_serial_SerialPort - * Method: read - * Signature: ()V - */ - -JNIEXPORT jbyteArray JNICALL Java_com_android_barcode_SerialPort_read - (JNIEnv *env, jobject obj, jint fd, jint len) -{ - int reval; - int nread = 0; - char buff[len]; - char result[len]; - struct timeval tv; - jbyteArray jba; - fd_set rfds; - - while(1) - { - FD_ZERO(&rfds); - FD_SET(fd, &rfds); - tv.tv_sec = 0; - tv.tv_usec = 100000; - if ((reval = select(1 + fd, &rfds, NULL, NULL, &tv)) > 0) - { - if(FD_ISSET(fd, &rfds)) - { - int temp = read(fd, buff + nread, len); - nread += temp; - LOGD("%i us is used", 100000 - tv.tv_usec); - LOGD("wo have data %d\n", temp); - } - } - else if(reval == 0 && nread != 0) //timeout means - { -// int ffd; - buff[nread]=0; -// ffd = open("/data/ndk", O_WRONLY | O_TRUNC); -// write(ffd, buff, nread); -// close(ffd); - int i, j; - for(i = 0, j = 0; i < nread; i++) - { - if(buff[i] != 0) - { - result[j] = buff[i]; - j++; - } - // LOGD("0x%2x ", buff[i]); - } - //LOGD("\n"); - //result[j] = 0; - jba = (*env)->NewByteArray(env, j); - (*env)->SetByteArrayRegion(env, jba, 0, j, result); -/* jba = (*env)->NewByteArray(env, nread); - (*env)->SetByteArrayRegion(env, jba, 0, nread, buff);*/ - LOGD("readlength=%d\n, reallength=%d\n, all data received!\n", nread, j); - return jba; - } - else - { return NULL; } } + + /* Create a corresponding file descriptor */ + { + jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor"); + jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "", "()V"); + jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I"); + mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor); + (*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd); + } + + return mFileDescriptor; } + /* * Class: cedric_serial_SerialPort * Method: close * Signature: ()V */ -JNIEXPORT void JNICALL Java_com_android_barcode_SerialPort_close - (JNIEnv *env, jobject obj, jint fd) +JNIEXPORT void JNICALL Java_android_1serialport_1api_SerialPort_close + (JNIEnv *env, jobject thiz) { - close(fd); + jclass SerialPortClass = (*env)->GetObjectClass(env, thiz); + jclass FileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor"); + + jfieldID mFdID = (*env)->GetFieldID(env, SerialPortClass, "mFd", "Ljava/io/FileDescriptor;"); + jfieldID descriptorID = (*env)->GetFieldID(env, FileDescriptorClass, "descriptor", "I"); + + jobject mFd = (*env)->GetObjectField(env, thiz, mFdID); + jint descriptor = (*env)->GetIntField(env, mFd, descriptorID); + + LOGD("close(fd = %d)", descriptor); + close(descriptor); } + diff --git a/app/jni/SerialPort.h b/app/jni/SerialPort.h new file mode 100644 index 0000000..61f1fb2 --- /dev/null +++ b/app/jni/SerialPort.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class android_serialport_api_SerialPort */ + +#ifndef _Included_android_serialport_api_SerialPort +#define _Included_android_serialport_api_SerialPort +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: android_serialport_api_SerialPort + * Method: open + * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor; + */ +JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open + (JNIEnv *, jclass, jstring, jint, jint); + +/* + * Class: android_serialport_api_SerialPort + * Method: close + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_android_1serialport_1api_SerialPort_close + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/app/jni/gen_SerialPort_h.sh b/app/jni/gen_SerialPort_h.sh new file mode 100644 index 0000000..1998186 --- /dev/null +++ b/app/jni/gen_SerialPort_h.sh @@ -0,0 +1,3 @@ +#!/bin/sh +javah -o SerialPort.h -jni -classpath ../src android_serialport_api.SerialPort + diff --git a/app/libs/armeabi/libserial_port.so b/app/libs/armeabi/libserial_port.so index 3ea9b56..d1c3622 100644 Binary files a/app/libs/armeabi/libserial_port.so and b/app/libs/armeabi/libserial_port.so differ diff --git a/app/src/main/java/android_serialport_api/SerialPort.java b/app/src/main/java/android_serialport_api/SerialPort.java index 1721f76..7cafe72 100644 --- a/app/src/main/java/android_serialport_api/SerialPort.java +++ b/app/src/main/java/android_serialport_api/SerialPort.java @@ -1,21 +1,19 @@ package android_serialport_api; -import android.app.Application; import android.util.Log; import com.util.DialogUtil; -import com.util.GlobalApplication; import map.baidu.com.BMapManagerUtil; import java.io.*; -public class SerialPort { +public class SerialPort{ private static final String TAG = "SerialPort"; private FileDescriptor mFd; private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream; - public SerialPort(File device, int baudrate, int bits, char event, int stop, int flags) throws SecurityException, + public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { if (!device.canRead() || !device.canWrite()) { @@ -35,7 +33,7 @@ public class SerialPort { throw new SecurityException(); } } - mFd = open(device.getAbsolutePath(), baudrate, bits, event, stop, flags); + mFd = open(device.getAbsolutePath(), baudrate, flags); Log.i("info", "open device!!"); if (mFd == null) { Log.e(TAG, "native open returns null"); @@ -57,12 +55,12 @@ public class SerialPort { return mFileOutputStream; } - private native static FileDescriptor open(String path, int baudrate, int bits, char event, int stop, int flags); + private native static FileDescriptor open(String path, int baudrate, int flags); public native void close(); static { - System.loadLibrary("serialport"); + System.loadLibrary("serial_port"); } } diff --git a/app/src/main/java/com/device/zk_r322a/ZKR322AControl.java b/app/src/main/java/com/device/zk_r322a/ZKR322AControl.java index 58b9d03..383f21d 100644 --- a/app/src/main/java/com/device/zk_r322a/ZKR322AControl.java +++ b/app/src/main/java/com/device/zk_r322a/ZKR322AControl.java @@ -1,24 +1,18 @@ package com.device.zk_r322a; +import android_serialport_api.SerialPort; + import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; - -import android.gpio.GpioJNI; -import android.util.Log; -import android_serialport_api.SerialPort; public class ZKR322AControl { private SerialPort mSerialPort = null; - private OutputStream mOutputStream = null; private InputStream mInputStream = null; - private boolean readVal = false; public ZKR322AControl() throws SecurityException, IOException { - mSerialPort = new SerialPort(new File("/dev/ttyS4"), 115200, 8, 'N', 1,0); - mOutputStream = mSerialPort.getOutputStream(); - mInputStream = mSerialPort.getInputStream(); + mSerialPort = new SerialPort(new File("/dev/ttyS1"), 115200,0); + mInputStream = mSerialPort.getInputStream(); } public String getData() { @@ -37,17 +31,11 @@ public class ZKR322AControl { } public void start() { - readVal = true; - GpioJNI.gpio_switch_scan_trig(1); +// GpioJNI.gpio_switch_scan_trig(1); } public void stop() { - readVal = false; - GpioJNI.gpio_switch_scan_trig(0); - } - - public boolean isRead() { - return readVal; +// GpioJNI.gpio_switch_scan_trig(0); } public void close() { @@ -58,7 +46,7 @@ public class ZKR322AControl { public void initScan() { - GpioJNI.gpio_switch_scan_rf_ired(0); - GpioJNI.gpio_switch_scan_power(1); +// GpioJNI.gpio_switch_scan_rf_ired(0); +// GpioJNI.gpio_switch_scan_power(1); } } diff --git a/app/src/main/java/com/jiebao/h518/scan/NewScanControler.java b/app/src/main/java/com/jiebao/h518/scan/NewScanControler.java index 0b80745..533615e 100644 --- a/app/src/main/java/com/jiebao/h518/scan/NewScanControler.java +++ b/app/src/main/java/com/jiebao/h518/scan/NewScanControler.java @@ -15,7 +15,7 @@ public class NewScanControler { private InputStream mInputStream; public NewScanControler() throws SecurityException, IOException { - mSerialPort = new SerialPort(new File("/dev/ttySAC1"), 9600, 8, 'N', 1, + mSerialPort = new SerialPort(new File("/dev/ttySAC1"), 9600, 0); mOutputStream = mSerialPort.getOutputStream(); mInputStream = mSerialPort.getInputStream(); diff --git a/app/src/main/java/com/util/GlobalApplication.java b/app/src/main/java/com/util/GlobalApplication.java deleted file mode 100644 index 868790a..0000000 --- a/app/src/main/java/com/util/GlobalApplication.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.util; - -/* - ********************************************** - * DATE PERSON REASON - * 2021-01-15 FXY Created - ********************************************** - */ - - -import android.app.Application; -import android.content.Context; - -/** - * 全局上下文,可以用来弹出无依赖的对话框等 - */ -public class GlobalApplication extends Application { - - private static Context context;//全局上下文 - - @Override - public void onCreate() { - super.onCreate(); - context = getApplicationContext(); - } - - //获取全局的上下文 - public static Context getContext() { - return context; - } -} \ No newline at end of file