2017-08-11 20 views
-5

我有一個應用程序,偵聽來自HC 05藍牙模塊的傳入串行數據。我想通過編輯文本設置某個值時,應用程序會將此值與藍牙模塊的輸入值進行比較。當來自藍牙模塊的數值超過用戶設置的值時,振動器服務被調用。當條件在Android

void beginListenForData() { 
    final Handler handler = new Handler(); 
    final byte delimiter = 10; // This is the ASCII code for a newline 
    // character 

    stopWorker = false; 
    readBufferPosition = 0; 
    readBuffer = new byte[1024]; 
    workerThread = new Thread(new Runnable() { 
     public void run() { 
      while (!Thread.currentThread().isInterrupted() && !stopWorker) { 
       try { 
       int bytesAvailable = mmInputStream.available(); 
        if (bytesAvailable > 0) { 
         byte[] packetBytes = new byte[bytesAvailable]; 
         mmInputStream.read(packetBytes); 
         for (int i = 0; i < bytesAvailable; i++) { 
          byte b = packetBytes[i]; 
          if (b == delimiter) { 
           byte[] encodedBytes = new byte[readBufferPosition]; 
           System.arraycopy(readBuffer, 0, 
             encodedBytes, 0, 
             encodedBytes.length); 
           final String data = new String(
             encodedBytes, "US-ASCII"); 
           readBufferPosition = 0; 

           handler.post(new Runnable() { 
            public void run() { 
             myLabel.setText(data); 
             Button btn = (Button) findViewById(R.id.user_usage); 
             btn.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) { 
                 int dt = Integer.parseInt(data); 
                 int us = Integer.parseInt(txt.getText().toString()); 
                 Toast.makeText(getApplicationContext(), "Usage set", Toast.LENGTH_SHORT).show(); 
                 if (dt>us) 
                 { 
                  Toast.makeText(getApplicationContext(), "Times Up", Toast.LENGTH_SHORT).show(); 
                  Vibrator vib = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); 
                  vib.vibrate(2000); 
                 } 
                } 
                } 
             ); 
              }}); 
          } 

如果用戶設置的值大於藍牙的值,上面的代碼只會調用振動器服務。

回答

1

經過多次編輯,我建議學習......很多。我知道一個事實,就是這段代碼直接在網上提供的教程中使用過。雖然把它作爲基礎很好,但如果你沒有充分理解你實際在做什麼,那麼完成這項任務就太困難了。

編輯::

有將是一個很大的變化在這裏,因爲你已經犯了很多錯誤的。

public class My_activity extends Activity { 

    ... 
    // move data, making it a member variable of your activity 

    String data = null; 

    ... 

    public void onCreate(Bundle savedInstanceState) { 
     // move your btn click code to on create 
     Button btn = (Button) findViewById(R.id.user_usage); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // begin bluetooth check with newly add loop 
       beginListenForData(); 
       // call your new checkData method 
       checkData(); 
      } 
     }); 
    } 
} 

// create a method for checking bt data 
public void checkData() { 
    if (data != null && txt.getText().toString().length > 0) { 
     int dt = Integer.parseInt(data); 
     int us = Integer.parseInt(txt.getText().toString()); 
     Toast.makeText(getApplicationContext(), "Usage set", Toast.LENGTH_SHORT).show(); 
     if (dt>us) { 
      Toast.makeText(getApplicationContext(), "Times Up", Toast.LENGTH_SHORT).show(); 
      Vibrator vib = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); 
      vib.vibrate(2000); 
     } 
    } 
} 

// edited method for checking bt data 
public void beginListenForData() { 
    final Handler handler = new Handler(); 
    final byte delimiter = 10; // This is the ASCII code for a newline 
    // character 
    stopWorker = false; 
    readBufferPosition = 0; 
    readBuffer = new byte[1024]; 
    workerThread = new Thread(new Runnable() { 
     public void run() { 
      while (!Thread.currentThread().isInterrupted() && !stopWorker) { 
       try { 
        int bytesAvailable = mmInputStream.available(); 
         if (bytesAvailable > 0) { 
          byte[] packetBytes = new byte[bytesAvailable]; 
          mmInputStream.read(packetBytes); 
          for (int i = 0; i < bytesAvailable; i++) { 
           byte b = packetBytes[i]; 
           if (b == delimiter) { 
            byte[] encodedBytes = new byte[readBufferPosition]; 
            System.arraycopy(readBuffer, 0, 
            encodedBytes, 0, 
            encodedBytes.length); 
            data = new String(
            encodedBytes, "US-ASCII"); 
            readBufferPosition = 0; 

            handler.post(new Runnable() { 
             public void run() { 
              myLabel.setText(data); 
              checkData(); 
             } 
            }); 
           } 
          } 
         } 
        // this is the end of your try block 
        // you didn't give me anything else to work with past this point 
        } // catch() 
+0

我不認爲你已經理解了這個問題。我想在**一個值大於另一個時檢查**。 –

+0

你想檢查一個更大的用戶輸入,而不是點擊按鈕嗎? – anomeric

+0

是的,我想這樣做 –