2012-08-02 96 views
2

对于我的应用程序,我需要使android设备充当USB主机。它需要发送和接收来自USB连接设备的数据。我已经通过了USB Host in Android Developers Site,并开发示例代码如下:将USB设备置于USB主机模式?

Main.java

public class UsbDemoProjActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button btn = (Button)findViewById(R.id.button1); 
     btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "button click", 30).show(); 

       Intent it = new Intent(UsbDemoProjActivity.this,Second.class); 
       startActivity(it); 
      } 
     }); 
    } 
} 

Second.java:

public class Second extends Activity{ 
    UsbDevice device; 
    UsbManager mUsbManager; 
    PendingIntent mPermissionIntent; 
    private static String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     System.out.println("111111111"); 
     mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
     ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION"; 
     System.out.println("2222222222"); 
     HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList(); 
     device = deviceList.get("deviceName"); 
     System.out.println("33333333333"); 
     mPermissionIntent = PendingIntent.getBroadcast(this, 0, 
              new Intent(ACTION_USB_PERMISSION), 0); 
     System.out.println("444444444444"); 
     IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 
     System.out.println("555555555555"); 
     registerReceiver(mUsbReceiver, filter); 
     System.out.println("66666666666"); 
     mUsbManager.requestPermission(device, mPermissionIntent); 
    } 

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      System.out.println("7777777777777"); 
      if (ACTION_USB_PERMISSION.equals(action)) { 
       synchronized (this) { 
        System.out.println("88888888888"); 
        UsbDevice device = (UsbDevice)intent.getParcelableExtra( 
                 UsbManager.EXTRA_DEVICE); 

        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { 
         if(device != null){ 
          System.out.println("99999999999999"); 
          //call method to set up device communication 
         } 
        } else { 
         //Log.d(TAG, "permission denied for device " + device); 
         System.out.println("permission denied for device" + device); 
        } 
       } 
      } 
     } 
    }; 
} 

然而,当我在Main.java点击按钮页面显示错误:

不幸的是USBDemo PROJ已停止

和logcat中显示错误按照下面的图片,但这里的system.output()线显示其在Second.java类中声明。任何人都可以请帮助我什么是我的应用程序中的错误?

此外,我在示例代码中使用正确的方法访问USB主机模式下的设备?任何人都可以给我一个更好的方法吗

enter image description here

回答

0

我使用slickdev图书馆跟我的串口设备,所以我不知道你的代码。它不是onDataReceived()?这可能仅仅是图书馆的功能,但...

我们的清单应该都有这样在它:

<uses-feature android:name="android.hardware.usb.host" android:required="true"></uses-feature> 

,并在

<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" />