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.
  • 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
            • 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
              #18

              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
              • 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
                #19

                How to find modules that send messages inside kernel source?

                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
                              • P Pavlex4

                                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 Offline
                                J Offline
                                Jochen Arndt
                                wrote on last edited by
                                #27

                                The above code seems to be executed when activating and deactivating the host mode and calls the msm_otg_host_notify function when enabled by build configuration. So it might be useful to inspect this function. But please understand that I don't have the time and interest to dig into this further (especially because it would also require to get the sources first). I pointed you to directions that might help to solve the problem. But I'm not going to do the research for you.

                                P 2 Replies Last reply
                                0
                                • J Jochen Arndt

                                  The above code seems to be executed when activating and deactivating the host mode and calls the msm_otg_host_notify function when enabled by build configuration. So it might be useful to inspect this function. But please understand that I don't have the time and interest to dig into this further (especially because it would also require to get the sources first). I pointed you to directions that might help to solve the problem. But I'm not going to do the research for you.

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

                                  Can this be helpful.It's from sec-switch.c file!

                                  #ifdef CONFIG_USB_HOST_NOTIFY
                                  } else if (usb_mode == USB_OTGHOST_DETACHED
                                  || usb_mode == USB_OTGHOST_ATTACHED) {

                                  	if (usb\_mode == USB\_OTGHOST\_DETACHED) {
                                  		pr\_info("USB Host detached");
                                  		sec\_otg\_notify(HNOTIFY\_ID\_PULL);
                                  	} else {
                                  		pr\_info("USB Host attached");
                                  		sec\_otg\_notify(HNOTIFY\_ID);
                                  	}
                                  
                                  } else if (usb\_mode == USB\_POWERED\_HOST\_DETACHED
                                  	|| usb\_mode == USB\_POWERED\_HOST\_ATTACHED) {
                                  	if (usb\_mode == USB\_POWERED\_HOST\_DETACHED){
                                  		pr\_info("USB Host HNOTIFY\_SMARTDOCK\_OFF");
                                  		sec\_otg\_notify(HNOTIFY\_SMARTDOCK\_OFF);
                                  	}else{
                                  		pr\_info("USB Host HNOTIFY\_SMARTDOCK\_ON");
                                  		sec\_otg\_notify(HNOTIFY\_SMARTDOCK\_ON);
                                  	}
                                  

                                  #endif
                                  }
                                  }

                                  or this file sm5502.c :

                                  #if defined(CONFIG_USB_HOST_NOTIFY)
                                  if (adc == 0x11 || adc == ADC_AUDIO_DOCK) {
                                  val2 = DEV_AUDIO_DOCK;
                                  val1 = 0;
                                  }
                                  #endif
                                  dev_err(&client->dev,
                                  "dev1: 0x%x,dev2: 0x%x,dev3: 0x%x,Carkit: 0x%x,ADC: 0x%x,Jig: %s\n",
                                  val1, val2, val3, val4, adc,
                                  (check_sm5502_jig_state() ? "ON" : "OFF"));

                                  /\* USB \*/
                                  if (val1 & DEV\_USB || val2 & DEV\_T2\_USB\_MASK ||
                                  		val4 & DEV\_CARKIT\_CHARGER1\_MASK) {
                                  	pr\_info("\[MUIC\] USB Connected\\n");
                                  	pdata->callback(CABLE\_TYPE\_USB, SM5502\_ATTACHED);
                                  /\* USB\_CDP \*/
                                  } else if (val1 & DEV\_USB\_CHG) {
                                  	pr\_info("\[MUIC\] CDP Connected\\n");
                                  	pdata->callback(CABLE\_TYPE\_CDP, SM5502\_ATTACHED);
                                  /\* UART \*/
                                  } else if (val1 & DEV\_T1\_UART\_MASK || val2 & DEV\_T2\_UART\_MASK) {
                                  	uart\_sm5502\_connecting = 1;
                                  	pr\_info("\[MUIC\] UART Connected\\n");
                                  	i2c\_smbus\_write\_byte\_data(client, REG\_MANUAL\_SW1, SW\_UART);
                                  	if(vbus & DEV\_VBUSIN\_VALID)
                                  		pdata->callback(CABLE\_TYPE\_JIG\_UART\_OFF\_VB, SM5502\_ATTACHED);
                                  	else
                                  		pdata->callback(CABLE\_TYPE\_UARTOFF, SM5502\_ATTACHED);
                                  

                                  #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
                                  flash_control(true);
                                  #endif
                                  /* CHARGER */
                                  } else if ((val1 & DEV_T1_CHARGER_MASK) ||
                                  (val3 & DEV_T3_CHARGER_MASK)) {
                                  pr_info("[MUIC] Charger Connected\n");
                                  pdata->callback(CABLE_TYPE_AC, SM5502_ATTACHED);
                                  #if defined(CONFIG_USB_HOST_NOTIFY)
                                  /* for SAMSUNG OTG */
                                  } else if (val1 & DEV_USB_OTG && adc == ADC_OTG) {
                                  pr_info("[MUIC] OTG Connected\n");

                                  #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
                                  sm5502_enable_rawdataInterrupts(usbsw);
                                  usbsw->dock_attached = SM5502_ATTACHED;
                                  usbsw->previous_dock = ADC_OTG;
                                  #endif
                                  sm5502_set_otg(usbsw, SM5502_ATTACHED);
                                  pdata->callback(CABLE_TYPE_OTG, SM5502_ATTACHED);
                                  #endif
                                  /* JIG */

                                  1 Reply Last reply
                                  0
                                  • J Jochen Arndt

                                    The above code seems to be executed when activating and deactivating the host mode and calls the msm_otg_host_notify function when enabled by build configuration. So it might be useful to inspect this function. But please understand that I don't have the time and interest to dig into this further (especially because it would also require to get the sources first). I pointed you to directions that might help to solve the problem. But I'm not going to do the research for you.

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

                                    How to use app to detect when otg cable is plugged in and when it's plugged out? Is there intent for otg cable like this for usb devices: "android.hardware.usb.action.USB_DEVICE_ATTACHED" I have created app like this but I only detects flash drive not otg cable:

                                    public class MainActivity extends AppCompatActivity
                                    {

                                    private TextView mInfo;
                                    private Logger mLogger;
                                    private HashMap mHashMap = new HashMap();
                                    private UsbManager mUsbManager;
                                    private PendingIntent mPermissionIntent;
                                    
                                    @Override
                                    protected void onCreate(Bundle savedInstanceState)
                                    {
                                        super.onCreate(savedInstanceState);
                                        setContentView(R.layout.activity\_main);
                                    
                                        mInfo = (TextView)findViewById(R.id.log);
                                    
                                        mLogger = new Logger(this);
                                        mLogger.setMode(Logger.MODE\_TOAST);
                                    
                                        mUsbManager = (UsbManager) getSystemService(Context.USB\_SERVICE);
                                    
                                        usbConnection();
                                    }
                                    
                                    private void usbConnection() {
                                        IntentFilter filter = new IntentFilter(UsbManager.ACTION\_USB\_DEVICE\_ATTACHED);
                                        registerReceiver(mUsbAttachReceiver , filter);
                                        filter = new IntentFilter(UsbManager.ACTION\_USB\_DEVICE\_DETACHED);
                                        registerReceiver(mUsbDetachReceiver , filter);
                                    
                                        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION\_USB\_PERMISSION), 0);
                                        filter = new IntentFilter(ACTION\_USB\_PERMISSION);
                                        registerReceiver(mUsbReceiver, filter);
                                    
                                        showDevices();
                                    }
                                    
                                    protected void onDestroy() {
                                        super.onDestroy();
                                        unregisterReceiver(mUsbDetachReceiver);
                                        unregisterReceiver(mUsbAttachReceiver);
                                        unregisterReceiver(mUsbReceiver);
                                    };
                                    
                                    BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver() {
                                        public void onReceive(Context context, Intent intent) {
                                            String action = intent.getAction();
                                    
                                            if (UsbManager.ACTION\_USB\_DEVICE\_DETACHED.equals(action)) {
                                                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA\_DEVICE);
                                                if (device != null) {
                                                    // call your method that cleans up and closes communication with the device
                                                    UsbDataBinder binder = mHashMap.get(device);
                                                    if (binder != null) {
                                                        binder.onDestroy();
                                                        mHashMap.remove(device);
                                                        Toast.makeText(MainActivity.this, "Attached!", Toast.LENG
                                    
                                    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
                                      #30

                                      Go to ParentHow to use app to detect when otg cable is plugged in and when it's plugged out? Is there intent for otg cable like this for usb devices: "android.hardware.usb.action.USB_DEVICE_ATTACHED" I have created app like this but I only detects flash drive not otg cable:

                                      public class MainActivity extends AppCompatActivity
                                      {

                                      private TextView mInfo;
                                      private Logger mLogger;
                                      private HashMap mHashMap = new HashMap();
                                      private UsbManager mUsbManager;
                                      private PendingIntent mPermissionIntent;
                                      
                                      @Override
                                      protected void onCreate(Bundle savedInstanceState)
                                      {
                                          super.onCreate(savedInstanceState);
                                          setContentView(R.layout.activity\_main);
                                      
                                          mInfo = (TextView)findViewById(R.id.log);
                                      
                                          mLogger = new Logger(this);
                                          mLogger.setMode(Logger.MODE\_TOAST);
                                      
                                          mUsbManager = (UsbManager) getSystemService(Context.USB\_SERVICE);
                                      
                                          usbConnection();
                                      }
                                      
                                      private void usbConnection() {
                                          IntentFilter filter = new IntentFilter(UsbManager.ACTION\_USB\_DEVICE\_ATTACHED);
                                          registerReceiver(mUsbAttachReceiver , filter);
                                          filter = new IntentFilter(UsbManager.ACTION\_USB\_DEVICE\_DETACHED);
                                          registerReceiver(mUsbDetachReceiver , filter);
                                      
                                          mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION\_USB\_PERMISSION), 0);
                                          filter = new IntentFilter(ACTION\_USB\_PERMISSION);
                                          registerReceiver(mUsbReceiver, filter);
                                      
                                          showDevices();
                                      }
                                      
                                      protected void onDestroy() {
                                          super.onDestroy();
                                          unregisterReceiver(mUsbDetachReceiver);
                                          unregisterReceiver(mUsbAttachReceiver);
                                          unregisterReceiver(mUsbReceiver);
                                      };
                                      
                                      BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver() {
                                          public void onReceive(Context context, Intent intent) {
                                              String action = intent.getAction();
                                      
                                              if (UsbManager.ACTION\_USB\_DEVICE\_DETACHED.equals(action)) {
                                                  UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA\_DEVICE);
                                                  if (device != null) {
                                                      // call your method that cleans up and closes communication with the device
                                                      UsbDataBinder binder = mHashMap.get(device);
                                                      if (binder != null) {
                                                          binder.onDestroy();
                                                          mHashMap.remove(device);
                                                          Toast.makeText(MainActivity.this, "Attached!"
                                      
                                      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