我尝试在我的笔记本电脑 (win7) 和我的安卓手机 (Android 4.2) 之间建立 USB 连接。笔记本电脑应作为主机为安卓手机供电。目标是笔记本和手机可以发送和接收xml字符串
我尝试关注解释附件模式的 android 页面(http://developer.android.com/guide/topics/connectivity/usb/accessory.html)。
1:我必须像他们在这里所做的那样定义一个附件过滤器:
<?xml version="1.0" encoding="utf-8"?> <resources> <usb-accessory model="DemoKit" manufacturer="Google" version="1.0"/> </resources>
因为我不想识别特殊的硬件。我希望能够识别所有类型的 Windows 计算机(例如,我将手机插入另一台计算机)。
2:我现在没有在窗户方面做任何事情。我只是按照android页面,插入usb电缆并观看日志。应用程序 startet 请求许可,但附件为空。任何提示为什么它为空?代码:
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:我可以使用任何库/项目来识别 Windows 端的 USB 连接吗?
- 4:还有什么我应该考虑的吗?有什么不对吗?
- 5:谢谢你的帮助 :)