Google Android Nexus 4 and MTP works... almost... [SOLVED]
Linux - MobileThis forum is for the discussion of all topics relating to Mobile Linux. This includes Android, Tizen, Sailfish OS, Replicant, Ubuntu Touch, webOS, and other similar projects and products.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Google Android Nexus 4 and MTP works... almost... [SOLVED]
So my work forced me to get a "smartphone". I scowled and objected, but finally caved in. I absolutely had to do email out on the road.
Well, the fone I got is a Nexus 4, apparently hand crafted by google with Android OS. It's actually pretty cool. I can even make fonecalls
The issue is getting stuff off it when I get home. It seems silly to email 3MB pix to myself when the thing is sitting here connected and charging off USB.
I tried Airdroid, some people praised it. It failed in that "Give Me Your Credit Card Number!" kinda way. adb does nothing. PTP and gphoto make my USB printer get excited but that's about all. Keep in mind I run old uber-hacked Mandrake10 with 2.6.6 kernel, so everything I do is compiled from source.
When I plug the fone in, log in as root, and go
Code:
mtp-files
sure enough, I get a file listing. But only once. After that, it's hang.......... until time-out, on any mtp command. I can only ever pull off *one* mtp command. I admit I have not investigated libmtp too much(yet).
What I discovered is that if I go to the fone's "USB computer connection" screen, it shows:
Code:
CONNECT AS
Media device (MTP) (check)
Camera (PTP) (no check)
If I tap that "Media device (MTP)" deelybob twice (eg. turn it off and then back on) my PC disconnects and reconnects, the devnum increments by one, and I can pull another mtp-* command. So, in order to retrieve 14 pix (and it has a very good camera) I use a script like this:
Code:
#!/bin/sh
if [ $UID -ne 0 ]; then
echo "You must be root to run this script."
exit 1
fi
function do_exit () {
echo -e "\nDone."
chown miven *
chgrp miven *
exit 2
}
trap do_exit 2
function readit () {
read -p'Reset USB Computer Connection and hit <ENTER> ?'
}
readit
mtp-files > files
grep -1 'IMG.*jpg' files | grep -v 'File size' > jpgs
cat jpgs | \
sed -e "s/^ *//" \
-e "s/File ID: //" \
-e "s/Filename: //" \
-e "s/--/|/" | \
tr '\n' ' ' | \
tr '|' '\n' \
> cmds
IFS=$'\n'
for line in $(cat cmds); do
cmd="mtp-getfile $line"
echo "-> $cmd"
readit
eval $cmd
done
do_exit
This allows me to do the tap-dance on the Android MTP check box in between every mtp call. This works. But, of course, it sucks.
I suppose my question is this:
Is there a way of forcing reset on the USB connection on the PC end, maybe a way of hacking libmtp, or is this completely on the Android end?
I might try some of those apps, but I think I'm onto a nice solution involving stuff I already have:
It appears to me that libmtp goes wrong with the android in that it opens the usb connection, does something, then closes it. Android doesn't like this. It likes to open a *session*, do everything, and I'm not so sure it even cares about closing the session. This is mtp after all. It's designed for teenage girls who just want to slap a few tunes/pix on/off their fone and then yank it out and run. None of that courtesy mount/unmount stuff here.
I'm going to have a go a creating a modified mtp-connect, that opens the connection, and waits in a loop for instructions (getfile, files, filetree, delfile, etc), then closes when I'm finally done. Looking at the code, it shouldn't be much of a stretch.
It's a shame that android discontinue usb mass storage in place of MTP. Usb mass storage did what we wanted, connect our android device and then drop and drag our files to it. Those were the good ol' days...
It's a shame that android discontinue usb mass storage in place of MTP. Usb mass storage did what we wanted, connect our android device and then drop and drag our files to it. Those were the good ol' days...
I hacked a version of mtp-connect, called it mtp-connect-android, and it runs in a loop, kinda like fdisk or something. I type "f <ENTER>" it shows the file listing of the device. Type "g 123" and it grabs File ID 123 and dumps it in the current directory as it's proper filename, eg "IMG_20131229_123112.jpg" or whatever.
I have to be logged in as root, but I can make as many mtp calls as I like without having to reset the device or USB connection. Awesome. It's pretty crude still, the code needs a lot of work, but it does work and proves my theory.
BTW, the reason these devices cannot be mounted as USB Mass Storage is so that they can continue working while plugged in. Ever remember mp3 players and stuff that essentially went dead when plugged in? You couldn't listen to music while charging? That's why. MTP allows the device to continue doing it's own thing while also allowing outside connection. Kinda like FTP server or something.
I'm slowly smoothing this hack over. I can get ranges like
g 145 153-157
and it will get file 145 and all the ones from 153 through 157, dump them in CWD.
The beauty of it is that I have 2 send modes:
s ~/bin/something 1
will send ~/bin/something to folder 1 (Music). But if I go
s 1
it notices the lack of from_path but catches the to_path, and sits in loop, so I can paste a huge chunk from
find /mnt/sda1/audio/mp3/C/ -name '*' | sort | less
into it. And it works! It chews the buffered terminal input (rxvt) and makes real send commands that work, and I get my Chopin and Corb Lund into the folder 1 on my device. It's fast too.
Life is good. I highly doubt that *doze stuff works this good. And Apps, well, there's a gajillion of them, and they never do what I want because they try to anticipate my needs, and always fail because I'm an old school hacker. There's a SMS and MMS grabber I use, and a feeble File Manager deelybob that seems to work, so far that's it for my Apps.
Simple MTP *works* with Android Nexus4. You just have to be crafty. In libmtp, you might notice that all of the mtp-* progs are in the dir /examples. That's great. They're examples of how the sh*t works. Learn. I have. Everyone wants stuff spoon fed, but, with all the changing tech, how can this be?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.