【问题标题】:Android USB Host not able to read and write data for Cp2102SerialDriverAndroid USB 主机无法读取和写入 Cp2102SerialDriver 的数据
【发布时间】:2014-04-01 22:18:22
【问题描述】:

我正在开发需要从所有连接设备读取数据的 Android 主机应用程序。使用 USB 转串口线“Cp2102SerialDriver”。

这是我引用的示例链接

http://code.google.com/p/usb-serial-for-android/

我能够成功显示带有供应商和产品 ID 的附加设备,这里是示例代码

/** Simple container for a UsbDevice and its driver. */
private static class DeviceEntry {
    public UsbDevice device;
    public UsbSerialDriver driver;

    DeviceEntry(UsbDevice device, UsbSerialDriver driver) {
        this.device = device;
        this.driver = driver;
    }
}

final List<DeviceEntry> result = new ArrayList<DeviceEntry>();
            for (final UsbDevice device : mUsbManager.getDeviceList()
                    .values()) {

                final List<UsbSerialDriver> drivers = UsbSerialProber
                        .probeSingleDevice(mUsbManager, device);

                Log.d(TAG, "Found usb device: " + device);
                if (drivers.isEmpty()) {
                    Log.d(TAG, "  - No UsbSerialDriver available.");
                    result.add(new DeviceEntry(device, null));
                } else {
                    for (UsbSerialDriver driver : drivers) {
                        Log.d(TAG, "  + " + driver);
                        result.add(new DeviceEntry(device, driver));
                    }
                }

我参考了android主机教程

http://developer.android.com/guide/topics/connectivity/usb/host.html

与设备通信

mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Log.d(TAG, "Pressed item " + position);
            if (position >= mEntries.size()) {
                Log.w(TAG, "Illegal position.");
                return;
            }

            final DeviceEntry entry = mEntries.get(position);


            if (entry.device != null) {

                IntentFilter intentfilter = new IntentFilter(
                        "com.android.example.USB_PERMISSION");
                DeviceListActivity.this.registerReceiver(
                        mUsbReceiverPermission, intentfilter);

                Toast.makeText(DeviceListActivity.this,
                        " Registered for broadcast reciever ",
                        Toast.LENGTH_SHORT).show();
                sendData();

            }


        }
    });


private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiverPermission = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        Toast.makeText(getApplicationContext(), action, Toast.LENGTH_SHORT)
                .show();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = (UsbDevice) intent
                        .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                 if (intent.getBooleanExtra(
                UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                if (device != null) {
                    usbDeviceConnection = mUsbManager.openDevice(device);
                    cp2102SerialDriver = new Cp2102SerialDriver(device,
                            usbDeviceConnection);
                    try {
                        cp2102SerialDriver.open();

                        Toast.makeText(getApplicationContext(),
                                "cp2102SerialDriver is open successfully",
                                Toast.LENGTH_SHORT).show();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                 } else {
                 Log.d(TAG, "permission denied for device " + device);

                 }
            }
        }
    }
};

要写入数据,我正在使用此代码

String data = etText.getText().toString() + "/n";

                bytes = data.getBytes(Charset.forName("UTF-8"));
                try {
                    cp2102SerialDriver.write(bytes, 3000);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

但是在写作时我遇到了异常。在此先感谢您的帮助。

【问题讨论】:

  • 那会是什么异常呢?请将 logcat 中异常的堆栈跟踪编辑到您的帖子中,这样我们就不必猜测它可能是什么。
  • 不是 Chris,是串口驱动异常 接口 CommonUsbSerialDriver 抛出
  • 如果您需要帮助,您仍然需要发布异常日志。
  • 感谢您对这里的兴趣,我认为上面的代码足以提供帮助。现在我自己完成了这个异常发生,因为我没有检查 USB 管理器是否有权限。我添加了两行 if (!mUsbManager.hasPermission(entry.device)) { mUsbManager.requestPermission(entry.device,pendingIntent); }

标签: java android serial-port usb


【解决方案1】:

现在我自己完成了发生异常,因为我没有检查 USB 管理器是否有权限。我在上面提到的代码上单击所选设备列表时添加了两行。

if (!mUsbManager.hasPermission(entry.device)) { mUsbManager.requestPermission(entry.device,pendingIntent); }

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    相关资源
    最近更新 更多