LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Mobile (https://www.linuxquestions.org/questions/linux-mobile-81/)
-   -   Google Android Nexus 4 and MTP works... almost... [SOLVED] (https://www.linuxquestions.org/questions/linux-mobile-81/google-android-nexus-4-and-mtp-works-almost-%5Bsolved%5D-4175489937/)

mdooligan 01-02-2014 06:12 PM

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?

Peace and Cheer.

schneidz 01-02-2014 06:59 PM

seems dependent on human intervention.

try sshdroid; i think you have to be rooted.

else you can scp from your fone to your pc using andftp.

HyperX 01-02-2014 11:00 PM

@ mdooligan

I use Airdroid and it never ask for a credit card at any time and it works great.

Back on topic, have you tried File Expert HD? it's a cool file manager with tools to manage your files and it can act as a server and client. And it's FREE!
https://play.google.com/store/apps/d...?id=xcxin.fehd

File Expert HD has a built-in ftp server and you can try file transfers and see if it works for you.

mdooligan 01-03-2014 11:15 AM

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.

Peace and Cheer.

HyperX 01-03-2014 11:53 AM

Ok, I understand :)

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...

schneidz 01-03-2014 11:57 AM

Quote:

Originally Posted by HyperX (Post 5091262)
Ok, I understand :)

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'm still holding onto my galaxy-s2...

HyperX 01-03-2014 12:01 PM

Cool :)

mdooligan 01-04-2014 10:22 AM

Update
 
My technique works.

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.

Peace and Cheer.

mdooligan 01-10-2014 10:39 PM

Update:

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?

Peace and Cheer.


All times are GMT -5. The time now is 10:53 PM.