java - Android USB 主机无法读取和写入 Cp2102SerialDriver 数据

标签 java android serial-port usb

我正在开发 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();
                }
            }

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

最佳答案

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

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

关于java - Android USB 主机无法读取和写入 Cp2102SerialDriver 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103195/

相关文章:

Java/MySQL 插入导致 OutofMemory 异常

View可见性和不可见性的Android动画

java - 词法分析器

java - NoSQL DAO 实现

Android - 丢失传入(高速)USB 数据

android - 删除 Firebase 监听器在 Android 上不起作用

node.js - 错误 : Could not locate the bindings file. 已尝试:

objective-c - Objective-C : Resource busy 中的串行通信错误

python - python从串口读取数据

java - 如何使用 Sikuli Java Standalone jar 输入特殊键?