Device Filter XML File

citywest

Member
Licensed User
Longtime User
Hello All,

This is probably a simple question but for the life of me I can't find any reference to it in the forums.

I'm using USB host in my app which downloads data from medical devices. All of this works fine but what is driving my users crazy is the requirement to Allow USB Permissions for the device each time they connect it. Being medical devices it is not possible to operate them whilst connected with a USB cable so for each measure set it needs to be un-plugged and then re-plugged. Ticking "Use by default for this USB device" makes no difference.

It seems from this link that I can fix it along these lines:

USB Host | Android Developers

But I don't know where to place the device_filter.xml file. If I place it in res/xml then it is removed at compile time so of course the compile fails.

If anybody know the answer to this or some other way of making the Permission persistent then it would be much appreciated.

Thanks in advance,

Mark S.
 

agraham

Expert
Licensed User
Longtime User
Maybe not! From "Obtaining permission to communicate with a device" in that link
Note: If your application uses an intent filter to discover USB devices as they're connected, it automatically receives permission if the user allows your application to handle the intent. If not, you must request permission explicitly in your application before connecting to the device.
 
Upvote 0

citywest

Member
Licensed User
Longtime User
Thank you gentlemen,

I guess I won't know unless I try it so any ideas as to how I can include device_filter.xml in the build? I suppose I could try manually adding a modified manifest.xml and the device_filter.xml to the APK after the build but it is likely to become quite tedious very quickly!

It is a significant problem for my users. There is some confidentiality around this project so I will explain in 'round about' detail.

The operator of the medical device takes two sets of measures which I'll call on-side and off-side. These can be arbitrary in number but usually six or eight either one side first and the second side next or alternating between sides with any number of measures between alternate sides. This is driven by the skill and experience of the operator.

There is some subjectivity with each measure in that the operator must remember some parameters such as the placement of the instrument and patient, patient reaction (they may/will flinch or move) and a few other things which impact on the accuracy of each measurement.

The Android application downloads data from the instrument which the operator needs to sift through and rate based on this subjectivity. When this has been done a number of calculations are carried out and the operator is presented with a single summary view for on-side off-side and the entire data set is stored in a local DB as well uploaded to a centralised DB.

The problem with the Permission Popup is that it interrupts the operators train of thought and the subjective aspect of each measure can be lost. Also the operator may decide to view data part way through a set of measures in the case of abnormal results on the devices own control screen.

As mentioned the device must be detached from the phone for measures to occur. There is a mandatory regulatory safety requirement that devices of this type and many others cannot be operational if there is any chance that the device could be somehow connected to an unapproved mains power supply. This would be the case if say somebody connected a notebook to the device via USB and then plugged the notebook into a mains power supply.

Sorry for the 'long winded' explanation and thank you in advance for any pointers in getting the device_filter.xml included in the build process.

Cheers,

Mark S.
 
Upvote 0

citywest

Member
Licensed User
Longtime User
OK saving the device_filter.xml to res/xml and setting read only includes in the build. Modify the manifest through the Manifest Editor,

AND the Request Permission modal has gone so problem solved. Hurray!

Regards,

Mark S.
 
Upvote 0

citywest

Member
Licensed User
Longtime User
For anybody wishing to run code on USB_ATTACHED and USB_DETACHED you can do the following.

Your Manifest will look something like this:

B4X:
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:
B4X:
<?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:

B4X:
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.
 
Last edited:
Upvote 0
Top