2017-10-31 28 views
0

안드로이드 애플리케이션에서 블루투스를 통해 임베디드 하드웨어 장치로 /로부터 데이터를 송수신합니다. 이를 위해 서비스 배경을 사용합니다. 블루투스 데이터를 읽으려면, 난 내 아래 코드를 사용안드로이드에서 블루투스를 통해 데이터를 수신하는 동안 시간 초과를 감지하는 방법

public void run() { 
     Log.d("DEBUG BT", "IN CONNECTED THREAD RUN"); 

     byte[] buffer = new byte[10240]; //1kb=1024bytes 
     int begin = 0; 
     int bytes = 0; 

     // Keep listening to the InputStream until an exception occurs. 

     while (true) { 
      try { 
       System.out.println("-------------New Data received"); 
       al = new ArrayList<>(); 

       try { 
        Thread.sleep(300); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        System.out.println("InterruptedException during sleep"); 
       } 

       // Read from the InputStream. 

       bytes += mmInStream.read(buffer, bytes, buffer.length - bytes); 

        for(int i = begin; i < bytes; i++) { 
         if(buffer[i] == "#".getBytes()[0]) { 

          // Send the obtained bytes to the UI activity. 
          bluetoothIn.obtainMessage(1, begin, i, buffer).sendToTarget(); 
          begin = i + 1; 
          if(i == bytes - 1) { 
           bytes = 0; 
           begin = 0; 
          } 
         } 
        } 
      } catch (IOException e) { 
       e.printStackTrace(); 
       System.out.println("Problem in reading Data:Input stream was disconnected "+e.getMessage()); 
       Handler hand = new Handler(Looper.getMainLooper()); 
       hand.post(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(BluetoothDataService.this.getApplicationContext(),"Problem in reading Data",Toast.LENGTH_SHORT).show(); 
         dismissDialog(); 
        } 
       }); 
       break; 
      } 
     } 
    } 
내 하드웨어 장치에 쿼리를 전송하고 블루투스를 통해 데이터를 기다리고있을 것입니다,하지만 난 시간 제한을 확인하는 방법을 알고 didnot

, 나는 진행 표시 줄까지를 사용 나는 데이터를 받지만 때로는 내가 20 초 이상 동안 데이터를받지 못한다면 "타임 아웃"으로 사용자에게 경고해야하지만, 내 진행 상황은 여전히로드 중이다. 나는 이것을 달성하는 방법을 고수하고있다. 누군가가 타임 아웃을 확인하는 방법을 제안한다.

답변

0
public void run() { 
     Log.d("DEBUG BT", "IN CONNECTED THREAD RUN"); 

     byte[] buffer = new byte[10240]; //1kb=1024bytes 
     int begin = 0; 
     int bytes = 0; 
     int timeOut = 10000; 
     int currTime = 0; 
     int interval = 50; 

     // Keep listening to the InputStream until an exception occurs. 

     while (true) { 
      try { 
       al = new ArrayList<>(); 

       try { 
        Thread.sleep(300); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        System.out.println("InterruptedException during sleep"); 
       } 

       // Read from the InputStream. 
       if(mmInStream.available() > 0){ 
        // something just arrived? 

        bytes += mmInStream.read(buffer, bytes, buffer.length - bytes); 

        currTime = 0;        // resets the timeout 

        for(int i = begin; i < bytes; i++) { 
         if(buffer[i] == "#".getBytes()[0]) { 

          // Send the obtained bytes to the UI activity. 
          bluetoothIn.obtainMessage(1, begin, i, buffer).sendToTarget(); 
          begin = i + 1; 
          if(i == bytes - 1) { 
           bytes = 0; 
           begin = 0; 
          } 
         } 
        } 
       }else if(currTime < timeOut) { 
        // do we have to wait some more? 
        try { 
         Thread.sleep(interval); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
         System.out.println("InterruptedException occurs while waiting for data from TPRS, "+e.getMessage()); 
         printMessage("InterruptedException occurs while waiting for data from TPRS, "); 
        } 
        currTime += interval; 
       }else { 
        // timeout detected 
        handleTimeOut(); 
        dismissDialog(); 
        currTime = 0;       // resets the timeout 
       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
       System.out.println("Problem in reading Data:Input stream was disconnected "+e.getMessage()); 
       printMessage("Problem in reading Data"); 
       break; 
      } 
     } 
    }