How to mount a USB drive on Android Things?

前端 未结 4 1930
[愿得一人]
[愿得一人] 2020-12-11 12:04

I\'m attempting to read files off of a USB drive for an Android Things app on a Raspberry Pi. I\'m able to scan the list of mounted devices like so:

<
相关标签:
4条回答
  • 2020-12-11 12:32

    I think the only solution is to create a service in init.rc to be able to execute the script with root permisions on Boot. Apparently Android things doesnt have a solution right know. Something like:

    on property:dev.bootcomplete=1

    start bootcomplete_handler
    

    service bootcomplete_handler /system/bin/sh /system/bin/bc_handler.sh

    class late_start
    
    user root
    
    group root
    
    disabled
    
    oneshot
    

    But dont know if this will work

    0 讨论(0)
  • 2020-12-11 12:37

    ADB ONLY SOLUTION

    Seem like as of now USB drives aren't mounted automatically. In order to make your code work I had to mount it manually.

    As you can see (from /proc/partitions) in the /proc partition the USB drive is detected as sda.

    ADB mounting

    • Make a directory to mount to

      mkdir /mnt/usb
      
    • Mount the device

      mount -t vfat -o rw /dev/block/sda1 /mnt/usb
      

    Now you should be able to list (and manage) the files on the USB drive both via ADB and from within the app (/mnt/usb will also be logged).

    0 讨论(0)
  • 2020-12-11 12:39

    I have made an script that every 10 seconds if detects a usb storage units mounts it automatically, the only problem is to launch it on Boot, maybe this could help you, I have my post here: Execute Script on Boot Android Things

    And the script:

    while true; do
    
    if [ "$( ls -l /dev/block/sd* | wc -l)" -ge 1 ];
    
    then echo "partition available"
    
    if [ "$( mount | grep  "usbAlv" -c)" -ge 1 ]; #if partition not mounted
    
    then echo " Unit Mounted"
    
    else
    
    echo "not  mounted"
    
    
    //if folder where we mount the partition doesnt exist
    
    if [ !"$( ls -l /sdcard/usbAlv | wc -l)" -ge 1 ];
    
    then mkdir /sdcard/usbAlv
    
    fi
    
    su root << EOSU
    
    mount -t vfat -o rw /dev/block/sd* /sdcard/usbAlv
    
    EOSU
    
    fi
    
    else
    
    echo "not partition available"
    
    fi
    
    sleep 10;
    
    done
    

    Hope it helps, i guess now is the only posible way to do it programatically

    0 讨论(0)
  • 2020-12-11 12:45

    Working and discarded options

    1. Use ADB (working)

    As answerer by Onik in this post

    2. Use USB API (working)

    As Android API for USB allows bulkTransfer, you can code SCSI compatible commands.

    You can also try existing libraries such as libaums, posted by Phaestion in response to this similar question

    3. Inject shell commands at init.rc at boot.img (NOT working)

    Shell commands can be injected to the init.rc file inside the boot.img (not the init.rc file that you can find in the /root directory). However, given the A-B boot nature of the AndroidThings compilation, I have been unable to make it work.

    4. Add advanced app permissions (NOT working)

    The following permissions seem promising to execute root commands from the app. However, permission denied error continues to block the root command execution.

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.CALL_PRIVILEGED" tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.MANAGE_USB" tools:ignore="ProtectedPermissions" />
    

    I even tried to compile an AndroidThings distribution with an app with such permissions but the Android Things console does NOT allow to include the app to the compilation

    5. White list the app (NOT working)

    There is a white list for priviledged apps. However, this white list is placed in a priviledged directory and cannot be overwritten even from the shell command with root user

    6. Install the app at a priviledged location (NOT working) Apps installed in priviledged directories can execute root commands. However, AndroidThings does NOT allow to install apps at such locations (even from the shell with root user)

    0 讨论(0)
提交回复
热议问题