0

I have a Broadcast receiver that is listening for USB connection events in my app. The code is as follows:

BroadcastReceiver mUsbReceiver; //Initialized elsewhere
void registerMyReceiver() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    this.registerReceiver(this.mUsbReceiver, filter);
}

This code, works fine on some devices, but not others. Larger devices like tablets seem to have better success than phones.

Why would this receiver detect USB connections on some devices but not others?

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78

2 Answers2

0

While answers like this gave insight as to the required cables and Pin connections needed and this one gave info as to the prerequisites of drivers and bad cables, neither answered my core question asked above.

The Android Docs gave info on how to access the devices once they were detected, but of no help in regards to my issue of actually getting them hooked up.

The answer came from this article which states:

We have identified 3 requirements for an Android device to support USB Host Mode and be able to communicate 
with [The USB Device]:

[1] The Android device must be running version 4.1 (Jelly Bean) of the OS, or higher.
[2] The output power on the Android device's USB port should be 5V.
[3] The configuration file android.hardware.usb.host.xml must exist on the Android device in the folder
 /system/etc/permissions. The presence of this configuration file is what enables USB 
Host Mode on your Android device.

The 3rd point is what I want to draw attention to as the xml file in question is this one:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- This is the standard feature indicating that the device can communicate
    with USB devices as the USB host. -->
<permissions>
    <feature name="android.hardware.usb.host" />
</permissions>

Which as you can see is a simple XML file. But as this answer outlines, it needs to be done at root-level in order to access it.

The upshot here is that the OS needs to either have this file set into the correct directory at the kernel level, or the device needs to be rooted and put in after the fact.

enter image description here

One of those 2 solutions will resolve this issue.

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
0

For some devices like Samsung S3 Neo I solved the issue [2] of missing 5V at the USB port with such externally powered USB OTG hub:

Community
  • 1
  • 1
kai-morich
  • 427
  • 2
  • 7