Click here to Skip to main content
15,883,535 members
Home / Discussions / Android
   

Android

 
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 
Go to ParentHow 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();

    }
}

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 
QuestionRe: how to input excel file into sqlite Android Pin
Richard MacCutchan11-Jan-17 21:15
mveRichard MacCutchan11-Jan-17 21:15 
AnswerRe: how to input excel file into sqlite Android Pin
Member 1294623516-Jan-17 2:22
Member 1294623516-Jan-17 2:22 
GeneralRe: how to input excel file into sqlite Android Pin
Richard MacCutchan16-Jan-17 2:24
mveRichard MacCutchan16-Jan-17 2:24 
QuestionRe: how to input excel file into sqlite Android Pin
David Crow16-Jan-17 3:21
David Crow16-Jan-17 3:21 
QuestionAndroid Otp Verification Pin
Member 1294449710-Jan-17 21:20
Member 1294449710-Jan-17 21:20 
QuestionRe: Android Otp Verification Pin
Richard MacCutchan10-Jan-17 21:33
mveRichard MacCutchan10-Jan-17 21:33 
AnswerRe: Android Otp Verification Pin
Member 1295601618-Jan-17 1:10
Member 1295601618-Jan-17 1:10 
Questionfull source code for doctor appointment app Pin
Member 1292829230-Dec-16 6:44
Member 1292829230-Dec-16 6:44 
AnswerRe: full source code for doctor appointment app Pin
Ravi Bhavnani30-Dec-16 6:54
professionalRavi Bhavnani30-Dec-16 6:54 
AnswerRe: full source code for doctor appointment app Pin
Richard MacCutchan30-Dec-16 21:10
mveRichard MacCutchan30-Dec-16 21:10 

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.