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:
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