Click here to Skip to main content
15,886,026 members
Home / Discussions / Android
   

Android

 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 4:05
professionalJochen Arndt24-Jan-17 4:05 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 4:07
Pavlex424-Jan-17 4:07 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 4:23
professionalJochen Arndt24-Jan-17 4:23 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 4:42
Pavlex424-Jan-17 4:42 
GeneralRe: Android USB Detection Pin
Pavlex425-Jan-17 0:22
Pavlex425-Jan-17 0:22 
GeneralRe: Android USB Detection Pin
Jochen Arndt25-Jan-17 0:33
professionalJochen Arndt25-Jan-17 0:33 
GeneralRe: Android USB Detection Pin
Pavlex426-Jan-17 6:20
Pavlex426-Jan-17 6:20 
GeneralRe: Android USB Detection Pin
Pavlex427-Jan-17 22:22
Pavlex427-Jan-17 22:22 
How to use app to detect when otg cable is plugged in and when it's plugged out? Is there intent for otg cable like this for usb devices: "android.hardware.usb.action.USB_DEVICE_ATTACHED"

I have created app like this but I only detects flash drive not otg cable:

C++
public class MainActivity extends AppCompatActivity
{

    private TextView mInfo;
    private Logger mLogger;
    private HashMap<UsbDevice, UsbDataBinder> mHashMap = new HashMap<UsbDevice, UsbDataBinder>();
    private UsbManager mUsbManager;
    private PendingIntent mPermissionIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mInfo = (TextView)findViewById(R.id.log);

        mLogger = new Logger(this);
        mLogger.setMode(Logger.MODE_TOAST);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        usbConnection();
    }

    private void usbConnection() {
        IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        registerReceiver(mUsbAttachReceiver , filter);
        filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbDetachReceiver , filter);

        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);

        showDevices();
    }

    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbDetachReceiver);
        unregisterReceiver(mUsbAttachReceiver);
        unregisterReceiver(mUsbReceiver);
    };

    BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    // call your method that cleans up and closes communication with the device
                    UsbDataBinder binder = mHashMap.get(device);
                    if (binder != null) {
                        binder.onDestroy();
                        mHashMap.remove(device);
                        Toast.makeText(MainActivity.this, "Attached!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };

    BroadcastReceiver mUsbAttachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                showDevices();
                Toast.makeText(MainActivity.this, "Detached!", Toast.LENGTH_SHORT).show();
            }
        }
    };

    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice) intent
                            .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(
                            UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            // call method to set up device communication
                            UsbDataBinder binder = new UsbDataBinder(mUsbManager, device);
                            mHashMap.put(device, binder);
                            Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        // Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }
        }
    };

    private void showDevices() {
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while(deviceIterator.hasNext()){
            UsbDevice device = deviceIterator.next();
            mUsbManager.requestPermission(device, mPermissionIntent);
            //your code
            mLogger.log("usb", "name: " + device.getDeviceName() + ", " +
                    "ID: " + device.getDeviceId());
            mInfo.append(device.getDeviceName() + "\n");
            mInfo.append(device.getDeviceId() + "\n");
            mInfo.append(device.getDeviceProtocol() + "\n");
            mInfo.append(device.getProductId() + "\n");
            mInfo.append(device.getVendorId() + "\n");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }
}

QuestionRe: Android USB Detection Pin
David Crow24-Jan-17 4:23
David Crow24-Jan-17 4:23 
AnswerRe: Android USB Detection Pin
Pavlex424-Jan-17 4:33
Pavlex424-Jan-17 4:33 
AnswerRe: Android USB Detection Pin
Pavlex424-Jan-17 4:42
Pavlex424-Jan-17 4:42 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 5:09
professionalJochen Arndt24-Jan-17 5:09 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 6:21
Pavlex424-Jan-17 6:21 
GeneralRe: Android USB Detection Pin
Richard MacCutchan24-Jan-17 6:28
mveRichard MacCutchan24-Jan-17 6:28 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 7:49
Pavlex424-Jan-17 7:49 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 10:49
Pavlex424-Jan-17 10:49 
AnswerRe: Android USB Detection Pin
Pavlex424-Jan-17 10:48
Pavlex424-Jan-17 10:48 
AnswerRe: Android USB Detection Pin
Pavlex427-Jan-17 22:23
Pavlex427-Jan-17 22:23 
Questionremote android app's source code Pin
AkhilVarghese21-Jan-17 6:34
AkhilVarghese21-Jan-17 6:34 
AnswerRe: remote android app's source code Pin
Afzaal Ahmad Zeeshan21-Jan-17 7:52
professionalAfzaal Ahmad Zeeshan21-Jan-17 7:52 
QuestionAndroid USB OTG Support Enable Pin
Pavlex420-Jan-17 6:55
Pavlex420-Jan-17 6:55 
AnswerRe: Android USB OTG Support Enable Pin
Richard MacCutchan20-Jan-17 7:10
mveRichard MacCutchan20-Jan-17 7:10 
QuestionYour Best Recommended Android Apps & Android Game Apps Tutorials For Complete Beginners Pin
Member 1295678918-Jan-17 10:06
Member 1295678918-Jan-17 10:06 
AnswerRe: Your Best Recommended Android Apps & Android Game Apps Tutorials For Complete Beginners Pin
Richard MacCutchan18-Jan-17 22:14
mveRichard MacCutchan18-Jan-17 22:14 
Questionhow to input excel file into sqlite Android Pin
Member 1294623511-Jan-17 19:59
Member 1294623511-Jan-17 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.