Android的USB主机无法为Cp2102SerialDriver读取和写入数据 [英] Android USB Host not able to read and write data for Cp2102SerialDriver
问题描述
我的工作在Android上的主机应用程序,我需要从所有连接的设备读取数据。使用USB转串口线Cp2102SerialDriver。
I am working on Android host application where I need to read data from all attached device. Using usb to serial cable "Cp2102SerialDriver".
下面是我指的例子中的链接
Here is the example link which I referring
HTTP://$c$c.google.com / p / USB串行换的Android /
我能够成功,以显示与供应商和产品ID连接的设备,这里是样本code
I am successfully able to display attached devices with vendor and product ID, here is the sample code
/** 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的主机教程
I refer to the android host tutorial
http://developer.android.com/guide/topics/连接/ 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);
}
}
}
}
};
要写出我用这code数据
To write data I am using this code
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();
}
}
不过,虽然写我得到异常。在此先感谢这里的任何帮助。
But while writing I am getting exception. Thanks in advance for any help here.
推荐答案
现在我已经自己做了这一例外正在发生,因为我没有检查USB经理有权限或没有。我在上面提到的code添加所选设备列表上点击两行。
Now I have done it by myself that exception was occurring because I was not checking USB manager have permission or not. I have added two lines on click on selected device list on above mentioned code.
if (!mUsbManager.hasPermission(entry.device)) { mUsbManager.requestPermission(entry.device,pendingIntent); }
这篇关于Android的USB主机无法为Cp2102SerialDriver读取和写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!