Skip to main content

MtpDevice

Struct MtpDevice 

Source
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

Source

pub fn builder() -> MtpDeviceBuilder

Create a builder for configuring device options.

Source

pub async fn open_first() -> Result<Self, Error>

Open the first available MTP device with default settings.

Source

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.

Source

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.

Source

pub fn list_devices() -> Result<Vec<MtpDeviceInfo>, Error>

List all available MTP devices without opening them.

Source

pub fn device_info(&self) -> &DeviceInfo

Get device information.

Source

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.

Source

pub async fn storages(&self) -> Result<Vec<Storage>, Error>

Get all storages on the device.

Source

pub async fn storage(&self, id: StorageId) -> Result<Storage, Error>

Get a specific storage by ID.

Source

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, or StorageId::ALL for all storages
  • parent - Parent folder handle, or None for root level only, or Some(ObjectHandle::ALL) for recursive listing
Source

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 device
  • Err(Error::Timeout) - No event within the timeout period (normal, keep polling)
  • Err(Error::Disconnected) - Device was disconnected
  • Err(_) - 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;
        }
    }
}
Source

pub async fn close(self) -> Result<(), Error>

Close the connection (also happens on drop).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.