Android获取串口路径

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;
 }
————————————————
版权声明:本文为CSDN博主「yangyang031213」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yangyang031213/article/details/84401496

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值