Java. io. IOException, “bad file number” USB connection
I'm setting up a USB connection between my Android phone and another device Now just send bytes to test I get some clear communication, but it always ends up in Java io. IOException: write failure: ebadf (wrong file number) "after one second, sometimes reading remains alive, but writing dies; the other two die
I didn't do anything super fancy, read and write like Google Docs:
Initial connection (in the broadcast receiver, I know this part is at least initial)
if (action.equals(ACTION_USB_PERMISSION)) { ParcelFileDescriptor pfd = manager.openAccessory(accessory); if (pfd != null) { FileDescriptor fd = pfd.getFileDescriptor(); mIn = new FileInputStream(fd); mOut = new FileOutputStream(fd); } }
Read:
Thread thread = new Thread(new Runnable() { @Override public void run() { byte[] buf = new byte[BUF_SIZE]; while (true) { try { int recvd = mIn.read(buf); if (recvd > 0) { byte[] b = new byte[recvd]; System.arraycopy(buf,b,recvd); //Parse message } } catch (IOException e) { Log.e("read error","Failed to read from stream"); e.printStackTrace(); } } } }); thread.start();
Writing:
synchronized(mWriteLock) { if (mOut !=null && byteArray.length>0) { try { //mOut.flush(); mOut.write(byteArray,byteArray.length); } catch (IOException e) { Log.e("error","error writing"); e.printStackTrace(); return false; } } else { Log.e(TAG,"Can't send data,serial stream is null"); return false; } }
Error stack trace:
java.io.IOException: write Failed: EBADF (Bad file number) W/System.err(14028): at libcore.io.IoBridge.write(IoBridge.java:452) W/System.err(14028): at java.io.FileOutputStream.write(FileOutputStream.java:187) W/System.err(14028): at com.my.android.transport.MyUSBService$5.send(MyUSBService.java:468) W/System.err(14028): at com.my.android.transport.MyUSBService$3.onReceive(MyUSBService.java:164) W/System.err(14028): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:781) W/System.err(14028): at android.os.Handler.handleCallback(Handler.java:608) W/System.err(14028): at android.os.Handler.dispatchMessage(Handler.java:92) W/System.err(14028): at android.os.Looper.loop(Looper.java:156) W/System.err(14028): at android.app.ActivityThread.main(ActivityThread.java:5045) W/System.err(14028): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err(14028): at java.lang.reflect.Method.invoke(Method.java:511) W/System.err(14028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) W/System.err(14028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) W/System.err(14028): at dalvik.system.NativeStart.main(Native Method) W/System.err(14028): Caused by: libcore.io.ErrnoException: write Failed: EBADF (Bad file number) W/System.err(14028): at libcore.io.Posix.writeBytes(Native Method) W/System.err(14028): at libcore.io.Posix.write(Posix.java:178) W/System.err(14028): at libcore.io.BlockGuardOs.write(BlockGuardOs.java:191) W/System.err(14028): at libcore.io.IoBridge.write(IoBridge.java:447) W/System.err(14028): ... 13 more
I've recorded the whole place, so I know it's not obvious, such as receiving another license request (so the file stream is reinitialized as an intermediate read) The stream is also not closed because I have never happened anywhere in my code (now) I don't get any separate or attached events (I record if it happens) Nothing seems too common; It's just dead
I thought it might be a concurrency problem, so I played with locks and slept without any work, I tried I don't think this is a throughput problem because it still happens every time I read (at both ends) and read a single packet at a time (ultra slow bit rate) Does the buffer have a chance to overspend at the other end? How can I solve this problem? I can access the code at the other end, which is also an Android device, using host mode If it is important, I can publish this code - standard batch transfer
Does the phone lack support for Android accessory mode? I tried two mobile phones and they failed, so I suspect so
I want to know why this error is usually caused when writing or reading from USB on Android?
Solution
It eventually becomes a threading problem I need to separate and even write better than reading
I'm based on this code