Re: Okay, I give up… how to connect my (Android) phone properly (MTP)?
Johnny Rosenberg
gurus.knugum at gmail.com
Tue Aug 4 19:21:11 UTC 2015
2015-08-04 15:57 GMT+02:00 Johnny Rosenberg <gurus.knugum at gmail.com>:
> 2015-08-04 15:34 GMT+02:00 Petter Adsen <petter at synth.no>:
>
>> On Tue, 4 Aug 2015 15:08:22 +0200
>> Johnny Rosenberg <gurus.knugum at gmail.com> wrote:
>>
>> > 2015-08-04 14:58 GMT+02:00 Petter Adsen <petter at synth.no>:
>> >
>> > > On Tue, 4 Aug 2015 14:33:09 +0200
>> > > Johnny Rosenberg <gurus.knugum at gmail.com> wrote:
>> > >
>> > > > 2015-08-04 13:30 GMT+02:00 Petter Adsen <petter at synth.no>:
>> > > > > I take this to mean that you replace DEVICE with the device node
>> and
>> > > > > LOCATION with where you want to mount it, but I might be wrong.
>> > > > >
>> > > >
>> > > > Yes, that should be what it means, but it didn't work. The line I
>> tried
>> > > was:
>> > > > gvfs-mount -d /dev/bus/usb/001/013 ${HOME}/Desktop/MyPhone
>> > > > but it mounted it at the usual place without any kind of error
>> message
>> > > (the
>> > > > few times it mounted it at all).
>> > >
>> > > OK, that is surprising. Bug, maybe? I really don't know, and the man
>> > > page is exceptionally unhelpful. It doesn't seem possible to interact
>> > > with gvfs file systems in a sane way, you are instructed to use
>> > > gvfs-copy, gvfs-rm etc, so you might be out of luck here. Try to file
>> a
>> > > bug against gvfs-bin and see what the developers have to say. Either
>> > > things do not work as expected, or the man page is at best misleading.
>> > >
>> > > Petter
>> > >
>> >
>> > Or maybe the mtp thing makes things a bit crazy?
>> >
>> > You don't happen to know where I file such a bug report, do you?
>>
>> 'ubuntu-bug gvfs-bin'
>>
>
> Oh, that simple? I didn't know that!
>
>
>>
>> Alternatively, upstream. I would guess that is bugs.gnome.org or
>> something similar. The homepage address from the package is:
>>
>> https://wiki.gnome.org/Projects/gvfs
>>
>> I do not know if Canonical has added any patches to gvfs, which is why
>> I suggested Launchpad first, but upstream is probably a better place to
>> get an actual answer that might tell you something.
>>
>
> Thanks for all that info. I'll file a bug one way or the other.
>
>
> Anyway, here's my new workaround. There are still an issue with it, but at
> least it works:
>
> First the udev rule, placed in a file called
> /etc/udev/rules.d/10-local.rules:
> ATTR{product}=="Nexus 6", ATTR{serial}=="<SerialNumberOfMyPhone>",
> SYMLINK+="Nexus_6", RUN+="/usr/local/bin/CreateLinkToPhone.sh"
>
> Now the script, translated to English:
> #!/bin/bash
>
> # Create a link to the connected phone in the Media directory.
>
> # Variables
> ————————————————————————————————————————————————————————————————————
> # Calculate User and XDG_Runtime_Dir by searching for the phone.
> Path="/run/user/"
> XDG_Runtime_Dir=$(find "$Path" -maxdepth 1 -type d -regex "${Path}[0-9]+")
> UserID=$(basename "${XDG_Runtime_Dir}")
> User=$(getent passwd "${UserID}" | cut -d ':' -f1)
>
> # Now just assign the right strings to all the other variables.
> Media="/media/${User}"
> LinkName="${Media}/${ID_MODEL}"
> Target="${XDG_Runtime_Dir}/gvfs/mtp:host=%5Busb%3A${BUSNUM}%2C${DEVNUM}%5D"
> LogFile="/var/log/CreateLinkToPhone"
>
> # Main
> —————————————————————————————————————————————————————————————————————————
> [[ -L ${LinkName} ]] && rm -f "${LinkName}"
> if [[ ${ACTION} == "add" ]]; then
> ln -s "${Target}" "${LinkName}"
> fi
>
> # For the log file:
> printf "%s\n Event: %s\n Link name: %s\n" \
> "$(date +'%F %T')" \
> "${ACTION^}" \
> "${LinkName}" >> "${LogFile}"
> printf "Target folder: %s\n User: %s (%s – %s)\n\n" \
> "${Target}" \
> $(getent passwd "${User}" | cut -d ':' -f 5 | cut -d ',' -f 1) \
> "${UserID}" \
> "${User}" >> "${LogFile}"
> # End of script
> ————————————————————————————————————————————————————————————————
>
>
> My log file currently looks like this (translated to English and replaced
> some names and stuff), after a few times plugging and unplugging the phone:
>
> 2015-08-04 15:23:15
> Event: Add
> Link name: /media/MyUserName/Nexus_6
> Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C015%5D
> User: MyName (1000 – MyUserName)
>
> 2015-08-04 15:27:10
> Event: Add
> Link name: /media/MyUserName/Nexus_6
> Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C016%5D
> User: MyName (1000 – MyUserName)
>
> 2015-08-04 15:29:50
> Event: Add
> Link name: /media/MyUserName/Nexus_6
> Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C017%5D
> User: MyName (1000 – MyUserName)
>
> 2015-08-04 15:31:42
> Event: Add
> Link name: /media/MyUserName/Nexus_6
> Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C018%5D
> User: MyName (1000 – MyUserName)
>
>
> Now I just wonder why the script doesn't run when I unplug the phone.
> Shouldn't it? If not, what is the ACTION environment variable for if the
> value is always ”add”…?
>
>
Solved it, I think.
Those ATTR{} are not available anymore when the phone is unplugged, but
there are environment variables that are. First I tried to use one called
ID_SERIAL, but for some reason tje script didn't start when plugging the
phone in, only when unplugging it.
So instead I went with PRODUCT, but that triggered the script twice when
unplugging.
So I ended up writing two different rules, one for each event. Here's my
new rule:
*ACTION=="add", ATTR{serial}="ZX1G428DSG",
RUN+="/usr/local/bin/CreateLinkToPhone.sh"*
*ACTION=="remove", ENV{ID_SERIAL_SHORT}=="ZX1G428DSG",
RUN+="/usr/local/bin/CreateLinkToPhone.sh"*
The ACTION variable isn't really necessary, but I think the rule is easier
to understand that way. It's easy to see what the rule is for and it
doesn't hurt.
The script is the same as before (the version below is just translated to
English):
*#!/bin/bash*
*# Create a link to the connected phone in the Media directory.*
*# Variables
————————————————————————————————————————————————————————————————————*
*# Calculate User and XDG_Runtime_Dir by searching for the phone.*
*Path="/run/user/"*
*XDG_Runtime_Dir=$(find "$Path" -maxdepth 1 -type d -regex "${Path}[0-9]+")*
*UserID=$(basename "${XDG_Runtime_Dir}")*
*User=$(getent passwd "${UserID}" | cut -d ':' -f1)*
*# Now just assign the right strings to all the other variables.*
*Media="/media/${User}"*
*LinkName="${Media}/${ID_MODEL}"*
*Target="${XDG_Runtime_Dir}/gvfs/mtp:host=%5Busb%3A${BUSNUM}%2C${DEVNUM}%5D"*
*LogFile="/var/log/CreateLinkToPhone"*
*# Main
—————————————————————————————————————————————————————————————————————————*
*[[ -L ${LinkName} ]] && rm -f "${LinkName}"*
*if [[ ${ACTION} == "add" ]]; then*
* ln -s "${Target}" "${LinkName}"*
*fi*
*# For the log file:*
*printf "%s\n Event: %s\n Link name: %s\n" \*
* "$(date +'%F %T')" \*
* "${ACTION^}" \*
* "${LinkName}" >> "${LogFile}"*
*printf "Target folder: %s\n User: %s (%s – %s)\n\n" \*
* "${Target}" \*
* $(getent passwd "${User}" | cut -d ':' -f 5 | cut -d ',' -f 1) \*
* "${UserID}" \*
* "${User}" >> "${LogFile}"*
My log file now looks like this after removing and adding my phone twice (I
translated to English and changed all names):
*2015-08-04 20:46:26*
* Event: Remove*
* Link name: /media/MyUserName/Nexus_6*
*Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C037%5D*
* User: MyName (1000 – MyUserName)*
*2015-08-04 20:46:31*
* Event: Add*
* Link name: /media/MyUserName/Nexus_6*
*Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C038%5D*
* User: MyName (1000 – MyUserName)*
*2015-08-04 20:51:50*
* Event: Remove*
* Link name: /media/MyUserName/Nexus_6*
*Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C038%5D*
* User: MyName (1000 – MyUserName)*
*2015-08-04 20:51:53*
* Event: Add*
* Link name: /media/MyUserName/Nexus_6*
*Target folder: /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C039%5D*
* User: MyName (1000 – MyUserName)*
Works like I expected. Also, the link is created and removed as it should
so I really have nothing to complain about, except that it would be fun to
have the same behaviour with only one rule…
And it would be nice if the script could be done without those workarounds
for lack of environment variables like USER and so on, but I think I will
leave this behind me now.
Thank you for all your help and input!
Kind regards
Johnny Rosenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150804/4c9035f4/attachment.html>
More information about the ubuntu-users
mailing list