0

I installed Android Things Developer Preview 5.1 (OIR1.170720.017) on my Raspberry Pi.

The app was created using Xamarin against Android 8 Platform API level 26. I want to detect hotplug events when a USB thumb drive is being attached. I archieved this by using Broadcast-Receiver in my code:

[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { UsbManager.ActionUsbDeviceAttached, UsbManager.ActionUsbDeviceDetached })]
public class MyBroadCastReceiver : BroadcastReceiver
{
    public event EventHandler UsbDeviceAttached;
    public event EventHandler UsbDeviceDetached;

    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action.Equals(UsbManager.ActionUsbDeviceDetached)) UsbDeviceDetached?.Invoke(this, new EventArgs());
        if (intent.Action.Equals(UsbManager.ActionUsbDeviceAttached)) UsbDeviceAttached?.Invoke(this, new EventArgs());
    }
}

Main Activity:

    private MyBroadCastReceiver receiver;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);

        this.receiver = new MyBroadCastReceiver();
    }

    protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(this.receiver, new IntentFilter(UsbManager.ActionUsbDeviceAttached));
    }

    protected override void OnPause()
    {
        UnregisterReceiver(this.receiver);
        base.OnPause();
    }

Now, I have an empty USB thumb drive with no partitions or filesystems on it, just plain block storage. When attaching or detaching the drive, the OnReceive(Context, Intent) method is called. I took a look at the context and intent arguments, but I was not able to determine more information about the device attached. Many properties more appear to be null: Content of intent argument I want to obtain the /dev node of the device (not /mnt or whatever mount point may be used when the drive has a filesystem on it). Looking in dmesg I can see that the block device was assigned to the node /dev/sda. Is there any way to get the node name/path from code? Do I need some additional permissions for that? Thank you

Anar Bayramov
  • 11,158
  • 5
  • 44
  • 64
Ranunculum
  • 73
  • 1
  • 7
  • 1
    https://stackoverflow.com/a/42474815/3290339 – Onik Oct 23 '17 at 11:37
  • Thanks, but this does not answer my question: How do I find out the /dev/ node of the device being attached? – Ranunculum Oct 23 '17 at 12:07
  • Take a look at [this](https://stackoverflow.com/q/41890251/6950238) question and answers. – Andrii Omelchenko Oct 23 '17 at 18:48
  • Thanks, that was very helpful. The libaums provides access for non-rooted device to the usb mass storage device and to the fat32 filesystem, if available. However, I searched along to find out how to read/write raw data to the block device and how to create new partitions and filesystems. This is pretty simple using the dd command and mkfs. But I am afraid I cannot find a way to do such things programmatically within my app... – Ranunculum Oct 26 '17 at 09:02

1 Answers1

0

Android's security model prevents apps from accessing raw device resources like the /dev node of an attached USB device. You will need to go through the USB APIs to communicate with the device.

You might want to review the USB Enumerator sample for Android Things to get a better idea of how to use the Android USB APIs to detect the device insertion and open a communication channel to the device.

Specifically, USB device attachment is not handled as a broadcast by apps. It's triggered as an activity intent for an app that filters on the given device type. The USB Host API Guide can also provide you more details on how all this works.

devunwired
  • 62,780
  • 12
  • 127
  • 139