0
デバイスが切断されたかどうかをプログラムで検出できますか?Androidアクセサリモードでのデバイス切断
アクセサリモードアプリケーションで切断イベントにフラグを立てようとしています。 それは可能ですか?はい、どうですか?
デバイスが切断されたかどうかをプログラムで検出できますか?Androidアクセサリモードでのデバイス切断
アクセサリモードアプリケーションで切断イベントにフラグを立てようとしています。 それは可能ですか?はい、どうですか?
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
}
}