安卓(配件模式)和视窗电脑(主机)之间的USB通信
2022-09-03 16:17:33
我尝试在我的笔记本电脑(win7)和我的Android手机(Android 4.2)之间建立USB连接。笔记本电脑应充当主机为Android手机供电。目标是笔记本电脑和手机可以发送和接收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计算机都能被识别(例如,我将手机插入另一台PC)。
-
2:我现在在窗户方面什么也没做。我只是按照Android页面,插入USB电缆并观看日志。应用启动器请求权限,但附件为 null。任何提示为什么它是空的?法典:
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:感谢您的帮助:)