Reputation: 948
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://code.google.com/p/usb-serial-for-android/
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));
}
}
I refer to the android host tutorial
http://developer.android.com/guide/topics/connectivity/usb/host.html
to communicate with the devices
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);
}
}
}
}
};
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.
Upvotes: 2
Views: 2866
Reputation: 948
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); }
Upvotes: 2