将Android设备置于USB主机模式?

时间:2012-08-02 08:19:31

标签: android usb

对于我的应用程序,我需要让android设备充当USB主机。它需要从USB连接设备发送和接收数据。我已经完成了USB Host in Android Developers Site,并按如下方式开发了示例代码:

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 页面中的按钮时,它显示的错误为:

  

不幸的是USBDemoProj已被停止

并且在logcat中显示错误,如下图所示,但此处system.output()行显示在 Second.java 类中声明的行。任何人都可以帮助我,我的应用程序中的错误是什么?

另外,我在示例代码中使用正确的方法来访问USB主机模式下的设备吗?谁能建议我一个更好的方法呢?

enter image description here

1 个答案:

答案 0 :(得分: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" />