USB主机为Android [英] USB host for android

查看:191
本文介绍了USB主机为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的freeduino板,类似于通过USB通过Android设备的Nexus 7其中有4.2(果冻豆)在它的Arduino UNO通讯

我使用的开发者指南从一两个月,但没有结果与设备进行通信。我觉得我缺少一些东西分钟。我想简单地显示我的freeduino板的供应商ID。我的清单文件看起来是这样的。

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.usb_host_final_t​​ry
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=com.example.usb_host_final_t​​ry.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
            &所述;意图滤光器>
        <作用机器人:名字=android.hardware.usb.action.USB_DEVICE_ATTACHED/>
    &所述; /意图滤光器>    <元数据机器人:名字=android.hardware.usb.action.USB_DEVICE_ATTACHED
        机器人:资源=@ XML / device_filter/>
        < /活性GT;
    < /用途>< /清单>

我已经在res / XML目录中创建的文件device_filter。它看起来像这样

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
    < USB设备厂商ID =0403产品ID =07d7/>
< /资源>

和我MainActivity.java具有以下code。

 包com.example.usb_host_final_t​​ry;进口的java.util.HashMap;进口android.hardware.usb.UsbDevice;
进口android.hardware.usb.UsbManager;
进口android.os.Build;
进口android.os.Bundle;
进口android.annotation.TargetApi;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.view.Menu;
进口android.widget.TextView;公共类MainActivity延伸活动{    @TargetApi(Build.VERSION_ codeS.HONEYCOMB_MR1)
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        意向意图=新的Intent();
        UsbManager经理=(UsbManager)getSystemService(Context.USB_SERVICE);
        HashMap的<弦乐,UsbDevice> DEVICELIST = manager.getDeviceList();
        UsbDevice设备= deviceList.get(设备名称);
        INT VID = device.getVendorId();
        TextView的电视=(的TextView)findViewById(R.id.textview);
        tv.setText(Integer.toString(VID));
    }

请帮我出我是从过去几个月搞砸了现在。我曾尝试这样做,但它也不能正常工作。

链接

链接

<一个href=\"http://stackoverflow.com/questions/12110555/how-to-detect-usb-device-in-android-tablet-which-acts-as-usb-host\">Stack overfolow问题

堆栈溢出问题

我已经尝试了所有这些,仍然没有能够成功。我也试着使用的if else循环,看看枚举的作品在那里我将文本设置为没有找到没有设备是别人的setText到供应商ID设备。在仿真器显示没有发现设备,但在我的平板电脑闻一连接设备,它强制关闭,否则它仍然显示没有设备发现文没有任何设备连接。

堆栈跟踪是在这里。


  

