Android : how to detect already connected usb device?

后端 未结 3 962
庸人自扰
庸人自扰 2020-12-09 23:22

I\'m trying to detect usb devices which are already connected to android. I understand there are actions to detect when USB is either attached or detached. But I don\'t rea

3条回答
  •  粉色の甜心
    2020-12-09 23:41

    The answer from "Christopher Garza" is probably in Kotlin. I'll send you the code but in Java.

    String actionString = getApplicationContext().getPackageName() + ".action.USB_PERMISSION";
    
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new
                    Intent(actionString), 0);
    

    You can remove "getApplicationContext()" if you are pasting this code in an Activity. Otherwise if you are pasting this in other class not specified with an Activity, then you need some sort of this code:

    public class Example {
        private Context context;
    
        public Example(Context context) {
            this.context = context;
        }
    }
    

提交回复
热议问题