2017-01-26 3 views
-1

Comment utiliser l'application pour détecter quand le câble otg est branché et quand il est branché? Y at-il l'intention pour le câble OTG comme celui-ci pour les périphériques USB: « android.hardware.usb.action.USB_DEVICE_ATTACHED »Android Kernel OTG

J'ai créé l'application comme cela, mais je ne détecte que le lecteur flash non câble OTG:

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(); 

    } 
} 
+0

Vous vous posez la même question encore et encore, s'il vous plaît arrêtez! – Selvin

+0

Est-ce que j'aurai une réponse à ma question? –

+0

Pourquoi je n'obtiendrai pas de réponse? –

Répondre

1

Je ne ai jamais essayé OTG USB, mais de https://github.com/shakalaca/USB-OTG-Manager vous pouvez utiliser

<intent-filter> 
    <action android:name="com.sonyericsson.hardware.action.USB_OTG_DEVICE_CONNECTED" /> 
    <action android:name="com.sonyericsson.hardware.action.USB_OTG_DEVICE_DISCONNECTED" /> 
    <action android:name="com.sonyericsson.hardware.action.USB_OTG_ERROR" /> 
    <action android:name="com.sonyericsson.usbotg.ACTION_ERROR_OK" /> 

    <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /> 

</intent-filter> 

de la documentation:

USB OTG Gestionnaire

Ce logiciel est initialement conçu pour Sony Ericsson Xperia Arc S. Depuis OTG est activé dans le noyau, je me demande pourquoi je ne peux pas accéder à mon clé. J'ai donc créé cette petite application pour lire livres sur mon disque de pouce, et heureusement pour toujours! : D

pris en charge Téléphone: * Sony Ericsson Xperia Arc S (4.0.2.A.042, 4.0.2.A.062) * Sony Ericsson Xperia Mini Pro (4.0.2.A.042, 4.0 .2.A.058) * Samsung Galaxy Nexus

périphérique USB: * FAT clé USB * Card Reader

projet lié: https://github.com/mik3y/usb-serial-for-android

vous êtes sur votre propre bateau maintenant.

+0

Comment l'utiliser sur mon s3 neo i9301i? –