以前做的BLE,现在做串口通信,说说自己的经验。
1、首先你要有串口通信代码 ,也就是C/C++的代码,网上有很多android_serialport_api,我也上传一个,方便使用,咦!~ 好像不行,没有办法了 ,只有你们自己下载了。
2、
添加 SerialPort 类
在网上找到 SerialPort 的 Java 类,添加到项目中。
package com.xd.serialport;
import android.util.Log;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SerialPort {
private static final String TAG = "SerialPort";
/*
* 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("/system/bin/su");
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);
}
// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
// JNI
private native static FileDescriptor open(String path, int baudrate, int flags);
public native void close();
static {
System.loadLibrary("serial_port");
}
}
项目结构如下:
生成 c 语言头文件
打开 Terminal(View -> Tool Windows -> Terminal)
输入
cd app\src\main\java
进入源码所在目录输入
javah -jni com.xd.serialport.SerialPort
生成头文件
创建 jni 文件夹,添加 .c 文件
右键 Moudle,右键菜单中选择
New -> Folder -> JNI Folder
,弹出的对话框,直接点击完成。将前面生成的 .h 文件移入 jni 文件夹中。
右键 jni 文件夹,右键菜单中选择
New -> C/C++ Source File
创建与 .h 文件同名的 .c 文件。为 .c 文件添加如下内容(来源于网络)。
注意:需要将include头文件、open、close方法中的包名修改为自己的包名。
#include <com_xd_serialport_SerialPort.h> #include <termios.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <jni.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) /* * Class: android_serialport_SerialPort * Method: open * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor; */ static speed_t getBaudrate(jint baudrate) { switch(baudrate) { case 0: return B0; case 50: return B50; case 75: return B75; case 110: return B110; case 134: return B134; case 150: return B150; case 200: return B200; case 300: return B300; case 600: return B600; case 1200: return B1200; case 1800: return B1800; case 2400: return B2400; case 4800: return B4800; case 9600: return B9600; case 19200: return B19200; case 38400: return B38400; case 57600: return B57600; case 115200: return B115200; case 230400: return B230400; case 460800: return B460800; case 500000: return B500000; case 576000: return B576000; case 921600: return B921600; case 1000000: return B1000000; case 1152000: return B1152000; case 1500000: return B1500000; case 2000000: return B2000000; case 2500000: return B2500000; case 3000000: return B3000000; case 3500000: return B3500000; case 4000000: return B4000000; default: return -1; } } /* * Class: android_serialport_SerialPort * Method: open * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor; */ JNIEXPORT jobject JNICALL Java_com_xd_serialport_SerialPort_open (JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags) { int fd; speed_t speed; jobject mFileDescriptor; /* Check arguments */ { speed = getBaudrate(baudrate); if (speed == -1) { /* TODO: throw an exception */ LOGE("Invalid baudrate"); return NULL; } } /* Opening device */ { jboolean iscopy; const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy); 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) { /* Throw an exception */ LOGE("Cannot open port"); /* TODO: throw an exception */ return NULL; } } /* Configure device */ { struct termios cfg; LOGD("Configuring serial port"); if (tcgetattr(fd, &cfg)) { LOGE("tcgetattr() failed"); close(fd); /* TODO: throw an exception */ return NULL; } cfmakeraw(&cfg); cfsetispeed(&cfg, speed); cfsetospeed(&cfg, speed); if (tcsetattr(fd, TCSANOW, &cfg)) { LOGE("tcsetattr() failed"); close(fd); /* TODO: throw an exception */ return NULL; } } /* Create a corresponding file descriptor */ { jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor"); jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()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_xd_serialport_SerialPort_close (JNIEnv *env, jobject thiz) { 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); }
配置NDK,生成 .so 文件
在 local.properties 添加 ndk 路径
Android-ndk的版本至少要 r9d 以上的版本,如果等于或低于该版本,运行时会提示
No rule to make target
的错误。我使用的是 r10e 的版本。在 module 的 build.gradle 中添加 ndk 配置
在 defaultConfig 节点下添加:
ndk{ abiFilter "armeabi" moduleName "serial_port" // 生成so的名称 ldLibs "log", "z", "m", "jnigraphics", "android" }
在 gradle.properties 中添加
android.useDeprecatedNdk=true
,否则 sync 时会提示错误NDK integration is deprecated in the current plugin.
Sync Project with Gradle Files
运行项目,将在目录
\build\intermediates\ndk\debug\lib\armeabi
下生成libserial_port.so
文件。将 so 文件拷贝到
\src\main\jniLibs\armeabi
目录下。
以上 参考 http://blog.youkuaiyun.com/doris_d/article/details/53330838 请尊重原创
你以为好了?其实没有,之后你可能看到会报错,你就要检查你的.c文件对不对,首先我说的是文件名称,其次就是文件里面各个方法 ,你自己要认真看,不能直接抄的哥们,之后可能还是有问题,因为串口通信是要API19的 你的是不是,反正我的不是,百度,说是改版本号,我悄悄告诉你, 就是在 jni中 加入termiso.h文件,就是这样,代码是:
/* * Copyright (C) 2008 The Android Open Source Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef _TERMIOS_H_ #define _TERMIOS_H_ #include <sys/cdefs.h> #include <sys/ioctl.h> #include <sys/types.h> #include <stdint.h> #include <linux/termios.h> __BEGIN_DECLS /* Redefine these to match their ioctl number */ #undef TCSANOW #define TCSANOW TCSETS #undef TCSADRAIN #define TCSADRAIN TCSETSW #undef TCSAFLUSH #define TCSAFLUSH TCSETSF static __inline__ int tcgetattr(int fd, struct termios *s) { return ioctl(fd, TCGETS, s); } static __inline__ int tcsetattr(int fd, int __opt, const struct termios *s) { return ioctl(fd, __opt, (void *)s); } static __inline__ int tcflow(int fd, int action) { return ioctl(fd, TCXONC, (void *)(intptr_t)action); } static __inline__ int tcflush(int fd, int __queue) { return ioctl(fd, TCFLSH, (void *)(intptr_t)__queue); } static __inline__ pid_t tcgetsid(int fd) { pid_t _pid; return ioctl(fd, TIOCGSID, &_pid) ? (pid_t)-1 : _pid; } static __inline__ int tcsendbreak(int fd, int __duration) { return ioctl(fd, TCSBRKP, (void *)(uintptr_t)__duration); } static __inline__ speed_t cfgetospeed(const struct termios *s) { return (speed_t)(s->c_cflag & CBAUD); } static __inline__ int cfsetospeed(struct termios *s, speed_t speed) { s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); return 0; } static __inline__ speed_t cfgetispeed(const struct termios *s) { return (speed_t)(s->c_cflag & CBAUD); } static __inline__ int cfsetispeed(struct termios *s, speed_t speed) { s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); return 0; } static __inline__ void cfmakeraw(struct termios *s) { s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); s->c_oflag &= ~OPOST; s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); s->c_cflag &= ~(CSIZE|PARENB); s->c_cflag |= CS8; } static __inline int cfsetspeed(struct termios* s, speed_t speed) { // TODO: check 'speed' is valid. s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); return 0; } //static __inline int tcdrain(int fd) { // // A non-zero argument to TCSBRK means "don't send a break". // // The drain is a side-effect of the ioctl! // return ioctl(fd, TCSBRK, __BIONIC_CAST(static_cast, unsigned long, 1)); //} __END_DECLS #endif /* _TERMIOS_H_ */这样 你在运行一次,看可以不,一般就可以了,哈哈~~
当然之后就是找物理端口,一般是/dev/ttyS2,9600 。当然这要根据你们硬件给你们 协议,当然他给你的也只是波特率就是这个9600,这个/dev/ttyS2 可能不是,这时候问硬件?他当然说不知道,你就要使用串口插上,再连上,进入cmd,
1.输入adb shell 只要你设备连接 就会成功的
2.cd /dev 进入这个文件夹
3.ls 看看有没有你们这个ttyS2 你可找到很多东西,比如ttyS0,ttyS1,ttyS3.... 如果有ttyS2 就说明你成功了额,你就不用看了。
但是我没有成功,没有ttyS2,问朋友说,ttyS1 ,我就把所有的是了一下,结果还是失败,没有办法了,问硬件,是怎么调试的,他说是串口转USB,尼玛!!!!怎么不早说,好吧 只有转了,之后就完了? 可能还不是,说没有权限,代码里面是写有设置权限的,但是我弄的时候说/dev/ttyUSB0 找不到文件,尼玛!!!这么坑,没有办法 下载个360root助手,root成功,你在打开,就可以看到 root询问你权限,当然 给权限,你看到 就OK了 ,好累,现在还在和硬件调试协议,硬件好坑,协议一点都不标准,回复又慢,坑的不行。给我ASCII 返回我各种符号 ,好难受啊。