1

I need your help. :-) First I want to excuse for my Englisch, I´m from Germany.

I have a Bash script which should be executed every time an Android Phone is connected through USB. This is done by my udev rule. It works but there is a major problem.

A little more Background. My Bash script should install Apps on all connected Android Phones separately. To achieve this I use the Android Debug Bridge (ADB) and the USB-Debugging mode on the Android device. Every time you connect a phone to the USB port you first need to allow USB-Debugging connection on the phone to access it via the ADB.

As I said it works, but when connecting the device is mounted and ask to allow USB-Debugging. Then the script starts and immediately stops and waits because the device unmounts itself. Then the device mounts itself again, asks again to allow USB-Debugging and a second instance of the bash script is called and both instances of the script are now executed. This is worse and inefficient.

I hope someone could help me.

Here is some code.

UDEV Rule

ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="12d1", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="24e3", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="17ef", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="1004", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="22b8", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="10a9", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="1d4d", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="05c6", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="054c", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="0fce", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="091e", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="04e8", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"

tmp.sh called through UDEV Rule

#!/bin/bash

echo /usr/local/bin/installApps.sh $1 | at now

installApps.sh called through tmp.sh

#!/bin/bash

cd /home/android/Schreibtisch/Apps
adb -s $1 wait-for-device

for APK in $(ls *.apk);
do
    adb -s $1 install $APK
done

Edit 1: Here is my output of udevadm monitor

KERNEL[384.540419] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2 (usb)
KERNEL[384.659326] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0 (usb)
KERNEL[384.660123] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1 (usb)
KERNEL[384.660969] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1/tty/ttyACM0 (tty)
KERNEL[384.663343] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.2 (usb)
UDEV  [384.663375] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2 (usb)
KERNEL[384.663386] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.3 (usb)
UDEV  [384.669236] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0 (usb)
UDEV  [384.670955] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.3 (usb)
UDEV  [384.672776] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1 (usb)
UDEV  [384.674355] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.2 (usb)
UDEV  [384.690927] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1/tty/ttyACM0 (tty)

The Output of it does not differ in any way at my second USB-Port.

Edit 2:

I somehow managed it to reduce the errors. But now there is a slightly different problem and I don´t know if a lock file will help. Now on nearly every 10th device not all apps are installed. I checked through a logfile what the problem is. The Allow USB Debugging Message shows only once but the udev rule is execute twice at the same moment at the same second. But this happens as said only to every 10th device. What could be the reason for that and how could I prevent this?

0

Android phones uses different idProduct for each mode. Check the ones related to USB Debug mode using:

  1. Run:

    tail -f /var/log/kern.log
    
  2. Change modes from phone and record idProduct's

    Example, mine:

    • MTP, idVendor=0bb4, idProduct=2008
    • PTP, idVendor=0bb4, idProduct=200b
    • MTP+Debug, idVendor=0bb4, idProduct=0c02
    • PTP+Debug, idVendor=0bb4, idProduct=200c
    • Charge only, None
  3. Limit your rules only for those modes, example

    ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0c02|200c", MODE="0666", GROUP="plugdev" RUN+="/usr/local/bin/tmp.sh $attr{serial}"
    

So, the rule is triggered only once after activating Debug.

Yep, connecting new devices or new machine pops up authorization message. It uses same debug idProduct. I though it's possibly changing USB interfaces (add USB debug one) after confirming the message, but it is NOT. It does not reconnect as your case, may be not all devices have same behavior.

One solution that may work in whatever case you have, is to use a lock file.

#!/bin/bash

if ! [ -f /tmp/$1-udev.lock ]
then
    touch /tmp/$1-udev.lock

    cd /home/android/Schreibtisch/Apps
    adb -s $1 wait-for-device

    for APK in $(ls *.apk);
    do
        adb -s $1 install $APK
    done

    rm /tmp/$1-udev.lock
fi

BTW, you can short those rules and split line using environment variable

ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="04e8|054c|05c6|091e|0bb4|0fce|1004|10a9|12d1|17ef|1d4d|22b8|24e3", ENV{MY_ADB_DEVICES}="1"
ENV{MY_ADB_DEVICES}=="1", MODE="0666", GROUP="plugdev", RUN+="/usr/local/bin/tmp.sh $attr{serial}"
  • 1
    I think that this does not help. The phones will be connected with activated USB-Debugging. Confirm the "Allow USB-Debugging" message won´t change the idProduct. It does only change if the phone is first connected without active USB-Debugging and when USB-Debugging is then turned on. – Deep_Horizons Jun 26 '15 at 7:59
  • thanks again for your help but I still didn´t found the solution and my problem changed slightly. – Deep_Horizons Jul 2 '15 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.