현재 Sevice 에서 usb host 모드를 개발하고 있습니다.
아래와 같이 USB 가 Plug-in 되면 ACTION_USB_DEVICE_ATTACHED 리시버를 통해 디바이스에 연결토록 구현을 하였습니다.
/////////////// 매니페스트
<service
android:name="com.aj.example.XXXService"
android:exported="true"
android:label="@string/app_name"
android:process=":remote"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="com.aj.example.XXXService" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<!-- <action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" /> -->
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</service>
<receiver
android:name="com.aj.example.XXXProcess"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
/////////////// XXXService.java
registerReceiver(mUsbReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
registerReceiver(mUsbReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(ACTION_USB_ATTACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.d(TAG, "usb device was attached! " + device);
if (device != null & device.getVendorId() == GHUB_VENDOR_ID && device.getProductId() == GHUB_PRODUCT_ID)
{
// 디바이스 연결
}
}
else if(ACTION_USB_DETACHED.equals(action)) {
// 디바이스 연결 해제
Log.d(TAG, "usb device was detached! " + mUsbDevice);
}
}
};
그런데, 안드로이드 버전 4.2 ~ 4.4 까지는 리시버가 정상적으로 동작하나, 안드로이드 버전 4.1.2에서는 동작되지 않습니다.
Activity 에서는 리시버가 발생되는 것을 확인했으나 Service에서는 발생되지 않더라고요.
혹시 안드로이드 버그인지, 무엇이 잘못 된건지 궁금합니다.
고수님들의 답변 부탁드립니다.