42mtp Module

42.1Overview

The mtp module provides measures to read/write data on a mobile platform like an Android device.

Below is an example that lists up files in the top directory of the device that is detected at first:

import(mtp)

devices = mtp.detect_devices()
device = devices[0]
println('Device: ', device.friendlyname)
storage = device.storages[0]
path.dir(storage.opendir('/')):stat {|stat|
    tm = stat.mtime
    if (stat.isdir) {
        printf('%04d/%02d/%02d %02d:%02d %12s  %s\n',
               tm.year, tm.month, tm.day, tm.hour, tm.min, '<dir>', stat.filename)
    } else {
        printf('%04d/%02d/%02d %02d:%02d %12d  %s\n',
               tm.year, tm.month, tm.day, tm.hour, tm.min, stat.size, stat.filename)
    }
}

42.2Module Function

mtp.detect_devices() {block?}
Detects MTP devices and returns a list of mtp.device instances.

42.3mtp.device Class

42.3.1Overview

The mtp.device class provides information such as device's friendly name and manufacturer name. It also provides a list of mtp.storage instances through which you can transfer and manipulate files on the remote device.

You can call mtp.detect_devices() to get a list of mtp.device instances that are associated to connected devices.

42.3.2Property

An mtp.device instance has the following properties:

mtp.device#friendlynamestring [read-only]
Friendly name.
mtp.device#manufacturerstring [read-only]
Manufacturer name.
mtp.device#storageslist [read-only]
Returns a list of mtp.storage instances.

42.4mtp.storage Class

42.4.1Overview

The class mtp.storage provices methods to transfer files from/to a device and to list and manipulate files on a device.

42.4.2Property

An mtp.storage instance has following properties:

mtp.storage#access_capabilitysymbol [read-only]

Indicates what access is permitted to the storage by following symbols:

  • `ReadWrite .. Read/write capable.
  • `ReadOnly .. Read-only.
  • `ReadOnlyWithObjectDeletion .. Read-only but deleting operation is permitted.
mtp.storage#free_space_in_bytesnumber [read-only]
Returns the free space in the storage in bytes.
mtp.storage#free_space_in_objectsnumber [read-only]
Returns the free space in the storage in number of objects.
mtp.storage#max_capacitynumber [read-only]
Returns the maximum capacity of the storage in bytes.
mtp.storage#storage_descriptionstring [read-only]
Returns the storage description.
mtp.storage#storage_typesymbol [read-only]

Indicates the type of the storage by following symbols:

  • `Undefined .. Undefined type.
  • `FixedROM .. Non-removable and read-only.
  • `RemovableROM .. Removable and read-only.
  • `FixedRAM .. Non-removable and read/write capable.
  • `RemovableRAM .. Removable and read/write capable.
mtp.storage#volume_identifierstring [read-only]
Returns the volume identifier.

42.4.3Method

The mtp.storage class has following methods:

mtp.storage#opendir(pathname:string) {block?}

Creates a directory instance that can be passed to functions that browse directories such as path.dir() and path.walk(). The argument pathname specifies the name of a directory on the device.

If block is specified, it would be evaluated with a block parameter |directory:directory|, where directory is the created instance. In this case, the block's result would become the function's returned value.

mtp.storage#recvfile(pathname:string, stream:stream:w):reduce {block?}
Receives the content of a file on the device that is specified by the argument pathname and writes it to stream. If block is specified, it would be evaluated during the receiving process with a block parameter of |recv:number, total:number| where recv is a number of bytes that has been received and total is that of the total size. This functions returns the reference of the target.
mtp.storage#remove(pathname:string):reduce
Removes a file on the device that is specified by the argument pathname. This functions returns the reference of the target.
mtp.storage#sendfile(pathname:string, stream:stream:r):reduce {block?}
Reads data from stream and sends it to the device as a file that is specified by the argument pathname. If block is specified, it would be evaluated during the receiving process with a block parameter of |sent:number, total:number| where sent is a number of bytes that has been sent and total is that of the total size. This functions returns the reference of the target.

42.5mtp.stat Class

42.5.1Overview

The mtp.stat class provides information about filename, timestamp and size of files on a portable device. An instance of mtp.stat is generated by calling path.dir() and path.walk() with a directory instance created by mtp.storage#opendir() for the argument and :stat attribute appended.

42.5.2Property

A mtp.stat instance has following properties:

mtp.stat#dirnamestring [read-only]
Directory name.
mtp.stat#filenamestring [read-only]
Filename.
mtp.stat#isdirboolean [read-only]
Returns true for a directory and false for a file.
mtp.stat#mtimedatetime [read-only]
Returns a datetime instance indicating the modification time stamp.
mtp.stat#pathnamestring [read-only]
Path name.
mtp.stat#sizenumber [read-only]
File size in bytes.

42.6Thanks

This module uses libusb and libmtp library which is distributed in the following site: