0

私のAndroidアプリケーションでは、Bluetoothを介してEmbedded Hardwareデバイスとの間でデータを送受信します。私はこれを達成するために背景サービスを使用します。ブルートゥースデータを読み取るには、私は私のハードウェアユニットにクエリーを送信すると、Bluetoothを介してデータを待っていることでしょうが、私は、私はまでプログレスバーを使用し、タイムアウトをチェックする方法を知っているdidnot私の次のコード、AndroidでBluetoothを使用してデータを受信中にタイムアウトを検出する方法

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; 
      } 
     } 
    } 

を使用します私はデータを受信しますが、時には私は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; 
      } 
     } 
    } 
関連する問題