APK:SerialPortFinder,显示串口列表
SerialPortFinder 方法 有点意思 这个方法是不涉及底层的,没有native方法,也不需要c和h文件 就能识别串口
MainActivity.java
package com.gatsby.serialport; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import android_serialport_api.SerialPort; import android_serialport_api.SerialPortFinder; public class MainActivity extends AppCompatActivity implements View.OnClickListener { SerialPortFinder mSerialPortFinder; final String[] devices = new SerialPortFinder().getAllDevicesPath(); final List<String> data = new ArrayList<>(); protected SerialPort mSerialPort; protected InputStream mInputStream; protected OutputStream mOutputStream; private String prot = "/dev/ttyS3"; private int baudrate = 9600; //数据 private static long i = 0; private Toast mToast; private Thread receiveThread; private Thread sendThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = findViewById(R.id.id_list); listView.setAdapter(bA); getSerialPortPression(); initLayout(); mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT); } private void initLayout() { findViewById(R.id.btn_open).setOnClickListener(MainActivity.this); findViewById(R.id.btn_send).setOnClickListener(MainActivity.this); findViewById(R.id.btn_close).setOnClickListener(MainActivity.this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_open: showTip("btn_open"); // 打开 try { mSerialPort = new SerialPort(new File(prot), baudrate,8,1,'N'); mInputStream = mSerialPort.getInputStream(); mOutputStream = mSerialPort.getOutputStream(); receiveThread(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { //打开失败 Log.e("gatsby", "open serialport default"); e.printStackTrace(); } break; case R.id.btn_send: showTip("btn_send"); // 发送 sendThread = new Thread() { @Override public void run() { while (true) { try { i++; mOutputStream.write((String.valueOf(i)).getBytes()); //发送成功 Log.i("gatsby", "send serialport sucess!" + i); Thread.sleep(50); } catch (Exception e) { //发送失败 Log.i("gatsby", "send serialport default!"); e.printStackTrace(); } } } }; sendThread.start(); break; case R.id.btn_close: showTip("btn_close"); closeSerialPort(); break; default: break; } } private void receiveThread() { // 接收 receiveThread = new Thread() { @Override public void run() { while (true) { int size; try { byte[] buffer = new byte[512]; if (mInputStream == null) return; size = mInputStream.read(buffer); if (size > 0) { String recinfo = new String(buffer, 0, size); Log.i("gatsby", "recevier serialport info :" + recinfo); // sb = recinfo; // handler.sendEmptyMessage(1); } } catch (IOException e) { e.printStackTrace(); } } } };//111 receiveThread.start(); } /** * 关闭串口 */ public void closeSerialPort() { if (mSerialPort != null) { mSerialPort.close(); } if (mInputStream != null) { try { mInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (mOutputStream != null) { try { mOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } private void showTip(final String str) { mToast.setText(str); mToast.show(); } public void getSerialPortPression() { if (devices.length > 0) { for (String device : devices) { File file = new File(device); String path = file.getAbsolutePath(); StringBuilder permission = new StringBuilder(); permission.append("["); permission.append(file.canRead() ? " 可读" : "不可读"); permission.append(file.canWrite() ? " 可写 " : " 不可写 "); permission.append(file.canExecute() ? "可执行" : "不可执行"); permission.append("]"); String msg = "(" + path + ")" + " 权限:" + permission; data.add(msg); } } } BaseAdapter bA = new BaseAdapter() { @Override public int getCount() { return devices.length; } @Override public Object getItem(int position) { return devices[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = getLayoutInflater().inflate(R.layout.item_layout, null); TextView textView = convertView.findViewById(R.id.id_item_text); textView.setText(data.get(position)); return convertView; } }; }