gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:11:24 26 4
gpt4 key购买 nike

我正在开发 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com