Search code examples
bashadb

How to wait for ADB push to complete?


Some automation setup requires files be loaded onto Android devices.. Internal memory does not refresh automatically when ADB push completes.

My current script loops through ADB commands sequentially for every device simultaneously, then sleeps and triggers a memory scan via the action.MEDIA_MOUNTED broadcast:

adb devices | while read line
do
    if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
    then
        device=`echo $line | awk '{print $1}'`
        echo "$device $@ ..."
        #Additional ADB commands
        #Load files
        adb -s $device push home/test/test-automation-agent/loadondevice/. /sdcard &
    fi
done

#Wait for push to complete
sleep 30

adb devices | while read line
do
    if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
    then
        device=`echo $line | awk '{print $1}'`
        echo "$device $@ ..."
            #Trigger refresh of internal storage
            adb -s $device shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://mnt/sdcard 
    fi
done

The time required to execute ADB push can change dramatically. I would like to replace the sleep with something that detects ADB push completion and executes the broadcast.

Any help is appreciated.


Solution

  • ADBDevices () {
      adb devices | awk -F"\t" '/\tdevice$/{print $1}'
    }
    
    ADBPushAndScan () {
      adb -s $1 push home/test/test-automation-agent/loadondevice/. /sdcard
      adb -s $1 shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://mnt/sdcard
    }
    
    Wait () {
      while [ $(jobs | grep Running | wc -l) -ne 0 ]; do sleep 1; done
    }
    
    for device in $(ADBDevices); do
      ADBPushAndScan $device &
    done
    
    Wait