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

Johnny Rosenberg gurus.knugum at gmail.com
Tue Aug 4 10:24:28 UTC 2015


2015-08-03 20:24 GMT+02:00 Petter Adsen <petter at synth.no>:

> On Mon, 3 Aug 2015 19:01:18 +0200
> Johnny Rosenberg <gurus.knugum at gmail.com> wrote:
>
> > 2015-08-03 11:20 GMT+02:00 Petter Adsen <petter at synth.no>:
> >
> > > On Mon, 3 Aug 2015 11:05:44 +0200
> > > Johnny Rosenberg <gurus.knugum at gmail.com> wrote:
> > >
> > > > 2015-08-03 9:08 GMT+02:00 Petter Adsen <petter at synth.no>:
> > > >
> > > > > On Sun, 2 Aug 2015 18:36:31 +0200
> > > > > Johnny Rosenberg <gurus.knugum at gmail.com> wrote:
> > > > > > 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).
> > > > >
> > > > > The only idea I can come up with is to write a udev rule to mount
> the
> > > > > phone (or create the link) when it is connected. Better than
> running
> > > > > the script every ten seconds.
> > > > >
> > > >
> > > > Yes, that sounds a lot better, thank you!
> > > > I'll do some searching and try to learn how to do that. If you have
> any
> > > > handy links for this, I'll be grateful.
> > > >
> > >
> > > Take a look at this:
> > >
> > >
> > >
> http://askubuntu.com/questions/638452/udev-rule-is-executed-twice-because-android-smartphone-is-mounted-2-times
> > >
> > > It's not exactly what you want, but browsing through it, it seems it
> > > should give you enough of the info you need to achieve what you want if
> > > you also read the answers. I've recently been playing with udev myself,
> > > so I can probably dig up a few more generic links if you need them.
> > >
> > > Use 'udevadm monitor' to get the codes you will need, and then get udev
> > > to mount the phone when it sees the specific code you want.
> > >
> > > A WARNING, though: do not run a script from udev that takes longer than
> > > 10 seconds to execute. AFAIK, there is a timeout there to prevent
> > > scripts from hanging.
> > >
> > > > By the way, my script runs perfectly now and it shows 0% CPU and 240
> KiB
> > > > memory, but I think your idea is better anyway. I'm just happy that I
> > > made
> > > > something that actually works… :P
> > >
> > > :-)
> > >
> > > Good luck,
> > >
> > > Petter
> > >
> >
> > Thanks!
> >
> > I have done some reading and I tried to give it a go. Currently I have a
> > problem though.
> >
> > My idea is to have a rule that runs a script every time the phone is
> > connected. That script creates that link I was talking about earlier.
> > My rule looks like this:
> > ATTR{product}=="Nexus 6", ATTR{serial}=="ZX1G428DSG", SYMLINK+="Nexus_6",
> > RUN+="/usr/local/bin/CreateLinkToPhone.sh"
> >
> > I saved it as /etc/udev/rules.d/10-local.rules
> >
> > The script "/usr/local/bin/CreateLinkToPhone.sh" is now heavily reduced
> and
> > looks like this:
> > #!/bin/bash
> >
> > # Create a link to the connected phone in the HOME directory.
> >
> > # Variables
> > ————————————————————————————————————————————————————————————————————
> > Media="/media/${USER}/"
> > LinkName="${Media}/${ID_MODEL}"
> >
> Target="${XDG_RUNTIME_DIR}/gvfs/mtp:host=%5Busb%3A${BUSNUM}%2C${DEVNUM}%5D"
> >
> > # Main
> > —————————————————————————————————————————————————————————————————————————
> > [[ -L ${LinkName} ]] && rm "${LinkName}"
> > if [[ ${ACTION} == "add" ]]; then
> > ln -s "${Target}" "${LinkName}"
> > fi
> >
> >
> > This doesn't work at all, because my usual environment variables are not
> > present, since the script is run from that udev thing. To do what I
> want, I
> > need at least ${USER} and ${XDG_RUNTIME_DIR} to determine the target of
> the
> > link, but they are not available. Of course I could hard code them, but
> > that seems to be a workaround only when everything is proven to be 100 %
> > impossible…
> >
> > Any ideas of a workaround or how to make the user's usual environment
> > variables accessible from the bash script among in addition to the udev
> > ones?
>
> udev runs with the environment given to it by the init system. I've
> been thinking about a way to do this with something like dbus, but I
> can't really come up with anything specific. That whole rat's nest of
> things you would need to delve into is anything but appealing.
>
> Alternatively, and I haven't looked into this at all - can you get udev
> to give you the device node and then run 'gvfs-mount -d /dev/sdx /foo'
> directly from the udev rule? That would give you the possibility to
> specify the location. NOTE: I don't know squat about gvfs, so that
> might be a totally unworkable approach, but you could try.
>

I gave that a short try but failed.
The mount point seems to be /dev/bus/usb/001/013 (at this very moment), so
I tried, in a terminal:
$ gvfs-mount -d /dev/bus/usb/001/013 ${HOME}/Skrivbord/"Min telefon"
Fel vid montering av /dev/bus/usb/001/013: Platsen är redan monterad
$

The error message means:
Error when mounting /dev/bus/usb/001/013: The place is already mounted.

And it is, at "/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C013%5D"

So I tried:
$ gvfs-mount -u /dev/bus/usb/001/013
Kunde inte hitta inneslutande montering: Infattande montering finns inte
$

I also tried:
$ gvfs-mount -u /dev/bus/usb/001/013
/run/user/1000/gvfs/mtp\:host\=%5Busb%3A001%2C013%5D
Kunde inte hitta inneslutande montering: Infattande montering finns inte
$

The error message means something like:
Could not find enclosing mount point: (Infattande) mount point does not
exist.
”Infatta” means things like ”mount”, but ”mounting mount points” doesn't
make any sense to me… And I don't have a clue what this message means even
in Swedish (which is my native language since I was born, almost 49 years
ago…). I guess it just means that the mount point doesn't exist (even
though it does, doesn't it?)

Never mind, I'll go for a workaround until I find something better.


>
> If that won't work I think the easiest way by far would be to hardcode
> it.
>

Well, after some thinking yesterday I realised that there is another way,
maybe too obvious…
The phone always ends up at /run/users/<UserID>/gvfs/mtp:host%5B…
If a user is not logged in, its UserID is not present, for instance if only
I am logged in, /run/users/1000 is present, but /run/users/1001 is not. So
the script could just search for the path, it won't take very long… It
feels like an uggly workaround, but at least not as uggly as hard coding
the path.


Kind regards

Johnny Rosenberg


>
> Petter
>
> --
> "I'm ionized"
> "Are you sure?"
> "I'm positive."
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150804/9627e17e/attachment.html>


More information about the ubuntu-users mailing list