我正在尝试使用我尝试this的管道与JNI进行通信。
我的JNI
void createPipe(std::string path){
//pipe test
const char* PATH = path.c_str();
char* line = "Hello Pipe!";
int pipe;
int fd;
errno = 0;
__android_log_write(ANDROID_LOG_ERROR, "NDK_FOO_TAG 0", PATH);
// open a named pipe
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
pipe = mkfifo(PATH, mode);
if(errno != 0) {
__android_log_write(ANDROID_LOG_ERROR, "NDK_FOO_TAG 1", strerror(errno));
//fd = open(PATH, O_WRONLY);
}
__android_log_write(ANDROID_LOG_DEBUG, "NDK_FOO_TAG", "file opened successfully");
// actually write out the data and close the pipe
int err = write(pipe, line, strlen(line));
__android_log_write(ANDROID_LOG_DEBUG, "NDK_FOO_TAG Success",strerror(errno));
// close the pipe
close(pipe);
//-----------
}
原生电话
extern "C"
JNIEXPORT void JNICALL
Java_com_mynative_library_minterface_InitPipe(JNIEnv *env, jobject instance,
jstring pipePath_) {
const char *pipePath = env->GetStringUTFChars(pipePath_, 0);
createPipe(pipePath);
// TODO
env->ReleaseStringUTFChars(pipePath_, pipePath);
}
JAVA
private void pipeRead(){
try{
// Reader reader = new FileReader(PATH);
FileReader mReader = new FileReader(FIFO_PATH);
System.out.println("NativeCommunicator Started pipe reader waiting .2..");
BufferedReader mPipeReader= new BufferedReader(mReader);
System.out.println("NativeCommunicator Started pipe reader waiting ...");
while (mPipeReader.ready()) {
System.out.println("NativeCommunicator Data from Pipe : " + mPipeReader.readLine());
}
mPipeReader.close();
}catch (Exception e){
e.printStackTrace();
}
}
始终在FileReader mReader = new FileReader(FIFO_PATH);
堆栈跟踪
E/NDK_FOO_TAG 0: /data/user/0/com.myproject.pipe/files/pipe_0
11-29 19:37:15.821 32476-32476/com.myproject.pipe D/NDK_FOO_TAG: file opened successfully
11-29 19:37:15.821 32476-32476/com.myproject.pipe D/NDK_FOO_TAG Success: Success
11-29 19:39:32.471 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
11-29 19:39:32.689 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XTWiFi-PE] unknown deliver target [OS-Agent]
11-29 19:39:32.880 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
11-29 19:39:37.471 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]