1

인텔 에디슨에서 실행되는 C에서 "레이스 박스"를 쓰고 있습니다. 그것은 블루투스 채팅을 기반으로 안드로이드 애플 리케이션과 블루투스 데이터를 보내거나받을 수 있습니다. 모든 것이 잘 작동합니다. 나는 보내고받을 수있다. 연결을 끊고 다시 가져 오면 연결이 다시 돌아오고 데이터가 C 프로그램에서 Android 앱으로 이동합니다. 다시 연결되면 C에서 Android로 더 이상 데이터를 보낼 수 없습니다. 이 읽기 진술을 추적하고 while 루프를 끝내지 않기 때문에 절대 값을 반환하지 않는다고 가정합니다. 특히,이 라인 :블루투스 연결이 끊긴 경우 블루투스 읽기 스레드가 종료되지 않습니다

while(bluetooth_up == 1 && (read(client, &aa, 1) == -1)){ 

내가 bluetooth_up == 0 (다른 스레드 fprints이 bluetooth_up와 블루투스가 다운 때 0) 알고있는 경우에도 종료하지 않음. 나의 결론은 읽기가 너무 차단 된 나는 라인

fcntl(s, F_SETFL,sock_flags|O_NONBLOCK); 

블루투스 연결이 블루투스 쓰기 스레드에와 그 문제를 해결하려고 시도이었다. 하지만 내가 말했듯이, bluetooth_up이 0 일 때 루프가 종료되지 않는 것을 제외하고는 모든 것이 작동합니다.

내가 알아들을 수있는 것은 내가 읽는 것이 차단되고 내가 알아 내지 못하는 것은 차단하지 않는 방법입니다. -1을 반환하고 while 루프는 bluetooth_up == 0을보고 종료 할 수 있습니다. 여기

는 bluetooth_up의 글로벌 정의입니다

volatile int bluetooth_up = 0; 

가 강력 할 필요가 내가 블루투스가 다시 작동하는 전원을 껐다가 사람들에게 경주 상자를 필요로하지 않기 때문에 나는이에 도움을 주셔서 감사합니다 것입니다 , 그것이 작동합니다.

void *blueRead(void *arg){ 
    blue_read_up = 1; 
    char aa; 
    char buffer[500]; 
    int idx = 0; 
    printf("\nBlue Read started\n"); 
    fcntl(s, F_SETFL,sock_flags|O_NONBLOCK); 
    while(bluetooth_up == 1){ 
     if (new_blue_read_sentence == 0 && bluetooth_up == 1){ 
      while(bluetooth_up == 1 && (read(client, &aa, 1) == -1)){ 
        if(bluetooth_up == 0){ 
         printf("\nExiting blue read\n"); 
         blue_read_up = 0; 
         return NULL; 
        } 
      } 
      printf("%i",aa); 
      if(aa != '\n') 
      { 
       buffer[idx++] = aa; 
      } 
      else 
      { 
       buffer[idx] = '\n'; 
       buffer[idx + 1] = '\0'; 
       idx = 0; 
       strcpy(blue_read_buffer, buffer); 
       new_blue_read_sentence = 1; 
      } 
     } 
    } 
    printf("\nExiting blue read 2\n"); 
    blue_read_up = 0; 
    return NULL; 
} 

나는 블루투스 연결 코드는 꽤 표준 생각하지만, 당신은 수신 대기 소켓 (들)에 O_NONBLOCK을 설정하는

 // allocate socket 
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 
    printf("\nsocket = %i\n", s); 

    // bind socket to port 1 of the first available local bluetooth adapter 
    loc_addr.rc_family = AF_BLUETOOTH; 
    loc_addr.rc_bdaddr = *BDADDR_ANY; 
    loc_addr.rc_channel = (uint8_t)1; 
    retval = bind(s, (struct sockaddr*)&loc_addr, sizeof(loc_addr)); 
    printf("\nbind = %i\n", retval); 

    // put socket into listening mode 
    //listen(s, 1); 
    retval = listen(s, 1); 
    printf("\nlisten = %i\n", retval); 

    // accept one connection 
    client = accept(s, (struct sockaddr*)&rem_addr, &opt); 

    sock_flags = fcntl(s,F_GETFL,0); 
    fcntl(s, F_SETFL,sock_flags|O_NONBLOCK); 

    printf("\n1 - client connect = %i socket %i\n", client, s); 
+0

pthread_cancel을 사용하여 전화를 끊지 않고 작동하는 다른 스레드에서 스레드를 종료 할 수 있지만 그 방법은 올바른 방법이 아닌 것 같습니다. 아마 그것이 적어도 작동합니다. –

답변

0

여기 있습니다. 대신 실제로 읽혀지는 소켓 인 accept 소켓 (클라이언트)을 시도하십시오.

+0

Android 앱인 화자는 아무런 문제가 없습니다. C 애플리케이션이 pthread_cancel을 통해 루프를 떠날 수있을 때, 안드로이드 측을 만지지 않고도 모든 것이 잘 동작합니다. 취소 솔루션이 작동 중이고, 나는 그것이 깨끗하다고 ​​생각하지 않습니다. –

관련 문제