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?}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#friendlyname … string [read-only]mtp.device#manufacturer … string [read-only]mtp.device#storages … list [read-only]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_capability … symbol [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_bytes … number [read-only]mtp.storage#free_space_in_objects … number [read-only]mtp.storage#max_capacity … number [read-only]mtp.storage#storage_description … string [read-only]mtp.storage#storage_type … symbol [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_identifier … string [read-only]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?}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):reducepathname. This functions returns the reference of the target.
mtp.storage#sendfile(pathname:string, stream:stream:r):reduce {block?}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#dirname … string [read-only]mtp.stat#filename … string [read-only]mtp.stat#isdir … boolean [read-only]true for a directory and false for a file.
mtp.stat#mtime … datetime [read-only]datetime instance indicating the modification time stamp.
mtp.stat#pathname … string [read-only]mtp.stat#size … number [read-only]42.6Thanks
This module uses libusb and libmtp library which is distributed in the following site: