B4J Tutorial [PyBridge] Accessing MTP devices

1748524625255.png


MTP - Media Transfer Protocol, is an important protocol that wasn't accessible from B4J (https://www.b4x.com/android/forum/t...to-desktop-using-b4j-code.164058/post-1006196).
It is an important protocol as this is the default USB connection method for Android devices, and the alternative methods to transfer files to a PC are not great.
This solution is based on: https://github.com/Heribert17/mtp/tree/main

Installation instructions:
1. Unzip mtp.zip.
2. Create a local Python runtime and click on the "open local Python shell".
3. Run:
B4X:
pip install C:\Users\H\Downloads\mtp-main 'change path!
And:
B4X:
python -m mtp.create_new_comtype_modules_from_wpd_dlls

Connect an Android device to the PC, with the default file transfer mode. Run the attached example.
The Python API documentation is included in the zip file.

The main steps:
1. Find the portable devices:
B4X:
Dim RemoteDevices As PyWrapper = WinAccess.Run("get_portable_devices")
Dim DevicesNames As PyWrapper = Py.Map_(Py.Lambda("dev: (dev.name, dev.serialnumber)"), RemoteDevices).ToList
Wait For (DevicesNames.Fetch) Complete (DevicesNames As PyWrapper)
Dim devices As List = DevicesNames.Value 'each item is an array of objects (name, serial).
2. Call get_content on a remote device to get its "storages".
3. Call get_children on a storage or a folder to get the children items.

The example implements a very simple file manager.
If you know the paths, you can directly access them by calling get_child or get_path.
 

Attachments

  • mtp.zip
    238.1 KB · Views: 91
  • MTPExample.zip
    5.8 KB · Views: 88

Erel

B4X founder
Staff member
Licensed User
Longtime User
BTW, I usually recommend avoiding RuntimePermissions.GetSafeDefaultExternal as in most cases it has no advantages over File.DirInternal (XUI.DefaultFolder), however using MTP you can access the former folder. This means that it can be useful if you need to transfer the mobile files to a connected PC, or upload file to the mobile device.
 
Top