How to detect USB connection via application?

Asked

Viewed 210 times

11

It is possible to detect a USB connection in my Android application and how to do this?

I have a sync menu that is performed via USB and FTP, however I want to make the USB option accessible only when the device is connected to the PC via USB.

I saw something about Usbmanager, but I didn’t find anything that could help me.

2 answers

7


In the Activity that has this menu do the following:

Declare a class derived from BroadcastReceiver

public class UsbDeviceDetect extends BroadcastReceiver { 

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED")) {

                //Aqui torne enable o seu item de menu
        }
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED")) {

                //Aqui torne disable o seu item menu
        }
    } 
}

Declare two attributes, one for the BroadcastReceiver and another to the IntentFilter

private UsbDeviceDetect usbDeviceDetect;
private IntentFilter filter;

In the method onCreate create an instance of BroadcastReceiver and its IntentFilter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ----------
    UsbDeviceDetect usbDeviceDetect = new UsbDeviceDetect();
    filter = new IntentFilter();
    filter.addAction("android.intent.action.UMS_CONNECTED");
    filter.addAction("android.intent.action.UMS_DISCONNECTED");

}

In the method onResume register the BroadcastReceiver

@Override
protected void onResume(){
    super.onResume();

    registerReceiver(usbDeviceDetect, filter));
}

In the method onPause do the unregister of BroadcastReceiver

@Override
protected void onPause() {

    unregisterReceiver(usbDeviceDetect);

    super.onPause();
}  

4

It seems to me that what you want is in that reply in the OS:

<receiver android:name=".DetactUSB">
   <intent-filter>
        <action android:name="android.intent.action.UMS_CONNECTED" />
        <action android:name="android.intent.action.UMS_DISCONNECTED" />
   </intent-filter>
</receiver>

Code:

public class DetactUSB extends BroadcastReceiver { 
    private static final String TAG = "DetactUSB";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED")) {
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.MAGENTA);
                textView.setTextColor(Color.BLUE);
                textView.setPadding(10,10,10,10);
                textView.setText("USB connected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
                Log.i(TAG,"USB connected..");
        }
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED")) {
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.MAGENTA);
                textView.setTextColor(Color.BLUE);
                textView.setPadding(10,10,10,10);
                textView.setText("USB Disconnected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
        }
    } 
}

I put in the Github for future reference.

If this is not exactly what you want maybe you can make an adaptation to use another action as per that other answer in the OS:

<intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>

Documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.