<ul class="toc">
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#find_open">Finding and Opening a Device</a></li>
+ <li><a href="#usb_detection">Monitoring Device Connections</a></li>
<li><a href="#find_endpoint">Finding the Interface and Endpoints</a></li>
<li><a href="#transfer">Transferring Data</a></li>
</ul>
<p>You can set up your application for the USB host functionality by initializing the interface context.</p>
</li>
<li>Finding and opening a device through a device list
-<p>To use the USB host functionality, you have to <a href="#find_open">find a device and gather information about it</a>.</p>
+<p>To use the USB host functionality, you have to <a href="#find_open">find a device and gather information about it</a>. You can also <a href="#usb_detection">monitor when a USB device is attached or detached</a>.</p>
</li>
<li>Finding interface and endpoints
<p>You can <a href="#find_endpoint">find the correct interface and configuration</a> to connect to a device using USB.</p></li>
</li>
</ol>
+ <h2 id="usb_detection">Monitoring Device Connections</h2>
+To receive notifications when a USB device is attached or detached:
+<ol>
+<li>Define and register a callback, which is called when the device is attached or detached.
+<p>Define the events you want to monitor in the third parameter of the <code>usb_host_set_hotplug_cb()</code> function. The available values are: <code>USB_HOST_HOTPLUG_EVENT_ATTACH</code>, <code>USB_HOST_HOTPLUG_EVENT_DETACH</code>, and <code>USB_HOST_HOTPLUG_EVENT_ANY</code>.</p>
+<pre class="prettyprint">
+void
+detected_cb(usb_host_device_h ndev, void *data)
+{
+ dlog_print(DLOG_INFO, LOG_TAG, "USB is attached or detached");
+}
+
+int ret;
+usb_host_hotplug_h hotplug;
+
+ret = usb_host_set_hotplug_cb(ctx, detected_cb, USB_HOST_HOTPLUG_EVENT_ANY, NULL, &hotplug);
+if (ret == USB_HOST_ERROR_NONE)
+ dlog_print(DLOG_INFO, LOG_TAG, "USB callback is successfully registered");
+else
+ dlog_print(DLOG_INFO, LOG_TAG, "Unable to register USB callback function");
+</pre>
+</li>
+<li>When the notifications are no longer needed, deregister the callback:
+<pre class="prettyprint">
+usb_host_unset_hotplug_cb(hotplug);
+</pre>
+</li>
+</ol>
+
<h2 id="find_endpoint">Finding the Interface and Endpoints</h2>
<p>USB communication always happens between the host and one of the endpoints. The endpoints are grouped into interfaces and interfaces into configurations. A device can have multiple configurations, but only one can be active at a time. All interfaces inside an active configuration can be used at the same time.</p>