Estoy usando el siguiente código:
private boolean statusUSB = false;
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null) {
switch (action) {
case UsbManager.ACTION_USB_DEVICE_DETACHED:
final UsbDevice detDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
final String detMsg="Device DEtached";
statusUSB = false;
Toast.makeText(context, "USB Disconnected", Toast.LENGTH_SHORT).show();
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
case UsbManager.ACTION_USB_ACCESSORY_ATTACHED:
final UsbDevice attDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
final String attMsg="Device atached";
statusUSB = true;
Toast.makeText(context, "USB Connected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
};
Para detectar en que momento el puerto usb es conectado o desconectado, aparte de eso tambien he insertado un flag:
statusUSB = true/false
Para luego usarlo en mi menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.main, menu );
if (statusUSB == true){
// Opcion1
}
if (statusUSB == false){
//Opcion2
}
return true;
}
El hecho es que el Android detecta ambas condiciones cuando se conecta y desconecta el puerto usb, pero el flag no cambia o no puedo leer este valor dentro de mi menu, estoy haciendo bien o hay otra forma de alimentar este flag