做一个计步器,使用的是单片机加传感器来采集加速度值,采样频率是一秒钟20组数据(一组数据包含加速度三轴值),我
在安卓端使用套接字来监听蓝牙数据:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[11];
String readMessage;
int realCount = 0;
// Keep listening to the InputStream while connected
while (true) {
try {
while (realCount < 11) {
realCount += mmInStream.read(buffer,realCount,11 - realCount);
}
Message msg = new Message();
Bundle data = new Bundle();
readMessage = bytesToHexString(buffer);
//data.putByteArray("ByteData",buffer);
data.putString("BTdata",readMessage);
msg.what = MainActivity.MESSAGE_READ;
msg.setData(data);
mHandler.sendMessage(msg);
// if (buffer[0] == DATA_START_BYTE ) {
// if (buffer[1] == ACCELDATA_START_BYTE) {
//
// }
// }
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
public String bytesToHexString(byte[] bytes) {
String result = "";
for (int i = 0; i < bytes.length; i++) {
String hexString = Integer.toHexString(bytes[i] & 0xFF);
if (hexString.length() == 1) {
hexString = '0' + hexString;
}
result += hexString.toUpperCase();
}
return result;
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(MainActivity.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
然后在mainActivity里面有一个handler来处理接收到的数据:
//处理线程返回信息
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE://1
Toast.makeText(MainActivity.this, "change", Toast.LENGTH_LONG);
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED:
progressDialog.dismiss();
Toast.makeText(getApplication(),"已成功连接设备"+ mConnectedDeviceName,Toast.LENGTH_SHORT).show();
break;
case BluetoothService.STATE_CONNECTING:
progressDialog.setMessage("设备连接中...");
progressDialog.setCancelable(true);
progressDialog.show();
break;
case BluetoothService.STATE_LISTEN:
case BluetoothService.STATE_NONE:
progressDialog.cancel();
Toast.makeText(getApplication(),"设备未连接",Toast.LENGTH_SHORT).show();
break;
}
break;
//发送了信息,显示在本地屏幕上(重写,显示结果即可)
case MESSAGE_WRITE://2
//Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG);
break;
//收到了信息,显示在本地屏幕上(重写,加入到通信录中)
case MESSAGE_READ://3读取数据
//reciever = new TransactionReciever();
//byte[] readBuffer = new byte[11];
Bundle data = msg.getData();
// readBuffer = data.getByteArray("ByteData");
recieveStr = data.getString("BTdata");
Log.e("recieveString == ",recieveStr);
break;
case MESSAGE_DEVICE_NAME://4
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
break;
case MESSAGE_TOAST://5
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
然后问题就来了,这样做程序就变得有些卡顿了,并且接收到的数据我打印出来永远的都一样的,比如:,无论我怎么晃动我的加速度器,都是永远一个值(串口显示器上数据是一直变化的在晃动的时候).
前辈们能指出一下我那儿做错了么,怎么处理卡顿呢?新手求指导。