android 監聽檢測USB存放裝置

來源:互聯網
上載者:User

標籤:

1,USB存放裝置(如:隨身碟,移動硬碟):

 //USB存放裝置 插拔監聽與 SD卡插拔監聽一致。

 private USBBroadCastReceiver mBroadcastReceiver;

     IntentFilter iFilter = new IntentFilter();
       iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
       iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
       iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
       iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);

       iFilter.addDataScheme("file");
       mBroadcastReceiver = new USBBroadCastReceiver();
       registerReceiver(mBroadcastReceiver, iFilter);

private class USBBroadCastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
       String action = intent.getAction();
           
       if (action.equals(Intent.ACTION_MEDIA_EJECT)) {

          //USB裝置移除,更新UI     
       } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {

          //USB裝置掛載,更新UI
        }
      }
 }

  //獲得掛載的USB裝置的儲存空間使用方式

public static String getUSBStorage(Context context){
      // USB Storage  

      //storage/udisk為USB裝置在Android裝置上的掛載路徑.不同廠商的Android裝置路徑不同。

      //這樣寫同樣適合於SD卡掛載。
      File path = new File("/storage/udisk");

      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long totalBlocks = stat.getBlockCount();
      long availableBlocks = stat.getAvailableBlocks();
      String usedSize = Formatter.formatFileSize(context, (totalBlocks-availableBlocks) * blockSize);
      String availableSize = Formatter.formatFileSize(context, availableBlocks * blockSize);
      return usedSize + " / " + availableSize;//空間:已使用/可用的
 }

2,USB外接輸入裝置(如:鍵盤,滑鼠,掃描槍)

try {

     //獲得外接USB輸入裝置的資訊
     Process p=Runtime.getRuntime().exec("cat /proc/bus/input/devices");
     BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
     String line = null;
     while((line = in.readLine())!= null){
       String deviceInfo = line.trim();

       //對擷取的每行的裝置資訊進行過濾,獲得自己想要的。

     }   
     
    } catch (Exception e) {
  // TODO: handle exception
     e.printStackTrace();
    }

另:裝置資訊也可以通過 adb shell 進入執行  cat /proc/bus/input/devices看到。

USB外接輸入裝置資訊列印如下:

I: Bus=0003 Vendor=11c0 Product=0030 Version=0110
N: Name="ACRUX USB Keyboard"
P: Phys=usb-0000:00:04.0-1.3/input1
S: Sysfs=/devices/pci0000:00/0000:00:04.0/usb1/1-1/1-1.3/1-1.3:1.1/input/input3
U: Uniq=
H: Handlers=mouse1 event3
B: PROP=0
B: EV=17
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=103
B: MSC=10

I: Bus=0003 Vendor=11c0 Product=0030 Version=0110 這行資訊會在Android裝置與USB裝置資料互動的是否使用到。

關於USB外接裝置如何與Android裝置資料資料互動的代碼書寫可以參考:

http://developer.android.com/guide/topics/connectivity/usb/index.html

N: Name="ACRUX USB Keyboard" 這行說明了外接的USB裝置的名稱。

 

P: Phys=usb-0000:00:04.0-1.3/input1

S: Sysfs=/devices/pci0000:00/0000:00:04.0/usb1/1-1/1-1.3/1-1.3:1.1/input/input3

這兩行可用於識別該USB裝置串連在Android裝置的哪一個USB口.

android 監聽檢測USB存放裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.