2016-07-21 5 views
0

デバイスが切断されたかどうかをプログラムで検出できますか?Androidアクセサリモードでのデバイス切断

アクセサリモードアプリケーションで切断イベントにフラグを立てようとしています。 それは可能ですか?はい、どうですか?

答えて

0

BroadcastReceiverを使用して、UsbManager.ACTION_USB_DEVICE_DETACHEDイベントとUsbManager.ACTION_USB_DEVICE_ATTACHEDイベントを待機することができます。

<receiver android:name="UsbConnectionReceiver"> 
     <intent-filter> 
      <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"> 
      </action> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.hardware.usb.action.ACTION_USB_DEVICE_DETACHED"> 
      </action> 
     </intent-filter> 
    </receiver> 

それともでし定期的にお使いのデバイスがリストにあるかどうかをチェックする(タイマーやScheduledExecutorServiceを使用して)接続されたデバイスを循環。

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); 
HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); 
UsbDevice device = deviceList.get("deviceName"); 

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); 
HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); 
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
while(deviceIterator.hasNext()){ 
UsbDevice device = deviceIterator.next(); 
if (usbDeviceIsMyDevice(device)){ 
    // your code 
} 
} 
関連する問題