Android 串口配置
\frameworks\base\core\res\res\values
values.xml
<string-array translatable="false" name="config_serialPorts"><item>"dev/ttyHS0"</item><item>"dev/ttyHS1"</item><item>"dev/ttyHS2"</item></string-array>
import android.hardware.SerialManager;
import android.hardware.SerialPort;private ByteBuffer mInputBuffer;
private ByteBuffer mOutputBuffer;
private SerialManager mSerialManager;
private SerialPort mSerialPort;mSerialManager = (SerialManager) getSystemService(Context.SERIAL_SERVICE);if (mSerialManager != null) {mInputBuffer = ByteBuffer.allocateDirect(1024);mOutputBuffer = ByteBuffer.allocateDirect(1024);}
String[] ports = mSerialManager.getSerialPorts();
Log.d(TAG, "ports:" + ports[0]);if (ports != null) {Log.e(TAG, "onData() ports[].length: " + ports.length);for (int i = 0; i < ports.length; i++) {Log.e(TAG, "onData() mPorts[" + i + "]: " + ports[i]);}}if (ports != null && ports.length > 0) {try {//mSerialPort = mSerialManager.openSerialPort(ports[0], 115200);mSerialPort = mSerialManager.openSerialPort(ports[2], 9600);if (mSerialPort != null) {//........}} catch (IOException e) {android.util.Log.e(TAG, "onResume: " + e.toString());}}
2025-03-27 22:33:48.748 4917-4917 SerialActivity com.android.test D ports:dev/ttyHS0
2025-03-27 22:33:48.748 4917-4917 SerialActivity com.android.test E onData() ports[].length: 3
2025-03-27 22:33:48.748 4917-4917 SerialActivity com.android.test E onData() mPorts[0]: dev/ttyHS0
2025-03-27 22:33:48.748 4917-4917 SerialActivity com.android.test E onData() mPorts[1]: dev/ttyHS1
2025-03-27 22:33:48.749 4917-4917 SerialActivity com.android.test E onData() mPorts[2]: dev/ttyHS2