Re: Okay, I give up… how to connect my (Android) phone properly (MTP)?

Johnny Rosenberg gurus.knugum at gmail.com
Sun Aug 2 16:36:31 UTC 2015


2014-07-07 21:31 GMT+02:00 Johnny Rosenberg <gurus.knugum at gmail.com>:
> My old operating system, Ubuntu 12.04, didn't have MTP support built in,
so
> I had to install some backport thing to make it work.
> Now, I have Ubuntu 14.04, which seem to have MTP support built in. Fine,
so
> far. But what do I need to do to make it work properly? In 12.04 my phone
> always ended up at the same place (~/.gvfs/mtp/). Very good. Fantastic.
> In 14.04 however, it does NOT! Today it ended up at
> ”/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C008%5D/”, but the exact path
is
> different each time, which breaks a lot of things, such as some (bash)
> scripts that copy some files to the phone – and I use those a lot. Or at
> least I did when they worked…
>
> So what do I need to do to make sure my phone always end up at exactly the
> same place using the exact same name?
>
>
> Johnny Rosenberg

This seems to be a question that nobody knows the answer to; I've asked it
on several places, over and over, and never got a reply back what so ever,
as far as I know.

So I did my own workaround. Now, since I'm not very good at this, I would
just ask here to see if anybody have a better idea or if anyone can tell me
why I shouldn't do this and what I should do instead.

So what I did, was a Bash script that looks for the phone every ten
seconds. If it is found, a soft link to it is created at ${HOME}, called ”
Nexus” (yes, this script only works with Nexus phones and only for one at a
time, which is okay with me but maybe not so okay with others).

My script looks for my phone's ID with ”lsusb” and if it find it, it
creates a link to it. Then it continues looking every ten seconds. If
nothing is changed, it waits for ten more seconds and so on.
If something changes, for instance if the phone no longer can be found, but
it was found earlier, the link is removed. If it is the other way around,
the phone was previously not connected, but now it is, then a new link is
created. Before creating a link, it looks for a link with the same name and
delete it if there is one to delete. It will probably be broken anyway,
since the target is different every time the phone is corrected.

Every time something new happens, a Yad dialogue (or rather a monologue in
this case, since they have no buttons, except the upper left close button)
telling the user that the phone was connected or disconnected. The dialogue
disappears after 5 seconds.

My point in doing this is that there is now a link somewhere, in my case
called ~/Nexus, that points to my phone, that solves some inconveniences
that I have with some applications (such as KeyPassX, for instance).

The script needs Yad (Yet Another Dialogue) to run properly. It should
however be fairly easy to rewrite it for Zenity instead, I think. If so,
there should only be one file to rewrite: ”~/bin/DialogueFunctions.sh”, see
below.

The last step was to add the script to the list of applications that starts
when logging in to Ubuntu, making sure it will always run (unless it crash,
of course…).

So, below is my script.
~/bin/NexusLinkMaintainer.sh:
#!/bin/bash

# Create a link to the connected Nexus phone in the HOME directory.

# External functions
———————————————————————————————————————————————————————————
source NexusPath.sh # For the NexusPath function.
source DialogueFunctions.sh # For some Yad-dialogues.

# Variables
————————————————————————————————————————————————————————————————————
LastErrorCode=1
LinkName="${HOME}/Nexus"

# Main
—————————————————————————————————————————————————————————————————————————
while true; do

PathToNexus=$(NexusPath)
ErrorCode=$?
if ((ErrorCode!=LastErrorCode)); then

LastErrorCode=${ErrorCode}

[[ -L ${LinkName} ]] && rm "${LinkName}"

if ((ErrorCode!=0)); then

TargetDisconnected "The phone" &

else

TargetConnected "The phone" &

ln -s "${PathToNexus}" "${LinkName}"

if (($?!=0)); then

Error "The link could not be created." "Link problem"

exit 1

fi

fi

fi
sleep 10

done

~/bin/NexusPath.sh:
#!/bin/bash

