USB Hotplug Detection in Rust on macOS — Reacting to Device Connect/Disconnect

rust dev.to

All tests run on an 8-year-old MacBook Air. All results from shipping 7 Mac apps as a solo developer. No sponsored opinion.

HiyokoAutoSync and HiyokoMTP both react instantly when an Android device is connected. No polling. No "refresh" button. Just plug in and it works. Here's how USB hotplug detection works in Rust on macOS.


The approach: nusb hotplug API

nusb provides a hotplug watch API that wraps macOS's IOKit USB notifications:

use nusb::hotplug::{HotplugEvent, HotplugWatch};

fn watch_usb_devices(app_handle: AppHandle) {
    std::thread::spawn(move || {
        let watch = nusb::watch_devices().expect("Failed to start USB watch");

        for event in watch {
            match event {
                HotplugEvent::Connected(device_info) => {
                    if is_android_device(&device_info) {
                        app_handle.emit("device-connected", DeviceInfo {
                            name: device_info.product_string().unwrap_or_default(),
                            vendor_id: device_info.vendor_id(),
                            product_id: device_info.product_id(),
                        }).ok();
                    }
                }
                HotplugEvent::Disconnected(device_info) => {
                    if is_android_device(&device_info) {
                        app_handle.emit("device-disconnected", ()).ok();
                    }
                }
            }
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

The watch runs in a dedicated thread — it's a blocking iterator. Emit Tauri events to notify the frontend.


Identifying Android devices

Android devices have well-known vendor IDs. A non-exhaustive list:

const ANDROID_VENDOR_IDS: &[u16] = &[
    0x18D1, // Google
    0x04E8, // Samsung
    0x22D9, // OPPO/OnePlus
    0x2717, // Xiaomi
    0x12D1, // Huawei
    0x0BB4, // HTC
    0x1004, // LG
    0x0FCE, // Sony
];

fn is_android_device(info: &nusb::DeviceInfo) -> bool {
    ANDROID_VENDOR_IDS.contains(&info.vendor_id())
}
Enter fullscreen mode Exit fullscreen mode

This catches most Android devices. ADB provides a more reliable check, but vendor ID filtering is fast and works before ADB connects.


ADB confirmation

After detecting a USB device with a known Android vendor ID, confirm with ADB:

async fn confirm_adb_device() -> bool {
    let output = Command::new("adb")
        .args(["devices", "-l"])
        .output()
        .await;

    match output {
        Ok(out) => {
            let stdout = String::from_utf8_lossy(&out.stdout);
            stdout.lines()
                .skip(1) // skip "List of devices attached"
                .any(|line| line.contains("device"))
        }
        Err(_) => false,
    }
}
Enter fullscreen mode Exit fullscreen mode

Vendor ID for fast detection, ADB for confirmation. Two-step keeps the UX instant while ensuring accuracy.


The frontend experience

The user plugs in their Android device. Within 1-2 seconds:

  1. USB hotplug fires
  2. ADB confirms the device
  3. Frontend receives device-connected event
  4. UI updates to show the device and enable sync

No button. No refresh. It just works.


TL;DR: Use nusb::watch_devices() in a dedicated thread to get IOKit USB hotplug events on macOS. Filter by vendor ID for instant detection, then confirm with adb devices for accuracy. Emit Tauri events to the frontend — plug in and it works within 1-2 seconds, no refresh button needed.


If this was useful, a ❤️ helps more than you'd think — thanks!

HiyokoAutoSync | X → @hiyoyok

Source: dev.to

arrow_back Back to Tutorials