APK:SerialPortFinder,显示串口列表
SerialPortFinder 方法 有点意思 这个方法是不涉及底层的,没有native方法,也不需要c和h文件 就能识别串口
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 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; } }; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】