9月1日至5日:10:3​​5.364:W /微量(1658):从nativeGetEnabledTags意外的值:0
  9月1日至5日:10:3​​5.364:W /微量(1658):意外从nativeGetEnabledTags值:0
  9月1日至5日:10:3​​5.464:W /微量(1658):意外从nativeGetEnabledTags值:0
  9月1日至5日:10:3​​5.464:W /微量(1658):意外从nativeGetEnabledTags值:0
  9月1日至5日:10:3​​5.624:D / AndroidRuntime(1658):关闭VM
  9月1日至5日:10:3​​5.624:W / dalvikvm(1658):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a70930)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):致命异常:主要
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.usb_host_final_t​​ry / com.example.usb_host_final_t​​ry.MainActivity}:显示java.lang.NullPointerException
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.os.Handler.dispatchMessage(Handler.java:99)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.os.Looper.loop(Looper.java:137)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.ActivityThread.main(ActivityThread.java:5039)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在java.lang.reflect.Method.invokeNative(本机方法)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在java.lang.reflect.Method.invoke(Method.java:511)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在dalvik.system.NativeStart.main(本机方法)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):因:显示java.lang.NullPointerException
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在com.example.usb_host_final_t​​ry.MainActivity.onCreate(MainActivity.java:35)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.Activity.performCreate(Activity.java:5104)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  9月1日至5日:10:3​​5.644:E / AndroidRuntime(1658):11 ...更多
  9月1日至5日:10:46.0​​74:W /微量1712:意外从nativeGetEnabledTags值:0
  9月1日至5日:10:46.0​​74:W /微量(1712):意外从nativeGetEnabledTags值:0
  9月1日至5日:10:46.754:D / AndroidRuntime(1712):关闭VM
  9月1日至5日:10:46.754:W / dalvikvm(1712):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a70930)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):致命异常:主要
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.usb_host_final_t​​ry / com.example.usb_host_final_t​​ry.MainActivity}:显示java.lang.NullPointerException
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  9月1日至5日:10:46.766:E / AndroidRuntime 1712:在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.os.Handler.dispatchMessage(Handler.java:99)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.os.Looper.loop(Looper.java:137)
  9月1日至5日:10:46.766:E / AndroidRuntime 1712:在android.app.ActivityThread.main(ActivityThread.java:5039)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在java.lang.reflect.Method.invokeNative(本机方法)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在java.lang.reflect.Method.invoke(Method.java:511)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在dalvik.system.NativeStart.main(本机方法)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):因:显示java.lang.NullPointerException
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在com.example.usb_host_final_t​​ry.MainActivity.onCreate(MainActivity.java:35)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.app.Activity.performCreate(Activity.java:5104)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  9月1日至5日:10:46.766:E / AndroidRuntime(1712):11 ...更多
  9月1日至5日:10:49.104:I /流程(1712):发送信号。 PID:1712 SIG:9



解决方案

主要活动现在变成这个样子和休息的它仍然是相同的。

 包com.example.usb_host_final_t​​ry;进口的java.util.HashMap;
进口java.util.Iterator的;进口android.hardware.usb.UsbAccessory;
进口android.hardware.usb.UsbDevice;
进口android.hardware.usb.UsbManager;
进口android.os.Build;
进口android.os.Bundle;
进口android.annotation.TargetApi;
进口android.app.Activity;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.util.Log;
进口android.view.Menu;
进口android.widget.TextView;公共类MainActivity延伸活动{    受保护的静态最后弦乐TAG = NULL;    @TargetApi(Build.VERSION_ codeS.HONEYCOMB_MR1)
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        UsbManager经理=(UsbManager)getSystemService(Context.USB_SERVICE);
        HashMap的&LT;弦乐,UsbDevice&GT; DEVICELIST = manager.getDeviceList();
        迭代器&LT; UsbDevice&GT; deviceIterator = deviceList.values​​()迭代器()。
        而(deviceIterator.hasNext()){
            UsbDevice设备= deviceIterator.next();
            字符串s = device.getDeviceName();
            INT PID = device.getProductId();
            INT VID = device.getVendorId();
            TextView的电视=(的TextView)findViewById(R.id.textview);
            tv.setText(S +\\ n+ Integer.toString(PID)+\\ n+ Integer.toString(VID));
        }    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }}

一切都归功于奥列格Vaskevich!

I am trying to communicate with my freeduino board which is similar to arduino uno via usb through android device 'nexus 7' which has 4.2 (jelly beans) in it.

I used the developers guide to communicate with the device from a couple of months but with no result. I think i am missing something minute in it. I am trying to simply display the vendor id of my freeduino board. My manifest file look like this.

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.usb_host_final_try.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>

I have created a file device_filter in res/xml dir. which look like this

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device vendor-id="0403" product-id="07d7" />
</resources>

and my MainActivity.java has the following code.

package com.example.usb_host_final_try;

import java.util.HashMap;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        Intent intent = new Intent();
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        UsbDevice device = deviceList.get("deviceName");
        int vid=device.getVendorId();
        TextView tv = (TextView) findViewById(R.id.textview);
        tv.setText(Integer.toString(vid));
    }

please help me out i am screwed up over it from past few months now. I have tried doing this but it is also not working.

link

link

Stack overfolow question

stack overflow question

