fileinputstream.available() is device dependent.
The way I worked around it was by having a dedicated read thread.
On Saturday, July 21, 2012 4:22:29 AM UTC-5, 4ntoine wrote:
>
> Hi, evebody!
>
> I'm playing with Android ADK and have strange problem with my Galaxy Tab 2.
>
> After opening accessory i create inputand output stream and start reading
> from input stream.
> On the first inStream.available() invocation i have exception:
> ---
> java.io.IOException: ioctl failed: EINVAL (Invalid argument)
> at libcore.io.IoBridge.available(IoBridge.java:68)
> at java.io.FileInputStream.available(FileInputStream.java:110)
> at
> name.antonsmirnov.firmata.serial.StreamingSerialAdapter$ReadingThread.run(StreamingSerialAdapter.java:81)
> Caused by: libcore.io.ErrnoException: ioctl failed: EINVAL
> (Invalid argument)
> at libcore.io.Posix.ioctlInt(Native Method)
> at libcore.io.ForwardingOs.ioctlInt(ForwardingOs.java:75)
> at libcore.io.IoBridge.available(IoBridge.java:52)
> ... 2 more
> ---
>
> The sources are:
> public class UsbAccessorySerialAdapter extends StreamingSerialAdapter {
>
> private UsbManager manager;
> private UsbAccessory accessory;
> private ParcelFileDescriptor parcelFileDescriptor;
>
> public UsbAccessorySerialAdapter(UsbManager manager, UsbAccessory
> accessory) {
> this.manager = manager;
> this.accessory = accessory;
> }
>
> @Override
> public void start() throws SerialException {
> try {
> parcelFileDescriptor = manager.openAccessory(accessory);
> if (parcelFileDescriptor == null)
> throw new RuntimeException("Failed to open USB accessory");
>
> FileDescriptor fileDescriptor =
> parcelFileDescriptor.getFileDescriptor();
> setInStream(new FileInputStream(fileDescriptor));
> setOutStream(new FileOutputStream(fileDescriptor));
> } catch (Exception e) {
> throw new SerialException(e);
> }
> super.start();
> }
>
> @Override
> public void stop() throws SerialException {
> setStopReading();
>
> try {
> if (parcelFileDescriptor != null)
> parcelFileDescriptor.close();
> } catch (Exception e) {
> throw new SerialException(e);
> }
> super.stop();
> }
> }
>
> ---
>
> reading thread code:
> /**
> * Threads, that reads InputStream
> */
> private class ReadingThread extends Thread implements
> Thread.UncaughtExceptionHandler{
>
> public ReadingThread() {
> setUncaughtExceptionHandler(this);
> }
>
> public void uncaughtException(Thread t, Throwable e) {
> handleException(e);
> }
>
> private void handleException(Throwable e) {
> if (!shouldStop.get())
> for (ISerialListener eachListener : listeners)
> eachListener.onException(e);
> }
>
> @Override
> public void run() {
>
> while (!shouldStop.get()) {
> try {
> if (inStream.available() > 0) // exception here!
> for (ISerialListener eachListener : listeners)
>
> eachListener.onDataReceived(StreamingSerialAdapter.this);
> } catch (IOException e) {
> handleException(e);
> break;
> }
> }
>
> try {
> inStream.close();
> } catch (IOException e) {}
> }
> }
>
> Is it hardware or Android OS problem? It seems that writing to outStream
> is okay, since writing-only code works. Any thoughts?
>
--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.