Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Android USB Detection

Android USB Detection

Scheduled Pinned Locked Moved Android
androidtutorialquestion
30 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Afzaal Ahmad Zeeshan

    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 !~

    P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #7

    I cannot have client-based application on other device because it's cable for otg !!!

    1 Reply Last reply
    0
    • P Pavlex4

      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!!!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #8

      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

      1 Reply Last reply
      0
      • P Pavlex4

        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!!!

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #9

        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.

        P 1 Reply Last reply
        0
        • J Jochen Arndt

          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.

          P Offline
          P Offline
          Pavlex4
          wrote on last edited by
          #10

          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?

          J 1 Reply Last reply
          0
          • P Pavlex4

            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?

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #11

            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.

            P 1 Reply Last reply
            0
            • J Jochen Arndt

              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.

              P Offline
              P Offline
              Pavlex4
              wrote on last edited by
              #12

              My phone is rooted!!!How to get event using udev?

              J 1 Reply Last reply
              0
              • P Pavlex4

                My phone is rooted!!!How to get event using udev?

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #13

                Sorry that I mentioned udev. It is not used by Android.

                P 1 Reply Last reply
                0
                • J Jochen Arndt

                  Sorry that I mentioned udev. It is not used by Android.

                  P Offline
                  P Offline
                  Pavlex4
                  wrote on last edited by
                  #14

                  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?

                  D J 2 Replies Last reply
                  0
                  • P Pavlex4

                    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?

                    J Offline
                    J Offline
                    Jochen Arndt
                    wrote on last edited by
                    #15

                    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.

                    P 2 Replies Last reply
                    0
                    • P Pavlex4

                      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?

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #16

                      Have you tried creating a BroadcastReceiver and registering it with a ACTION_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

                      P 4 Replies Last reply
                      0
                      • D David Crow

                        Have you tried creating a BroadcastReceiver and registering it with a ACTION_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

                        P Offline
                        P Offline
                        Pavlex4
                        wrote on last edited by
                        #17

                        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[^]

                        1 Reply Last reply
                        0
                        • J Jochen Arndt

                          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.

                          P Offline
                          P Offline
                          Pavlex4
                          wrote on last edited by
                          #18

                          How to find modules that send messages inside kernel source?

                          1 Reply Last reply
                          0
                          • D David Crow

                            Have you tried creating a BroadcastReceiver and registering it with a ACTION_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

                            P Offline
                            P Offline
                            Pavlex4
                            wrote on last edited by
                            #19

                            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.

                            J 1 Reply Last reply
                            0
                            • P Pavlex4

                              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.

                              J Offline
                              J Offline
                              Jochen Arndt
                              wrote on last edited by
                              #20

                              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 uses uevent. 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.

                              P 2 Replies Last reply
                              0
                              • J Jochen Arndt

                                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 uses uevent. 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.

                                P Offline
                                P Offline
                                Pavlex4
                                wrote on last edited by
                                #21

                                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

                                L 1 Reply Last reply
                                0
                                • P Pavlex4

                                  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

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #22

                                  Any of them, you need to do some searching.

                                  P 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Any of them, you need to do some searching.

                                    P Offline
                                    P Offline
                                    Pavlex4
                                    wrote on last edited by
                                    #23

                                    I have searched all files and I didn't find kernel message for USB !!!!

                                    1 Reply Last reply
                                    0
                                    • D David Crow

                                      Have you tried creating a BroadcastReceiver and registering it with a ACTION_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

                                      P Offline
                                      P Offline
                                      Pavlex4
                                      wrote on last edited by
                                      #24

                                      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);
                                      #endif

                                      hcd = 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[^]

                                      1 Reply Last reply
                                      0
                                      • J Jochen Arndt

                                        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 uses uevent. 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.

                                        P Offline
                                        P Offline
                                        Pavlex4
                                        wrote on last edited by
                                        #25

                                        I have searched all files and I didn't find kernel message for USB !!!!

                                        1 Reply Last reply
                                        0
                                        • J Jochen Arndt

                                          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.

                                          P Offline
                                          P Offline
                                          Pavlex4
                                          wrote on last edited by
                                          #26

                                          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);
                                          #endif

                                          hcd = 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[^]

                                          J 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups