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

Petter Adsen petter at synth.no
Mon Aug 3 18:24:32 UTC 2015


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.

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

Petter

-- 
"I'm ionized"
"Are you sure?"
"I'm positive."
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 213 bytes
Desc: OpenPGP digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150803/1c9de76f/attachment.sig>


More information about the ubuntu-users mailing list