pub struct MtpDevice { /* private fields */ }Expand description
An MTP device connection.
This is the main entry point for interacting with MTP devices.
Use MtpDevice::open_first() to connect to the first available device,
or MtpDevice::builder() for more control.
§Example
use mtp_rs::mtp::MtpDevice;
// Open the first MTP device
let device = MtpDevice::open_first().await?;
println!("Connected to: {} {}",
device.device_info().manufacturer,
device.device_info().model);
// Get storages
for storage in device.storages().await? {
println!("Storage: {} ({} free)",
storage.info().description,
storage.info().free_space_bytes);
}Implementations§
Source§impl MtpDevice
impl MtpDevice
Sourcepub fn builder() -> MtpDeviceBuilder
pub fn builder() -> MtpDeviceBuilder
Create a builder for configuring device options.
Sourcepub async fn open_first() -> Result<Self, Error>
pub async fn open_first() -> Result<Self, Error>
Open the first available MTP device with default settings.
Sourcepub async fn open_by_location(location_id: u64) -> Result<Self, Error>
pub async fn open_by_location(location_id: u64) -> Result<Self, Error>
Open a device at a specific USB location (port) with default settings.
Use list_devices() to get available location IDs.
Sourcepub async fn open_by_serial(serial: &str) -> Result<Self, Error>
pub async fn open_by_serial(serial: &str) -> Result<Self, Error>
Open a device by its serial number with default settings.
This identifies a specific physical device regardless of which USB port it’s connected to.
Sourcepub fn list_devices() -> Result<Vec<MtpDeviceInfo>, Error>
pub fn list_devices() -> Result<Vec<MtpDeviceInfo>, Error>
List all available MTP devices without opening them.
Sourcepub fn device_info(&self) -> &DeviceInfo
pub fn device_info(&self) -> &DeviceInfo
Get device information.
Sourcepub fn supports_rename(&self) -> bool
pub fn supports_rename(&self) -> bool
Check if the device supports renaming objects.
This checks for support of the SetObjectPropValue operation (0x9804), which is required to rename files and folders via the ObjectFileName property.
§Returns
Returns true if the device advertises SetObjectPropValue support.
Sourcepub async fn storage(&self, id: StorageId) -> Result<Storage, Error>
pub async fn storage(&self, id: StorageId) -> Result<Storage, Error>
Get a specific storage by ID.
Sourcepub async fn get_object_handles(
&self,
storage_id: StorageId,
parent: Option<ObjectHandle>,
) -> Result<Vec<ObjectHandle>, Error>
pub async fn get_object_handles( &self, storage_id: StorageId, parent: Option<ObjectHandle>, ) -> Result<Vec<ObjectHandle>, Error>
Get object handles in a storage.
§Arguments
storage_id- Storage to search, orStorageId::ALLfor all storagesparent- Parent folder handle, orNonefor root level only, orSome(ObjectHandle::ALL)for recursive listing
Sourcepub async fn next_event(&self) -> Result<DeviceEvent, Error>
pub async fn next_event(&self) -> Result<DeviceEvent, Error>
Receive the next event from the device.
This method waits for an event on the USB interrupt endpoint, returning when an event arrives or the event timeout expires (default: 200ms). The short default timeout allows responsive event loops without blocking other operations.
Configure the timeout via MtpDeviceBuilder::event_timeout().
§Returns
Ok(event)- An event was received from the deviceErr(Error::Timeout)- No event within the timeout period (normal, keep polling)Err(Error::Disconnected)- Device was disconnectedErr(_)- Other communication error
§Example
// Configure event timeout if needed (default is 200ms)
let device = MtpDevice::builder()
.event_timeout(Duration::from_millis(100))
.open_first()
.await?;
loop {
match device.next_event().await {
Ok(event) => {
match event {
DeviceEvent::ObjectAdded { handle } => {
println!("New object: {:?}", handle);
}
DeviceEvent::StoreRemoved { storage_id } => {
println!("Storage removed: {:?}", storage_id);
}
_ => {}
}
}
Err(Error::Disconnected) => break,
Err(Error::Timeout) => continue, // No event, keep polling
Err(e) => {
eprintln!("Error: {}", e);
break;
}
}
}