-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);
}
}
}
);
}});
}
如果用戶設置的值大於藍牙的值,上面的代碼只會調用振動器服務。
我不認爲你已經理解了這個問題。我想在**一個值大於另一個時檢查**。 –
你想檢查一個更大的用戶輸入,而不是點擊按鈕嗎? – anomeric
是的,我想這樣做 –