Android USB Detection
-
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[^]
-
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[^]
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. -
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.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 */ -
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.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
-
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
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!"