2017-08-11 7 views
-5

私は、hc 05 Bluetoothモジュールからの着信シリアルデータをリッスンするアプリケーションを持っています。私は、ある値が編集テキストを介して設定されたときに、この値をブルートゥースモジュールからの入力と比較する状況を作りたいと思います。ブルートゥースモジュールからの値がユーザによって設定された値を超えると、バイブレータサービスが呼び出される。ユーザによって設定された値は、Bluetoothからそれよりも大きい場合にのみ、上記条件が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

はい私はそれをしたいと思っています –

関連する問題