我嘗試在筆記本電腦(win7)和我的android手機(Android 4.2)之間建立USB連接。 筆記本電腦應充當主機來爲android手機供電。目標是筆記本和手機可以發送和接收xml字符串Android(配件模式)和Windows PC(主機)之間的USB通信
我試圖按照說明配件模式的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電纜並觀看日誌。應用程序開始請求許可,但附件爲空。任何暗示爲什麼它是空的? 代碼:
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:是否有任何libarys /項目,我可以用它來識別在Windows端USB連接?
- 4:我應該考慮更多的事情嗎?事情是錯的?
- 5:THX對您有所幫助:)
所以我猜#1現在更清晰了。 Intentfilter僅用於自動檢測 – B770 2013-04-11 11:52:52