Android USB Host mode service - Start based on USB_DEVICE_ATTACHED

后端 未结 3 1114
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-10 03:21

I want to write a service in Android which starts based on USB_DEVICE_ATTACHED intent. So, basically my service should start when a specific USB Device(FT232C - VID:PID 0403:601

3条回答
  •  爱一瞬间的悲伤
    2021-02-10 04:02

    Yes, it is very well possible, sorry to say that you are using wrong intent-filter in your reveiver tag in AndroidManifest.xml. Let me guide you

    AndroidManifest.xml

    .
    .
    .
    
       
            
            
       
    
    

    BroadcastReceiver file

    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"))
            {
                    // Fire your Intent to start Activity
                    Log.i(TAG,"USB connected..");
            }
            if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
            {
            }
        } 
    }
    

提交回复
热议问题