How to use unix pipes in Android

前端 未结 2 1964
天涯浪人
天涯浪人 2021-02-04 12:40

I need to send some data to a C program from my app in Android, and I think about using pipes. I read that Java can access to existing pipes (and open them as if it\'s a normal

相关标签:
2条回答
  • 2021-02-04 13:32

    Ok, I tried to fix the problem with the most stupid app that exists. You can find this one as a gist on github.

    So far, I discover this :

    • The only place where the pipe works is the app folder (ie /data/data/package.full.name/)
    • If you want to pass data to another program, you had better to launch it as a child of your app to ensure they are in the same group and thus have the same authorisation for the folder. If you can't, you might be able to play with the groups (do ls -l -a on /data/data/ and have a look to the group name).

    DO NOT FORGET : You can't actually write in the pipe until someone is listening at the other side. So if you test the file I posted on github, you will have that kind of logcat result.

    I/ActivityManager(  220): Start proc fr.stackr.android.upt for activity fr.stackr.android.upt/.UnixPipeActivity: pid=1359 uid=10048 gids={}
    I/UPIPE   ( 1359): Attempt started
    W/ActivityManager(  220): Launch timeout has expired, giving up wake lock!
    W/ActivityManager(  220): Activity idle timeout for HistoryRecord{4643c8b8 fr.stackr.android.upt/.UnixPipeActivity}
    

    Here, the system pause because nothing happens… Then I run cat v_pipe on the phone.

    V/UPIPE   ( 1359): SEND :: Try to write the first paragraph ....
    V/UPIPE   ( 1359): SEND :: Bip
    V/UPIPE   ( 1359): Flushing...
    V/UPIPE   ( 1359): SEND :: Bip post flush
    V/UPIPE   ( 1359): Closing…
    I/UPIPE   ( 1359): Attempt ended
    

    That's done.

    closing : when I close the OutputStreamWriter, the listening side (ie cat) ends. If I commment the line, cat will still wait for input.

    flushing : seems to be important if you intent to get something without calling close.

    Carriage Return : Use \n.

    0 讨论(0)
  • 2021-02-04 13:32

    I think you can use ParcelFileDescriptor.createPipe()

    It will return an array of pipe for read and write. For more information, visit the developers website.

    0 讨论(0)
提交回复
热议问题