Skip to content

Instantly share code, notes, and snippets.

@osfunapps
Last active June 3, 2024 15:22
Show Gist options
  • Select an option

  • Save osfunapps/bf5ced177302e13fd2a2fa387773b7fd to your computer and use it in GitHub Desktop.

Select an option

Save osfunapps/bf5ced177302e13fd2a2fa387773b7fd to your computer and use it in GitHub Desktop.

Revisions

  1. osfunapps revised this gist Apr 5, 2022. No changes.
  2. osfunapps revised this gist Apr 5, 2022. 1 changed file with 16 additions and 27 deletions.
    43 changes: 16 additions & 27 deletions apple_device_connection_helper.command
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,17 @@
    # removefrom quarantine:
    # sudo xattr -rd com.apple.quarantine "/Users/home/Programming/Python/projects/ToolBoxPy/src/linux/connectPhone.command"

    # This small script meant to save Apple users the hassle of resetting/disconnecting and connecting an Apple device, again and again, until a steady connection.
    # Just connect your device and run this command, it will reconnect the device until a steady connection

    # set here the sensitivity of the checks. I fount it best to use these props
    MAX_PASSED_CHECKS=40
    TIMEOUT_BETWEEN_CHECKS=0.5 # in secs
    MAX_PASSED_CHECKS=50
    TIMEOUT_BETWEEN_CHECKS=0.3 # in secs
    TIMEOUT_AFTER_EACH_USB_RESET=4 # in secs

    # dynamic variables
    CHECKS_PASSED=0

    # obtain the currently connected devices
    DEVICES_COMMAND=$(ioreg -p IOUSB)
    DEVICES_COUNT_STR=$(echo "$DEVICES_COMMAND" | wc -l)
    OLD_DEVICES_COUNT=$((DEVICES_COUNT_STR))

    # start the checks
    while true
    do
    @@ -25,29 +22,21 @@ do
    fi

    echo "---------------------------------------------"
    echo "Checking again..."
    DEVICES_COMMAND=$(ioreg -p IOUSB)
    DEVICES_COUNT_STR=$(echo "$DEVICES_COMMAND" | wc -l)
    CURR_DEVICES_COUNT=$((DEVICES_COUNT_STR))

    echo "Old devices count is: $OLD_DEVICES_COUNT"
    echo "New devices count is: $CURR_DEVICES_COUNT"

    if [ $OLD_DEVICES_COUNT == $CURR_DEVICES_COUNT ]
    echo "**** Checking start..."
    CHECK_DEVICES_COMMAND=$(system_profiler SPUSBDataType | grep -e iPad -e iPhone)
    printf "**** Checking done...\n\n"
    if [ -z "$CHECK_DEVICES_COMMAND" ]
    then
    echo "Devices have the same size. Increasing checks by one!"
    CHECKS_PASSED=$((CHECKS_PASSED+1))
    echo "couldn't find device. Resetting USB..."
    CHECKS_PASSED=0
    sudo launchctl stop com.apple.usbd; sudo launchctl start com.apple.usbd
    echo "Waiting a bit and starting again"
    sleep $TIMEOUT_AFTER_EACH_USB_RESET
    else
    echo "Devices have different size. Resetting checks"
    CHECKS_PASSED=0
    echo "Resetting usb"
    sudo launchctl stop com.apple.usbd; sudo launchctl start com.apple.usbd
    echo "Waiting a bit and starting again"
    sleep $TIMEOUT_AFTER_EACH_USB_RESET

    echo "device found! +1 to checks!"
    CHECKS_PASSED=$((CHECKS_PASSED+1))
    fi
    OLD_DEVICES_COUNT=$CURR_DEVICES_COUNT
    echo "Number of checks passed: $CHECKS_PASSED/$MAX_PASSED_CHECKS"
    sleep $TIMEOUT_BETWEEN_CHECKS
    done
    exit
    exit
  3. osfunapps renamed this gist Aug 31, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. osfunapps created this gist Aug 31, 2021.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@

    # This small script meant to save Apple users the hassle of resetting/disconnecting and connecting an Apple device, again and again, until a steady connection.
    # Just connect your device and run this command, it will reconnect the device until a steady connection

    # set here the sensitivity of the checks. I fount it best to use these props
    MAX_PASSED_CHECKS=40
    TIMEOUT_BETWEEN_CHECKS=0.5 # in secs
    TIMEOUT_AFTER_EACH_USB_RESET=4 # in secs

    # dynamic variables
    CHECKS_PASSED=0

    # obtain the currently connected devices
    DEVICES_COMMAND=$(ioreg -p IOUSB)
    DEVICES_COUNT_STR=$(echo "$DEVICES_COMMAND" | wc -l)
    OLD_DEVICES_COUNT=$((DEVICES_COUNT_STR))

    # start the checks
    while true
    do
    if [ $CHECKS_PASSED == $MAX_PASSED_CHECKS ]
    then
    echo "Device connected!"
    break
    fi

    echo "---------------------------------------------"
    echo "Checking again..."
    DEVICES_COMMAND=$(ioreg -p IOUSB)
    DEVICES_COUNT_STR=$(echo "$DEVICES_COMMAND" | wc -l)
    CURR_DEVICES_COUNT=$((DEVICES_COUNT_STR))

    echo "Old devices count is: $OLD_DEVICES_COUNT"
    echo "New devices count is: $CURR_DEVICES_COUNT"

    if [ $OLD_DEVICES_COUNT == $CURR_DEVICES_COUNT ]
    then
    echo "Devices have the same size. Increasing checks by one!"
    CHECKS_PASSED=$((CHECKS_PASSED+1))
    else
    echo "Devices have different size. Resetting checks"
    CHECKS_PASSED=0
    echo "Resetting usb"
    sudo launchctl stop com.apple.usbd; sudo launchctl start com.apple.usbd
    echo "Waiting a bit and starting again"
    sleep $TIMEOUT_AFTER_EACH_USB_RESET

    fi
    OLD_DEVICES_COUNT=$CURR_DEVICES_COUNT
    echo "Number of checks passed: $CHECKS_PASSED/$MAX_PASSED_CHECKS"
    sleep $TIMEOUT_BETWEEN_CHECKS
    done
    exit