0

In C language, I want to develop an application to run bluetoothctl, and then send command to it and receive the info, like we run bluetoothctl in console.

int main(void)
{
int fd, err = 0;
char* tty;

wait_pid = -1;
printf("BLEapp program start.......\r\n");
if (wait_pid == -1) {
    printf("Start to run bluetoothctl......\r\n");
    if ((pid = fork()) < 0) {
        err = 1;
        printf("Create new progress failed.\r\n");
    }
    else if (pid == 0) {
        // Child progress, run as default when xid = 0x0 and mastAddr = 0x0
        execl("/root/bluez-5.42/client/bluetoothctl", "bluetoothctl", NULL);
    }

}
else {
    printf("The child procress is still running.....\r\n");
    err = 1;
}

//tty = ttyname(STDIN_FILENO);
//printf("stdin name is %s.\r\n", tty);
//fd = open(tty, O_WRONLY);
//if (fd < 0) {
//    printf("Open stdin error..............\r\n");
//}
while (1) {
    sleep(2);
    fputs("devices\n", stdin);
    //write(fd, "devices\n", 8);
}


return err;
}

I run bluetoothctl as child process, and want to send "devices\n" command to it, and read the listed devices. But it doesn't work.

Please help me to fix this problem.

Thanks.

Mian
  • 23
  • 3

0 Answers0