Android實(shí)現(xiàn)串口通信
本文實(shí)例為大家分享了Android實(shí)現(xiàn)串口通信的具體代碼,供大家參考,具體內(nèi)容如下
生成so文件
首先確保已經(jīng)安裝了NDK和CMake
然后創(chuàng)建一個(gè)SerialPort.java文件
主要用來處理so文件
注意包名一旦寫好不要更改位置,具體代碼:
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; /** ?* Created by abc on 2021/1/23. ?*/ 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 { ? ? ? ? if (device == null) { ? ? ? ? ? ? System.out.println("device is null"); ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? /* 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 777 " + 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"); ? ? } }
然后clean project 再rebuild project 生成class文件,
這時(shí)候打開如下圖的文件夾看是否生成了classes文件夾,沒有生成請(qǐng)重新再試一遍。
當(dāng)然你也不一定會(huì)生成compileDebugJavaWithJavac 只要javac->debug下存在classes就可以
再打開Terminal輸入指令
cd app/build/intermediates/javac/debug/classes(具體位置定位到classes)
然后再輸入指令
javah -jni 包名.SerialPort
注意 這里javah -jni后面跟的是 SerialPort.java 的全路徑,如果javah報(bào)不存在之類的,是你的Java環(huán)境沒有配置好。
這時(shí)候打開 debug/classes下面的文件發(fā)現(xiàn)多了一個(gè)以 .h 結(jié)尾文件
編寫處理C文件
在main下創(chuàng)建一個(gè)jni包 將生成的.h文件復(fù)制進(jìn)去
然后創(chuàng)建SerialPort.c文件
#include <stdlib.h> #include <stdio.h> #include <jni.h> #include <assert.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 這里是生成.h文件全稱(com_***_***_serialport_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) 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; ?*/ ?//這里同樣是生成.h文件全稱 JNIEXPORT jobject JNICALL Java_com_***_***_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 ?*/ ? //這里同樣是生成.h文件全稱 JNIEXPORT void JNICALL Java_com_***_***_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); }
termios.h 具體代碼:
#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; } __END_DECLS #endif /* _TERMIOS_H_ */
生成so文件
首先,在項(xiàng)目(app)的build.gradel中的defaultConfig下添加:
externalNativeBuild { ? ? ? ? ? ? cmake { ? ? ? ? ? ? ? ? cppFlags "" ? ? ? ? ? ? ? ?// abiFilters "armeabi-v7a", "x86", "arm64-v8a" ? ? ? ? ? ? } ? ? ? ? }
然后再在項(xiàng)目(app)的build.gradel 的 android 閉包下添加:
externalNativeBuild { ? ? ? ? cmake { ? ? ? ? ? ? path "CMakeLists.txt"? ? ? ? ? } ? ? }
在app下創(chuàng)建一個(gè)CMakeLists.txt
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. #設(shè)置生成的so動(dòng)態(tài)庫最后輸出的路徑 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}) add_library( # Sets the name of the library. ? ? ? ? #此處填入library名稱,就是生成so文件的名稱 ? ? ? ? serial_port ? ? ? ? # Sets the library as a shared library. ? ? ? ? SHARED ? ? ? ? # Provides a relative path to your source file(s). c文件或cpp文件的相對(duì)路徑 ? ? ? ? src/main/jni/SerialPort.c ? ? ? ? ) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. 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. ? ? ? ? #此處填入library名稱,就是生成so文件的名稱 ? ? ? ? serial_port ? ? ? ? # Links the target library to the log library ? ? ? ? # included in the NDK. ? ? ? ? ${log-lib})
你現(xiàn)在就可以Make Project 或者 Rebuild Project 一下 ,之后會(huì)在這里會(huì)生成so庫:
把生成的so文件復(fù)制到libs下或者jniLibs下
這時(shí)so文件我們生成了就需要把build的注釋掉不然下次運(yùn)行還會(huì)生成
externalNativeBuild { ? ? ? ? ? ? cmake { ? ? ? ? ? ? ? ? cppFlags "" ? ? ? ? ? ? ? ?// abiFilters "armeabi-v7a", "x86", "arm64-v8a" ? ? ? ? ? ? } ? ? ? ? } ?externalNativeBuild { ? ? ? ? cmake { ? ? ? ? ? ? path "CMakeLists.txt"? ? ? ? ? } ? ? }
然后我們封裝一個(gè)SerialPortUtils
import android.util.Log; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** ?* @author renquan ?*/ public class SerialPortUtils { ? ? private static SerialPortUtils serialPortUtils; ? ? private final String TAG = "SerialPortUtils"; // ? ?private String path = "/dev/ttyS1"; // ? ?private int baudrate = 9600; ? ? public boolean serialPortStatus = false; //是否打開串口標(biāo)志 ? ? public String data_; ? ? public boolean threadStatus; //線程狀態(tài),為了安全終止線程 ? ? public SerialPort serialPort = null; ? ? public InputStream inputStream = null; ? ? public OutputStream outputStream = null; ? ? public ChangeTool changeTool = new ChangeTool(); ? ? public static SerialPortUtils getInstance() { ? ? ? ? if (null == serialPortUtils){ ? ? ? ? ? ? serialPortUtils = new SerialPortUtils(); ? ? ? ? } ? ? ? ? return serialPortUtils; ? ? } ? ? /** ? ? ?* 獲取狀態(tài) ? ? ?* @return ? ? ?*/ ? ? public boolean getPortStatus(){ ? ? ? ? return serialPortStatus; ? ? } ? ? /** ? ? ?* 打開串口 ? ? ?* @return serialPort串口對(duì)象 ? ? ?*/ ? ? public SerialPort openSerialPort(String path, int baudrate){ ? ? ? ? if (getPortStatus()){ ? ? ? ? ? ? return serialPort; ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? serialPort = new SerialPort(new File(path),baudrate,0); ? ? ? ? ? ? this.serialPortStatus = true; ? ? ? ? ? ? threadStatus = false; //線程狀態(tài) ? ? ? ? ? ? //獲取打開的串口中的輸入輸出流,以便于串口數(shù)據(jù)的收發(fā) ? ? ? ? ? ? inputStream = serialPort.getInputStream(); ? ? ? ? ? ? outputStream = serialPort.getOutputStream(); ? ? ? ? ? ? new ReadThread().start(); //開始線程監(jiān)控是否有數(shù)據(jù)要接收 ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? Log.d(TAG,"打開串口異常"); ? ? ? ? ? ? Toast.makeText(MyApp.context, "打開串口異常", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? isException = true; ? ? ? ? ? ? return serialPort; ? ? ? ? } ? ? ? ? Log.d(TAG, "openSerialPort: 打開串口"); ? ? ? ? Toast.makeText(MyApp.context, "打開串口", Toast.LENGTH_SHORT).show(); ? ? ? ? return serialPort; ? ? } ? ? /** ? ? ?* 關(guān)閉串口 ? ? ?*/ ? ? public void closeSerialPort(){ ? ? ? ? if (!getPortStatus()){ ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? if (null == inputStream || null == outputStream){ ? ? ? ? ? ? Log.e(TAG, "closeSerialPort: 關(guān)閉串口異常:"+"null"); ? ? ? ? ? ? Toast.makeText(MyApp.context, "關(guān)閉串口異常", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? isException = true; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? inputStream.close(); ? ? ? ? ? ? outputStream.close(); ? ? ? ? ? ? this.serialPortStatus = false; ? ? ? ? ? ? this.threadStatus = true; //線程狀態(tài) ? ? ? ? ? ? serialPort.close(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? Log.e(TAG, "closeSerialPort: 關(guān)閉串口異常:"+e.toString()); ? ? ? ? ? ? Toast.makeText(MyApp.context, "關(guān)閉串口異常", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? isException = true; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? Log.d(TAG, "closeSerialPort: 關(guān)閉串口成功"); ? ? ? ? Toast.makeText(MyApp.context, "關(guān)閉串口", Toast.LENGTH_SHORT).show(); ? ? } ? ? /** ? ? ?* 發(fā)送串口指令(字符串) ? ? ?* @param data String數(shù)據(jù)指令 ? ? ?*/ ? ? public void sendSerialPort(String data){ ? ? ? ? Log.d(TAG, "sendSerialPort: 發(fā)送數(shù)據(jù)"+data); ? ? ? ? if (null == outputStream){ ? ? ? ? ? ? Log.e(TAG, "sendSerialPort: 串口數(shù)據(jù)發(fā)送失?。?+"null"); ? ? ? ? ? ? Toast.makeText(MyApp.context, "串口數(shù)據(jù)發(fā)送失敗", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? byte[] sendData = data.getBytes(); //string轉(zhuǎn)byte[] ? ? ? ? ? ? this.data_ = new String(sendData); //byte[]轉(zhuǎn)string ? ? ? ? ? ? if (sendData.length > 0) { ? ? ? ? ? ? ? ? outputStream.write(sendData); // ? ? ? ? ? ? ? ?outputStream.write('\n'); // ? ? ? ? ? ? ? ?outputStream.write('\r'+'\n'); ? ? ? ? ? ? ? ? outputStream.write('\r'); ? ? ? ? ? ? ? ? outputStream.flush(); ? ? ? ? ? ? ? ? Log.d(TAG, "sendSerialPort: 串口數(shù)據(jù)發(fā)送成功"); ? ? ? ? ? ? } ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? Log.e(TAG, "sendSerialPort: 串口數(shù)據(jù)發(fā)送失敗:"+e.toString()); ? ? ? ? ? ? Toast.makeText(MyApp.context, "串口數(shù)據(jù)發(fā)送失敗", Toast.LENGTH_SHORT).show(); ? ? ? ? } ? ? } ? ? /** ? ? ?* 單開一線程,來讀數(shù)據(jù) ? ? ?*/ ? ? private class ReadThread extends Thread { ? ? ? ? @Override ? ? ? ? public void run() { ? ? ? ? ? ? super.run(); ? ? ? ? ? ? //判斷進(jìn)程是否在運(yùn)行,更安全的結(jié)束進(jìn)程 ? ? ? ? ? ? while (!threadStatus){ ? ? ? ? ? ? ? ? Log.d(TAG, "進(jìn)入線程run"); ? ? ? ? ? ? ? ? //64 ? 1024 ? ? ? ? ? ? ? ? byte[] buffer = new byte[64]; ? ? ? ? ? ? ? ? int size; //讀取數(shù)據(jù)的大小 ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? size = inputStream.read(buffer); ? ? ? ? ? ? ? ? ? ? if (size > 0){ ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "run: 接收到了數(shù)據(jù):" + changeTool.ByteArrToHex(buffer)); ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "run: 接收到了數(shù)據(jù)大小:" + String.valueOf(size)); // ? ? ? ? ? ? ? ? ? ? ? ?onDataReceiveListener.onDataReceive(buffer,size); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? Log.e(TAG, "run: 數(shù)據(jù)讀取異常:" +e.toString()); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? //數(shù)據(jù)回調(diào) ? ? public OnDataReceiveListener onDataReceiveListener = null; ? ? public static interface OnDataReceiveListener { ? ? ? ? public void onDataReceive(byte[] buffer, int size); ? ? } ? ? public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) { ? ? ? ? onDataReceiveListener = dataReceiveListener; ? ? } }
常見bug
這說明我們生成的so文件有問題 那么就從頭執(zhí)行一次
或者說我們并沒有實(shí)現(xiàn)so的open方法這時(shí)需要看SerialPort.java
或者打個(gè)log看看so文件內(nèi)的SerialPort和我們創(chuàng)建的SerialPort包名是否一致
如果我們走到了這里拋出異常
說明我們的Android機(jī)器并沒有root無法訪問權(quán)限這時(shí)我們需要找到廠商改下系統(tǒng)
或者系統(tǒng)給了權(quán)限但是我們并沒有添加
<!--往sdcard中寫入數(shù)據(jù)的權(quán)限 --> ? ? <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> ? ? <!--在sdcard中創(chuàng)建/刪除文件的權(quán)限 --> ? ? <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
*注意
由于設(shè)備不同文件路徑有不同
這里默認(rèn)的是/system/bin/su
有的機(jī)型路徑是/system/xbin/su
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)空心圓角矩形按鈕的實(shí)例代碼
頁面上有時(shí)會(huì)用到背景為空心圓角矩形的Button,可以通過xml繪制出來。這篇文章主要介紹了Android實(shí)現(xiàn)空心圓角矩形按鈕的實(shí)例代碼,需要的朋友參考下吧2017-01-01Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android4.X讀取SIM卡短信和聯(lián)系人相關(guān)類實(shí)例分析
這篇文章主要介紹了Android 4.X讀取SIM卡短信和聯(lián)系人相關(guān)類,以實(shí)例形式分析了Android 4.X讀取SIM卡短信和聯(lián)系人的兩個(gè)相關(guān)類的功能、用法與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android打造流暢九宮格抽獎(jiǎng)活動(dòng)效果
抽獎(jiǎng)活動(dòng)有很多種形式,轉(zhuǎn)盤抽獎(jiǎng),九宮格抽獎(jiǎng),刮刮卡抽獎(jiǎng),這篇文章主要為大家詳細(xì)介紹了如何打造流暢九宮格抽獎(jiǎng)活動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android獲取照片的快照 思路及實(shí)現(xiàn)方法
android獲取照片的快照 思路及實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-06-06Android滑動(dòng)優(yōu)化高仿QQ6.0側(cè)滑菜單(滑動(dòng)優(yōu)化)
之前的實(shí)現(xiàn)只是簡(jiǎn)單的可以顯示和隱藏左側(cè)的菜單,但是特別生硬,而且沒有任何平滑的趨勢(shì),那么今天就來優(yōu)化一下吧,加上平滑效果,而且可以根據(jù)手勢(shì)滑動(dòng)的方向來判斷是否是顯示和隱藏2016-02-02Android顯示系統(tǒng)SurfaceFlinger詳解
本文詳細(xì)講解了Android顯示系統(tǒng)SurfaceFlinger,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12RecyclerView實(shí)現(xiàn)流式標(biāo)簽單選多選功能
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。這篇文章主要介紹了RecyclerView實(shí)現(xiàn)的流式標(biāo)簽單選多選功能,需要的朋友可以參考下2019-11-11