For anybody wishing to run code on USB_ATTACHED and USB_DETACHED you can do the following.
Your Manifest will look something like this:
AddApplicationText(
<activity android:name=".measurements" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
</activity>
)
AddReceiverText(USBService,
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
)
You will also need a read only, otherwise it will be removed during the compile process, device_filter.xml file in res/xml like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="999" product-id="9999" />
</resources>
With the relevant vendor-id and product-id in decimal.
This will get rid of the "USB Request Permission Dialog"
The USBService will run on USB_DETACHED. Note it is not possible to run a service as far as I can tell on USB_ATTACHED as per
android - USB_DEVICE_ATTACHED only startsActivity of Galaxy S3 ICS - Stack Overflow
So you will need to run an Activity, in this case I'm running one called measurements (note it is case sensitive). You can catch the sender like this if you wish and run a service or something else:
Sub Activity_Resume
If Activity.GetStartingIntent.Action = "android.hardware.usb.action.USB_DEVICE_ATTACHED" Then
ConnectUSB
End If
End Sub
So the result is an app that responds to USB_ATTACHED and USB_DETACHED Intents automatically and irrespective as to whether it is running or not.
Hope this is of help for someone.
Cheers,
Mark S.