Android USB Detection
-
The best way to achieve this will be to install a server application on your own device, and have a client-based application on other devices. Then upon connection, detect the devices and connect to them using server-client channel that you will hold in the network. This is the safest way, and the simplest way, otherwise USB won't allow much of permissions, such as executing any command (write commands are way too strict). There are many ways to do this, [Android Client-Server Using Sockets – Client Implementation](http://androidsrc.net/android-client-server-using-sockets-client-implementation/), since you require USB based communication, please check the following and then move onwards to a conclusion. [root access - How do I use Android device with USB OTG device as MTP client for another device? - Android Enthusiasts Stack Exchange](http://android.stackexchange.com/questions/149612/how-do-i-use-android-device-with-usb-otg-device-as-mtp-client-for-another-device) [usb on the go - What is the difference between USB On-The-Go (OTG) and USB Host Mode? - Android Enthusiasts Stack Exchange](http://android.stackexchange.com/questions/51035/what-is-the-difference-between-usb-on-the-go-otg-and-usb-host-mode)
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
This has nothing to do with windows.I want to detect when OTG Cable is plugged inside inside android usb port and go to app and enable switch!!!
Pavlex4 wrote:
I want to detect when OTG Cable is plugged inside inside android usb port...
I may be wrong, but I don't think that is possible, since a circuit has not yet been completed. You've basically just extended the wires at the USB port.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
This has nothing to do with windows.I want to detect when OTG Cable is plugged inside inside android usb port and go to app and enable switch!!!
As already said by David, there will be no event when only plugging in a cable. To monitor events see for example (first result when googling for "android usb events"): How to Monitor USB Events on Android[^]. Read also USB Host and Accessory | Android Developers[^]:
Quote:
Note: Support for USB host and accessory modes are ultimately dependant on the device's hardware, regardless of platform level.
So it might be not possible with your device.
-
As already said by David, there will be no event when only plugging in a cable. To monitor events see for example (first result when googling for "android usb events"): How to Monitor USB Events on Android[^]. Read also USB Host and Accessory | Android Developers[^]:
Quote:
Note: Support for USB host and accessory modes are ultimately dependant on the device's hardware, regardless of platform level.
So it might be not possible with your device.
When otg cable is plugged in kernel says "USB connector connected" ! How to enable switch inside app when kernel says above message? or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out? or to detect when Android enters Host Mode using app?
-
When otg cable is plugged in kernel says "USB connector connected" ! How to enable switch inside app when kernel says above message? or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out? or to detect when Android enters Host Mode using app?
Then it is not a simple cable but has built-in resistors or something connected at the other end. If you got a kernel message, you should be able to get an event using udev rules. But this requires root privileges to save the rules file.
-
Then it is not a simple cable but has built-in resistors or something connected at the other end. If you got a kernel message, you should be able to get an event using udev rules. But this requires root privileges to save the rules file.
-
Sorry that I mentioned udev. It is not used by Android.
-
Sorry that I mentioned udev. It is not used by Android.
Than how to do any of this: When otg cable is plugged in kernel says "USB connector connected" ! How to enable switch inside app when kernel says above message? or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out? or to detect when Android enters Host Mode using app?
-
Than how to do any of this: When otg cable is plugged in kernel says "USB connector connected" ! How to enable switch inside app when kernel says above message? or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out? or to detect when Android enters Host Mode using app?
By inspecting the Android sources to find which module is generating the message (if the message is not prefixed with the module name). Once the module is known check if it provides some kind of notification that can be used. Or trying to use the provided Android APIs like android.hardware.usb | Android Developers[^]. Or polling the /dev directory for changes. The host mode is entered when an USB device is attached. That can be detected as described in my initial answer (if supported). If you did not get a final answer here or searching the web, you can try to do it yourself. This requires some research and implementing one or more of the above.
-
Than how to do any of this: When otg cable is plugged in kernel says "USB connector connected" ! How to enable switch inside app when kernel says above message? or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out? or to detect when Android enters Host Mode using app?
Have you tried creating a
BroadcastReceiver
and registering it with aACTION_USB_DEVICE_ATTACHED
filter?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Have you tried creating a
BroadcastReceiver
and registering it with aACTION_USB_DEVICE_ATTACHED
filter?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Yes I tried but it only work when I plug in otg cable with usb device attached to it and enable manually otg inside app and enter app that detects it.Without usb device on otg cable it won't detect it!!!I used ACTION_USB_DEVICE_ATTACED with this app: How to Monitor USB Events on Android[^]
-
By inspecting the Android sources to find which module is generating the message (if the message is not prefixed with the module name). Once the module is known check if it provides some kind of notification that can be used. Or trying to use the provided Android APIs like android.hardware.usb | Android Developers[^]. Or polling the /dev directory for changes. The host mode is entered when an USB device is attached. That can be detected as described in my initial answer (if supported). If you did not get a final answer here or searching the web, you can try to do it yourself. This requires some research and implementing one or more of the above.
-
Have you tried creating a
BroadcastReceiver
and registering it with aACTION_USB_DEVICE_ATTACHED
filter?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
How to find modules that send messages inside kernel source? I have found file called host_notifier.c inside /drivers/usb/otg/ inside kernel source! There is also file called Kconfig which contains this:
USB Host notify configuration
config USB_HOST_NOTIFY
boolean "USB Host notify Driver"
depends on USB
help
Android framework needs uevents for usb host operation.
Host notify Driver serves uevent format
that is used by usb host or otg.config USB_DEBUG_DETEAILED_LOG
boolean "USB detailed log for debugging"
depends on USB
help
Add detailed log for debugging. -
How to find modules that send messages inside kernel source? I have found file called host_notifier.c inside /drivers/usb/otg/ inside kernel source! There is also file called Kconfig which contains this:
USB Host notify configuration
config USB_HOST_NOTIFY
boolean "USB Host notify Driver"
depends on USB
help
Android framework needs uevents for usb host operation.
Host notify Driver serves uevent format
that is used by usb host or otg.config USB_DEBUG_DETEAILED_LOG
boolean "USB detailed log for debugging"
depends on USB
help
Add detailed log for debugging.That is the kernel config file which defines what has to be compiled. If you have also a Makefile, you can check which sources are compiled when
USB_HOST_NOTIFY
is set. But it already contains a hint: It usesuevent
. But again, these might only occur when a device is attached. To know when the cable message is generated, you have to find the message string in the sources. -
That is the kernel config file which defines what has to be compiled. If you have also a Makefile, you can check which sources are compiled when
USB_HOST_NOTIFY
is set. But it already contains a hint: It usesuevent
. But again, these might only occur when a device is attached. To know when the cable message is generated, you have to find the message string in the sources. -
I am searching inside /drivers/usb Which of these folders could contain message: atm c67x00 class core dwc3 early gadget host image misc mon musb otg renesas_usbhs serial storage wusbcore
-
Have you tried creating a
BroadcastReceiver
and registering it with aACTION_USB_DEVICE_ATTACHED
filter?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I have searched all files and I didn't find kernel message for USB !!!! I ran android terminal on my phone, typed cat /proc/kmsg and when plugged in otg cable I got msm_otg f9a55000.usb : host on I found that message inside msm_otg kernel source:
static void msm_otg_start_host(struct usb_otg *otg, int on)
{
struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
struct msm_otg_platform_data *pdata = motg->pdata;
struct usb_hcd *hcd;if (!otg->host) return;
#ifdef CONFIG_USB_HOST_NOTIFY
msm_otg_host_notify(motg, on);
#endifhcd = bus\_to\_hcd(otg->host); if (on) { dev\_dbg(otg->phy->dev, "host on\\n"); if (pdata->otg\_control == OTG\_PHY\_CONTROL) ulpi\_write(otg->phy, OTG\_COMP\_DISABLE, ULPI\_SET(ULPI\_PWR\_CLK\_MNG\_REG)); /\* \* Some boards have a switch cotrolled by gpio \* to enable/disable internal HUB. Enable internal \* HUB before kicking the host. \*/ if (pdata->setup\_gpio) pdata->setup\_gpio(OTG\_STATE\_A\_HOST); usb\_add\_hcd(hcd, hcd->irq, IRQF\_SHARED); } else { dev\_dbg(otg->phy->dev, "host off\\n"); usb\_remove\_hcd(hcd); /\* HCD core reset all bits of PORTSC. select ULPI phy \*/ writel\_relaxed(0x80000000, USB\_PORTSC); if (pdata->setup\_gpio) pdata->setup\_gpio(OTG\_STATE\_UNDEFINED); if (pdata->otg\_control == OTG\_PHY\_CONTROL) ulpi\_write(otg->phy, OTG\_COMP\_DISABLE, ULPI\_CLR(ULPI\_PWR\_CLK\_MNG\_REG)); }
}
and do I need to create something like this: As code goes by: UEvents in Android - from kernel events to notifications[^]
-
That is the kernel config file which defines what has to be compiled. If you have also a Makefile, you can check which sources are compiled when
USB_HOST_NOTIFY
is set. But it already contains a hint: It usesuevent
. But again, these might only occur when a device is attached. To know when the cable message is generated, you have to find the message string in the sources. -
By inspecting the Android sources to find which module is generating the message (if the message is not prefixed with the module name). Once the module is known check if it provides some kind of notification that can be used. Or trying to use the provided Android APIs like android.hardware.usb | Android Developers[^]. Or polling the /dev directory for changes. The host mode is entered when an USB device is attached. That can be detected as described in my initial answer (if supported). If you did not get a final answer here or searching the web, you can try to do it yourself. This requires some research and implementing one or more of the above.
I have searched all files and I didn't find kernel message for USB !!!! I ran android terminal on my phone, typed cat /proc/kmsg and when plugged in otg cable I got msm_otg f9a55000.usb : host on I found that message inside msm_otg kernel source:
static void msm_otg_start_host(struct usb_otg *otg, int on)
{
struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
struct msm_otg_platform_data *pdata = motg->pdata;
struct usb_hcd *hcd;if (!otg->host) return;
#ifdef CONFIG_USB_HOST_NOTIFY
msm_otg_host_notify(motg, on);
#endifhcd = bus\_to\_hcd(otg->host); if (on) { dev\_dbg(otg->phy->dev, "host on\\n"); if (pdata->otg\_control == OTG\_PHY\_CONTROL) ulpi\_write(otg->phy, OTG\_COMP\_DISABLE, ULPI\_SET(ULPI\_PWR\_CLK\_MNG\_REG)); /\* \* Some boards have a switch cotrolled by gpio \* to enable/disable internal HUB. Enable internal \* HUB before kicking the host. \*/ if (pdata->setup\_gpio) pdata->setup\_gpio(OTG\_STATE\_A\_HOST); usb\_add\_hcd(hcd, hcd->irq, IRQF\_SHARED); } else { dev\_dbg(otg->phy->dev, "host off\\n"); usb\_remove\_hcd(hcd); /\* HCD core reset all bits of PORTSC. select ULPI phy \*/ writel\_relaxed(0x80000000, USB\_PORTSC); if (pdata->setup\_gpio) pdata->setup\_gpio(OTG\_STATE\_UNDEFINED); if (pdata->otg\_control == OTG\_PHY\_CONTROL) ulpi\_write(otg->phy, OTG\_COMP\_DISABLE, ULPI\_CLR(ULPI\_PWR\_CLK\_MNG\_REG)); }
}
and do I need to create something like this: As code goes by: UEvents in Android - from kernel events to notifications[^]