2013-09-24 48 views
1

我想打控制連接到PIC18F4550.I發光二極管我要去送一個數組爲,但發送我想檢測我的USB device.For檢測我發現簡單的例子,任何數據之前的應用程序,但它不工作,並且找不到我做錯的地方。Android的主機設備檢測

任何人都可以幫助我嗎?

此致敬禮。

這裏是我的mainActivity:

import java.util.HashMap; 
import java.util.Iterator; 
import android.hardware.usb.UsbDevice; 
import android.hardware.usb.UsbManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); 

    HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); 
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 

    while(deviceIterator.hasNext()){ 

     UsbDevice device = deviceIterator.next(); 
     Toast.makeText(this,"Value of device :" +device.getDeviceName(), Toast.LENGTH_LONG).show(); 

    } 

     } 
} 

清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.usbsenderandroid" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="12" 
    android:targetSdkVersion="12" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.usbsenderandroid.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
     </intent-filter> 
     <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
        android:resource="@xml/device_filter" /> 
    </activity> 
</application> 

</manifest> 

device_filter:

<resources> 
    <usb-device vendor-id="1240" product-id="0063" /> 
</resources> 

這就是細節時,我連我的照片

HID \ VID_04D8 & PID_003F & REV_0002

HID \ VID_04D8 & PID_003F

HID_DEVICE_UP:FF00_U:0001

HID_DEVICE

回答

1

我感覺有點傷感來回答我的問題,但我想通了的傢伙。問題不在於代碼。我發現在圖片的頁面上:「一個Android配件必須是USB主機,18F USB PIC是設備,而不是主機」。這裏的鏈接 description

+0

這是真的,但沒有真正相關的,因爲你的代碼有上面沒有爲「Android的附件」,而是爲在Android設備是USB主機的情況下,與外部設備USB從設備。所以你的項目是可行的,*假設你的特定Android設備支持可選的USB主機模式* –

+0

我感到困惑,那麼你是否告訴我,可以用我的android手機與pic18f4550進行通信?如果是,你可以向我解釋如何? – Mete

+0

如果您的特定電話具有**可選** USB主機模式,並且您使用的OTG電纜適配器的選擇引腳接地,並且PIC具有USB設備接口,那麼是的。 –