I have tried all these and still haven't been able to succeed. I also tried to use an if else loop to see if enumeration works where i set the text to device not found if no device is else settext to vendor id. On emulator is showed no device found but on my tablet wen i connect the device it force closes or else it still shows no device found wen nothing is connected.

The stack trace is here..

01-05 09:10:35.364: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.364: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.464: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.464: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:35.624: D/AndroidRuntime(1658): Shutting down VM 01-05 09:10:35.624: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 01-05 09:10:35.644: E/AndroidRuntime(1658): FATAL EXCEPTION: main 01-05 09:10:35.644: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usb_host_final_try/com.example.usb_host_final_try.MainActivity}: java.lang.NullPointerException 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.access$600(ActivityThread.java:141) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.os.Looper.loop(Looper.java:137) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.main(ActivityThread.java:5039) 01-05 09:10:35.644: E/AndroidRuntime(1658): at java.lang.reflect.Method.invokeNative(Native Method) 01-05 09:10:35.644: E/AndroidRuntime(1658): at java.lang.reflect.Method.invoke(Method.java:511) 01-05 09:10:35.644: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-05 09:10:35.644: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-05 09:10:35.644: E/AndroidRuntime(1658): at dalvik.system.NativeStart.main(Native Method) 01-05 09:10:35.644: E/AndroidRuntime(1658): Caused by: java.lang.NullPointerException 01-05 09:10:35.644: E/AndroidRuntime(1658): at com.example.usb_host_final_try.MainActivity.onCreate(MainActivity.java:35) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.Activity.performCreate(Activity.java:5104) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-05 09:10:35.644: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-05 09:10:35.644: E/AndroidRuntime(1658): ... 11 more 01-05 09:10:46.074: W/Trace(1712): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:46.074: W/Trace(1712): Unexpected value from nativeGetEnabledTags: 0 01-05 09:10:46.754: D/AndroidRuntime(1712): Shutting down VM 01-05 09:10:46.754: W/dalvikvm(1712): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 01-05 09:10:46.766: E/AndroidRuntime(1712): FATAL EXCEPTION: main 01-05 09:10:46.766: E/AndroidRuntime(1712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usb_host_final_try/com.example.usb_host_final_try.MainActivity}: java.lang.NullPointerException 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.access$600(ActivityThread.java:141) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.os.Looper.loop(Looper.java:137) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.main(ActivityThread.java:5039) 01-05 09:10:46.766: E/AndroidRuntime(1712): at java.lang.reflect.Method.invokeNative(Native Method) 01-05 09:10:46.766: E/AndroidRuntime(1712): at java.lang.reflect.Method.invoke(Method.java:511) 01-05 09:10:46.766: E/AndroidRuntime(1712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-05 09:10:46.766: E/AndroidRuntime(1712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-05 09:10:46.766: E/AndroidRuntime(1712): at dalvik.system.NativeStart.main(Native Method) 01-05 09:10:46.766: E/AndroidRuntime(1712): Caused by: java.lang.NullPointerException 01-05 09:10:46.766: E/AndroidRuntime(1712): at com.example.usb_host_final_try.MainActivity.onCreate(MainActivity.java:35) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.Activity.performCreate(Activity.java:5104) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-05 09:10:46.766: E/AndroidRuntime(1712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-05 09:10:46.766: E/AndroidRuntime(1712): ... 11 more 01-05 09:10:49.104: I/Process(1712): Sending signal. PID: 1712 SIG: 9

解决方案

the main activity will now become like this and rest of it remains the same

    package com.example.usb_host_final_try;

import java.util.HashMap;
import java.util.Iterator;

import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    protected static final String TAG = null;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @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();
            String s=device.getDeviceName();
            int pid= device.getProductId();
            int vid = device.getVendorId();
            TextView tv = (TextView) findViewById(R.id.textview);
            tv.setText(s+"\n"+Integer.toString(pid)+"\n"+Integer.toString(vid));
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

All thanks to oleg Vaskevich!!

这篇关于USB主机为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