同步操作将从 bluecard2020/AndroidSerialPort 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
It was migrated from Google's official serial communication library and expanded on this basis. Provides a packaged API for serial communication in one minute. Stop bits, data bits, parity, and flow control can be set.
Added in the project's build.gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Added in module build.gradle
dependencies {
implementation 'com.github.Acccord:AndroidSerialPort:1.5.0'
}
-keepclasseswithmembernames class * {
native <methods>;
}
-keep class android.serialport.* {*;}
For the lack of special serial communication settings, use the default serial port configuration
/**
* Open serial port
* @param portStr serial port number
* @param ibaudRate baud rate
*
* @return 0:Open the serial port successfully
* -1:Failed to open the serial port: no serial port read/write permission!
* -2:Failed to open serial port: unknown error!
* -3:Failed to open the serial port: the parameter is wrong!
*/
NormalSerial.instance().open(String portStr, int ibaudRate);
//data: The data you want to send, so your data is sent to the serial port.
NormalSerial.instance().sendData(String data)
//dataListener is the receive data callback of the serial port. The default receiving type is hex.
//Need other data types, this project provides a SerialDataUtils tool conversion on the line
NormalSerial.instance().addDataListener(OnNormalDataListener dataListener)
Summary: After the fast use of only the required open, you can use sendData to send data to the serial port, and addDataListener to listen to the serial port data return. For other features to use, please refer to the following Custom Use.
/**
* Open serial port
* @param portStr Serial port number
* @param ibaudRate Baud rate
*/
BaseSerial mBaseSerial = new BaseSerial() {
@Override
public void onDataBack(String data) {
//Here is the serial port data return, the default return type is hex string
}
};
/**
* Set the serial port
* @param portStr Serial port number
*/
mBaseSerial.setsPort(String sPort);
/**
* Set baud rate
* @param iBaudRate Baud rate
*/
mBaseSerial.setiBaudRate( int iBaudRate);
/**
* Stop bit [1 or 2]
* @param mStopBits Stop bit (default 1)
*/
mBaseSerial.setmStopBits( int mStopBits);
/**
* Data bit [5 ~ 8]
* @param mDataBits Data bit (default 8)
*/
mBaseSerial.setmDataBits( int mDataBits);
/**
* Parity【0 None; 1 Odd; 2 Even】
* @param mParity Parity (default 0)
*/
mBaseSerial.setmParity( int mParity);
/**
* Flow control [No flow control (NONE), hardware flow control (RTS/CTS), software flow control (XON/XOFF)]
* @param mFlowCon Flow control is not used by default
*/
mBaseSerial.setmFlowCon(int mFlowCon);
/**
* Open serial port
*
* @return 0:Open the serial port successfully
* -1:Failed to open the serial port: no serial port read/write permission!
* -2:Failed to open serial port: unknown error!
* -3:Failed to open the serial port: the parameter is wrong!
*/
mBaseSerial.openSerial();
//Send HEX string
mBaseSerial.sendHex(String sHex);
//Send string
mBaseSerial.sendTxt(String sTxt);
//Send byte array
mBaseSerial.sendByteArray(byte[] bOutArray);
Method Name | Return Parameter | Introduction |
---|---|---|
close() | void | Close the serial port |
getBaudRate() | int | Get baud rate |
getDataBits() | int | Get data bit |
getFlowCon() | int | Get flow control |
getParity() | int | Get parity mode |
getPort() | String | Get the serial port number |
getStopBits() | int | Get stop bit |
isOpen() | boolean | Is open |
onDataBack(String data) | void | Serial data reception callback, the method is in the main thread |
openSerial() | int | Open the serial port; 0=Open the serial port successfully; -1=Unable to open the serial port: No serial port read/write permission; -2=Unable to open serial port: Unknown error; -3=Unable to open serial port: Parameter error! |
sendHex(String sHex) | void | Send a HEX string to the serial port |
sendTxt(String sTxt) | void | Send a string to the serial port |
sendByteArray(byte[] bOutArray) | void | Send a byte array to the serial port |
setDelay(int delay) | void | Serial data transmission interval, default 300ms |
setBaudRate(int iBaudRate) | void | Set baud rate |
setGap(int gap) | void | Serial data read interval, default 30ms |
setDataBits(int mDataBits) | void | Set data bit |
setFlowCon(int mFlowCon) | void | Set up flow control |
setParity(int mParity) | void | Set parity mode |
setPort(String sPort) | void | Set the serial port number |
setStopBits(int mStopBits) | void | Set stop bit |
setSerialDataListener(OnSerialDataListener dataListener) | void | Listening to the sending and receiving of serial data, this method can be used for log printing; note that the method callback is not in the main thread |
//Need to call before opening the serial port
SerialPort.setSuPath("/system/xbin/su");
SerialPortFinder serialPortFinder = new SerialPortFinder();
String[] allDevices = serialPortFinder.getAllDevices();
String[] allDevicesPath = serialPortFinder.getAllDevicesPath();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。