1

Here is my schematic schematic

  1. My device is a non-standard USB device
  2. I only use the 5V power of cellphone to supply my device by connecting the ID pin to ground
  3. I connect the D- pin to 5V with a 1.5k pull-up resistor in order to tell the phone there is a voltage change on the D- pin when my device plugged in
  4. I don't use the D+/D- pin to communicate with the phone

My question: how to detect the device when it attached to an android phone? I have tried to read the both variable

UsbManager.ACTION_USB_DEVICE_ATTACHED 
android.hardware.usb.action.USB_STATE

both have no change.

but weird is when I insert a USB wire the android.hardware.usb.action.USB_STATE =connected

lukeluck
  • 11
  • 5
  • My guess is you don't with simple circuitry as a USB port is not only a mechanical connection but protocol as well. See: http://esd.cs.ucr.edu/webres/usb11.pdf and https://www.beyondlogic.org/usbnutshell/usb1.shtml – Morrison Chang May 31 '18 at 07:05
  • @ Morrison Chang yes, the USB plug is only for supplying power to my device, and the device does not communicate with the phone. – lukeluck May 31 '18 at 07:09
  • `I have tried to read the both variable UsbManager.ACTION_USB_DEVICE_ATTACHED ` ??? Variable? It is just a string containing an action description. You should implement a broadcast receiver for this action. I told you that before. – greenapps May 31 '18 at 07:57
  • Even if you have a broadcast receiver and it would be invoked the system could not tell you if it was your device as you made communication impossible. – greenapps May 31 '18 at 07:59
  • @greenapps but why I insert a USB wire he android.hardware.usb.action.USB_STATE =connected , what's more the code is like this link:https://blog.csdn.net/chy555chy/article/details/52350810 – lukeluck May 31 '18 at 08:33
  • @greenapps a wire which has no chip inside cannot be able to communicate with the phone, but it can be detected. how to do this? and what's the mechanism – lukeluck May 31 '18 at 08:36
  • You have not reacted to my suggestion implementing a broadcast receiver. If you do not react it is difficult talking. – greenapps May 31 '18 at 09:43
  • @lukeluck you should have referenced your hardware post here: https://electronics.stackexchange.com/q/377012/5078 This may help: https://stackoverflow.com/a/43161190/295004 but honestly you should find/create a app and test it with a known working OTG cable/device as right now no one can tell if you have a working [MCVE](https://stackoverflow.com/help/mcve) and this site doesn't do tutorials if you are new to Android (your link and mine have a `BroadcastReceiver` as @greenapps references). – Morrison Chang May 31 '18 at 19:24

1 Answers1

0

USB is a complex protocol. USB devices are organized in device classes like mass storage class, communication device class ( CDC ), ... ( http://www.usb.org/developers/docs/devclass_docs/ , https://www.kernel.org/doc/html/v4.13/driver-api/usb/power-management.html ) when a USB device is attached to a host it reports its class to the host ( via the data D- D+ lines ) and the host loads the appropriate kernel modules / drivers and configuration

the device class and USB type also determines how much power is the output of the USB port, when simply connecting a USB cable a standard configuration for output power is used ( i think 500 mA ) however i do not know the mechanism of how the host detects an attached USB cable, maybe use a multimeter and measure the resistance and capacitances between the various lines of an USB cable ...

possibly attach your device to an USB cable and attach it to the android because the cable is detected and power output enabled ...

ralf htp
  • 9,149
  • 4
  • 22
  • 34