Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android到Arduino USB通信:读取_Java_Android_Serial Port_Arduino_Usb - Fatal编程技术网

Java Android到Arduino USB通信:读取

Java Android到Arduino USB通信:读取,java,android,serial-port,arduino,usb,Java,Android,Serial Port,Arduino,Usb,我一直在看示例,但我仍然不清楚如何在我的Android应用程序中从usb端口读取数据。我希望它是事件/意图驱动的,但我对如何做到这一点非常迷茫。我有一个BroadcastReceiver,但我不确定我需要寻找什么意图。操作来捕获传入的数据。任何洞察都将不胜感激 编辑:为了澄清我的Android设备是作为USB主机运行的Nexus9,我正在寻找与Arduino Leonardo的通信 Edit2:此时,我有一个Arduino草图正在运行,每2秒通过串行发送一条消息。我有一个按钮,根据我的理解,按下

我一直在看示例,但我仍然不清楚如何在我的Android应用程序中从usb端口读取数据。我希望它是事件/意图驱动的,但我对如何做到这一点非常迷茫。我有一个BroadcastReceiver,但我不确定我需要寻找什么意图。操作来捕获传入的数据。任何洞察都将不胜感激

编辑:为了澄清我的Android设备是作为USB主机运行的Nexus9,我正在寻找与Arduino Leonardo的通信

Edit2:此时,我有一个Arduino草图正在运行,每2秒通过串行发送一条消息。我有一个按钮,根据我的理解,按下时应该读取缓冲区,但实际上什么都不做

    TextView displayMessages = (TextView)findViewById(R.id.textView);

    try
    {
        byte[] msgBytes = new byte[1000];
        connection.bulkTransfer(input, msgBytes, msgBytes.length, TIMEOUT);

        String msgString = new String(msgBytes, "UTF-8");// msgBytes.toString();
        displayMessages.setText(msgString);
    }
    catch (Exception e)
    {
        displayMessages.setText(e.getMessage());
    }
结果就是textView为空。如果我不进行转换并将字节[].toString()放入,我会得到十六进制值,每次按下都会改变,但我不知道如何解释


编辑3:只是一些更多的信息随着时间的推移,我已经禁用了Arduino Leonardo,显示为HID键盘输入设备。我只是修改了USBDesc.h以删除“#define HID_ENABLED”。这并没有改变现状。同时,我还实现了Physicaloid USB库,这也被证明是不成功的。我正在调试/转换mik3y的android库USB串行接口,但到目前为止还无法进行测试

您的android设备可以作为一个应用程序使用。 因此,根据您是否将手机用作usb主机,您可以在清单中使用以下意图和权限

<uses-feature android:name="android.hardware.usb.host" />
<activity ... usb host...>
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
            <meta-data
            android:resource="@xml/device_filter"
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
你需要注册你的接收者

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

    mPermissionIntent =
            PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
然后创建函数来读取和写入数据

private Byte[] bytes
private static int TIMEOUT = 0;
private boolean forceClaim = true;

...

UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device); 
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //do in another thread
您需要修改此代码以将数据带到您想要的位置。 提供了有关处理传输线程的详细讨论

此代码已从android.com文档中获取并部分修改


同样,也有一个类似的过程将设备设置为

是的,所以我以前看过这段代码,但我仍然不明白这与读取传入数据有什么关系。据我所知,这只是设置设备通信权限,而不是从USB读取任何内容。好的,我已经设置好了所有这些,我可以将数据发送到我的设备并接收数据。已经阅读了你在Android文档中发布的所有内容,阅读仍然不清楚,这就是我来这里的原因。从看起来的情况来看,文档暗示我不能进行基于事件/意图的读取,我必须不断地从USB端口异步读取数据,这看起来是一个糟糕的解决方案。必须有一个更好的方法来做到这一点,这就是我想知道的。我也没有看到从串行端口读取的新链接。我是否只需要创建一个从端口读取数据的持续运行的线程?我很难接受没有事件驱动的方式来实现这一点。
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

    mPermissionIntent =
            PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
private Byte[] bytes
private static int TIMEOUT = 0;
private boolean forceClaim = true;

...

UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device); 
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //do in another thread