Android获取串口路径
yangyang031213 2018-11-23 17:55:50 3585 收藏 1 分类专栏: Android 版权 使用 File::listFiles 遍历文件,查找读取 idProduct 和 idVendor 文件,然后找到 ttyUSB 路径。
public String GetTtyUsbPath(){ String root = "/sys/bus/usb/devices/"; File dev = new File(root); File[] files = dev.listFiles(); for(int i = 0;i < files.length;++i){ if(files[i].isDirectory()){ File fProduct = new File(files[i].getAbsolutePath() + "/idProduct"); File fVendor = new File(files[i].getAbsolutePath() + "/idVendor"); if(fProduct.exists() && fVendor.exists()){ try { LineNumberReader readerProduct = new LineNumberReader(new FileReader(fProduct)); String version = readerProduct.readLine(); if(version != null && version.equals("2303")){ }else{ continue; } readerProduct.close(); }catch (Exception e){ e.printStackTrace(); } try { LineNumberReader readerVendor = new LineNumberReader(new FileReader(fVendor)); String version = readerVendor.readLine(); if(version != null && version.equals("067b")){ }else{ continue; } readerVendor.close(); }catch (Exception e){ e.printStackTrace(); } File fdev = new File(files[i].getAbsolutePath()); File[] f = fdev.listFiles(); for(int j = 0;j < f.length;++j){ if(f[j].isDirectory() && f[j].getName().startsWith(files[i].getName())){ File ttyDev = new File(f[j].getAbsolutePath()); File[] fTty = ttyDev.listFiles(); for (int k = 0;k < fTty.length;++k){ if(fTty[k].getName().startsWith("ttyUSB")){ Log.e(TAG, "Find GPS USB Dev Path:" + fTty[k].getName()); return "/dev/" + fTty[k].getName(); } } } } } } } return null; } ———————————————— 版权声明:本文为博主「yangyang031213」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/yangyang031213/article/details/84401496