NexusPath () {
# Declare local variables
local ID="18d1:4ee1"
local Description
Description=$(lsusb -d "${ID}")
ErrorCode=$?
if ((ErrorCode!=0)); then
return ${ErrorCode}
else
local Bus=$(echo "${Description}" | awk '{print $2}')
local Dev=$(echo "${Description}" | awk '{print $4}' | sed 's/://')
local P="${XDG_RUNTIME_DIR}/gvfs/mtp:host=%5Busb%3A${Bus}%2C${Dev}%5D"

# Print the answer
printf "%s\n" "${P}"
fi
}

~/bin/DialogueFunctions.sh (contains quite a few functions, but only those
below are needed):
# Variables (in alphabetic order)
——————————————————————————————————————————————
              Con="was connected"
           Discon="was disconnected"
      TargetFound="Target directory found"
    TargetMissing="Target directory missing"
# Functions (in alphabetic order)
——————————————————————————————————————————————
Error () {
local Message="$1"
local WindowTitle="$2"

yad --image="error" \
--title="$WindowTitle" \
--text="$Message" \
--width=300 \
--center \
--button="OK":0
exit 2
}

TargetConnected() {
local Type="${1,,}"

yad --title "${TargetFound}" \
--image="info" \
--text "${Type^} ${Con}." \
--center \
--no-buttons \
--timeout 5
}

TargetDisconnected() {
local Type="${1,,}"

yad --title "${TargetMissing}" \
--image="info" \
--text "${Type^} ${Discon}." \
--center \
--no-buttons \
--timeout 5
}


Of course all this shouldn't really be necessary, so if anyone knows of an
actually solution to the problem, I sure want to hear about it.
And again, my original problem is that I don't know an obvious and easy way
to find my Android device using the same path every time, since it change
every time the device is plugged in, for instance:
First time plugged in:
/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C006%5D
Unplugg it, then plugg it in again:
/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C010%5D
and so on.

As of now, I determine the path using my NexusPath function, see above, but
I hope there is a way to make this method obsolete. I mean, what sense does
it make having the phone ending up at different places every time? What's
the next step? Drive letters…?

Oh, and of course I take no responsibility for my script what so ever. I'm
an amateur, and I was happy to make this work at all for me on my
particular system. There are probably tons of mistakes in there. My
original script was in Swedish, but I think I translated everything
properly. I didn't actually run this translated version though, so it might
look funny, I don't know.

Don't run it and then blame me for whatever happens (the fact that I can't
think of anything bad that could happen doesn't mean much, I guess), or at
least take backups before doing it…

This is how it works when running it (the original Swedish version) on my
system:

   - I start my computer and log in. The script is started automatically.
   - If my phone is already connected, a link to my phone is created,
   called ~/Nexus. I can use that link in my scripts to move files to and from
   the phone or just navigate in it, open files and whatever.
   - Whenever I connect or disconnect my phone physically from the USB
   port, a dialogue pops up telling me that my phone was connected or
   disconnected (different dialogues for the two different occasions). I can
   either close the dialogue with the close button in the upper left (or right
   depending on system settings) corner or just wait for 5 seconds for it to
   close itself automatically.
   - When the phone is unplugged, the link is removed.
   - When the phone is plugged in, the link is removed if there is one
   (maybe from an earlier session, if I closed the computer when the phone was
   still physically plugged in), then a new link is created. No tests to
   determine if the existing link actually works is performed, the link is
   just removed and a new, hopefully working one, is created.


Well, that's just about it, I think…

And if something above doesn't make sense, don't hesitate to ask. My native
language is not English, so I can imagine that there are a few things that
are not too clear… I've tried my best, though.


I'm also sorry for using text formatting on this email, but I needed TABs
in the code and I just couldn't get it to work in another way.
I wrote this in Chrome, using the web interface for my GMail account.


Kind regards

Johnny Rosenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150802/0f182d23/attachment.html>


More information about the ubuntu-users mailing list