我真的很困惑,为什么当我按下传感器时我有一个意想不到的0000数组(一个转换A / D的电路。数字数据从0000开始到1024取决于来自传感器的压力)。数字适配器应该发送来自压力传感器的任何信息。
我想附上一些MainActivity
代码。我认为问题是READ_MESSAGE案例内部处理程序。一旦数据发送到它,它也会减慢我的图形。我不确定runOnUiThread
在没有removeCallBack
的情况下是否正常工作。
我还需要pauseGraphUI()
。我不知道它的用途。
非常感谢。
这是我的代码:
@SuppressWarnings("UnusedAssignment")
public class MainActivity extends ActionBarActivity {
private void postData(final double time, final int pressure) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentLineGraph.appendData(new DataPoint(time, pressure), false, 1024);
}
});
}
public void pauseGraphUI() {
mHandler.removeCallbacks((Runnable) mCurrentLineGraph);
}
@SuppressLint("HandlerLeak")
public MainActivity() {
mHandler = new Handler() { // this the handler
//private double time = 0.0;
@Override
public void handleMessage(Message msg) {
byte[] readBuf;
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
Log.i(TAG, "STATE_CONNECTED: " + msg.arg1);
setStatus(getString(R.string.title_connected_to,
mConnectedDeviceName));
mDigitalInAdapter.clear();
startGraph();
break;
case BluetoothChatService.STATE_CONNECTING:
Log.i(TAG, "STATE_CONNECTING: " + msg.arg1);
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
Log.i(TAG, "STATE_LISTEN | STATE_NONE: " + msg.arg1);
setStatus(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
Log.i(TAG, "MESSAGE_WRITE: " + msg.arg1);
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mDigitalInAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ://here I'm reading...
Log.i(TAG, "MESSAGE_READ: " + msg.arg1);
readBuf = (byte[]) msg.obj; // Array that will back the new buffer.
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
ByteBuffer buffer = ByteBuffer.wrap(readBuf, 0, readBuf.length);
buffer.order(ByteOrder.BIG_ENDIAN);
Log.i(TAG, "digital read in: " + readMessage + ", str-" + pressure + " s-");
String[] strNumbers = readMessage.split("\r"); //stand for ASCII(13);
if (strNumbers.length > 0) {
Log.i(TAG, "readMessage = " + readMessage);
// if (strNumbers.length > 0) {
// Log.i(TAG, "digital after split: " + strNumbers[0] + ", length=" + strNumbers.length);
// } else {
// Log.i(TAG, "no data read!");
// }
readMessage = strNumbers[0];
lastInput = strNumbers[0];
try {
pressure = Integer.valueOf(readMessage);
} catch (NumberFormatException e) {
Log.e(TAG, "NumberFormatException - " + e.getMessage());
e.printStackTrace();
}
if (pressure >= 0) {
pushDataIntonAdapter("mDigitalInAdapter",mDigitalInAdapter,pressure);
float pound;// = 0x0f;
try {
pound = digitalToPoundConvert(pressure);
Log.i(TAG, String.format("Digital to Pound convert: %04d -> %4.2f", pressure, pound));
pushDataIntonAdapter("mPoundAdapter",mPoundAdapter,pound);
if (mPoundAdapter != null) {
// check if new pounds >= last recorded
if (mRecordingAdapter.getCount() > 0) {
float last_pounds;//= 0.0f;
float current_pounds;// = 0.0f;
last_pounds = Float.parseFloat(mRecordingAdapter.getItem(mRecordingAdapter.getCount() - 1));
current_pounds = Float.parseFloat(mPoundAdapter.getItem(mPoundAdapter.getCount() - 1));
if (current_pounds >= last_pounds) {
// turn data record back on
sendEmptyMessage(MESSAGE_ALERTDIALOG);
}
}
}
if (mCurrentLineGraph != null) {
postData(time, pressure);
time++;
// pauseGraphUI();
//onResume();
// mCurrentLineGraph.appendData(new DataPoint(time, pressure), false, 1000);
//noinspection NumericOverflow
//time = time + 1.0 /20.0;
}
} catch (Exception e) {
Log.e(TAG, "onProgressUpdate failed: " + e.getMessage());
}
} else {
Log.i(TAG, "The digital was - " + 0);
}
}
break;
case MESSAGE_DEVICE_NAME:
Log.i(TAG, "MESSAGE_DEVICE_NAME: " + msg.arg1);
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(),
"Connected to " + mConnectedDeviceName,
Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Log.i(TAG, "MESSAGE_TOAST: " + msg.arg1);
Toast.makeText(getApplicationContext(),
msg.getData().getString(TOAST), Toast.LENGTH_SHORT)
.show();
break;
case MESSAGE_ALERTDIALOG:
Log.i(TAG,"MESSAGE_ALERTDIALOG"+ msg.arg1);
AlerThreshold();
break;
}
}
};
}