我尝试遵循Android页面上解释附件模式的说明(http://developer.android.com/guide/topics/connectivity/usb/accessory.html)。
1: Must I define a accessory filter like they did here:
<?xml version="1.0" encoding="utf-8"?> <resources> <usb-accessory model="DemoKit" manufacturer="Google" version="1.0"/> </resources>
Because I don't want a special hardware to be recognized. I want all kind of windows computers to be recognized (e.g. I plug the phone in another pc).
2: I've done nothing on the windows side right now. I just followd the android page, pluged in the usb cable and watched the log. The app startet asks for permission, but the accessory is null. Any hints why it is null? Code:
public class MainActivity extends Activity { private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private static final String TAG = "USB_PERMISSION"; UsbAccessory accessory; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); registerReceiver(mUsbReceiver, filter); accessory = (UsbAccessory) getIntent().getParcelableExtra( UsbManager.EXTRA_ACCESSORY); manager.requestPermission(accessory, mPermissionIntent); } 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) { if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { String manufacturer; Log.d(TAG, "permission accepted for accessory " + accessory); if (accessory != null) { manufacturer = accessory.getManufacturer(); Log.d(TAG, "Manufacturer: " + manufacturer); } } else { Log.d(TAG, "permission denied for accessory "+ accessory); } } } } }; }
3: Are there any libarys/projects I can use to identify the USB connection on the Windows side?
- 4: Any further things I should think about? Things that are wrong?
- 5: thx for your help :)