【发布时间】:2020-01-19 07:11:07
【问题描述】:
我有一个连接到 Arduino Uno 和 HC-06 蓝牙模块的心电图模块,以 100 Hz 的频率测量心电图信号,并以 6 字节的数据每 10 毫秒向 Android 应用程序发送数据。但是,我无法实现每秒接收 100 个样本。
这是我的 Arduino 代码:
#include <SoftwareSerial.h>
SoftwareSerial myBT(2, 3);
void setup() {
myBT.begin(115200);
Serial.begin(115200);
pinMode(A0, OUTPUT);
}
char cmd;
int ECG;
void loop()
{
if(myBT.available()>0)
{
cmd = myBT.read();
while(cmd = 'r')
{
ECG = ((ECG+1)%1023);
float Volt = (float)ECG*5.0/1023.0;
myBT.print("s"+String(Volt,2));
delay(10);
}
}
}
这是我的 Android Java 代码:
package com.example.mscale;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.icu.text.UnicodeSetSpanner;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.Series;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
public class MonitorActivity extends AppCompatActivity {
private final String TAG = "MonitorActivity";
//Stuffs
GraphView graphx;
Button recordButton;
TextView statusBtTxt, KilogramTxt;
//Bluetooth Stuffs
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter bluetoothAdapter;
private ArrayList<String> mDeviceList;
private String pairedAddres;
ArrayAdapter<String> adapter;
private BluetoothSocket mBluetoothSocket;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
protected static final int MESSAGE_WRITE = 2;
private boolean recording = false;
/* @Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (Bluetooth.connectedThread != null) {
Bluetooth.connectedThread.write("Q");}//Stop streaming
super.onBackPressed();
}*/
@SuppressLint("HandlerLeak")
public Handler mHandler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCESS_CONNECT:
statusBtTxt.setText("Connected");
recording = true;
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, 5); // create string from bytes array
statusBtTxt.setText(strIncom);
Log.d(TAG, strIncom);
/*if (strIncom.indexOf("s") == 3) {
Log.d(TAG, strIncom);
strIncom = strIncom.replace("s", "");
if(isFloatNumber(strIncom)){
}
}else Log.d(TAG, "The s is not where it should be!");*/
break;
case MESSAGE_WRITE:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
connectedThread.write("r");
break;
}
}
};
public boolean isFloatNumber(String num) {
//Log.d("checkfloatNum", num);
try {
Double.parseDouble(num);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideStatusbar();
setContentView(R.layout.activity_monitor);
findIDStuff();
plotting();
System.gc();
recordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recording == false) {
bluetoothManagement();
} else {
ConnectedThread connectedThread = new ConnectedThread(mBluetoothSocket);
connectedThread.write("r");
connectedThread.start();
}
}
});
}
//Find ID of Stuffs
private void findIDStuff() {
recordButton = findViewById(R.id.recordBtn);
//graphx = (GraphView) findViewById(R.id.graph);
statusBtTxt = findViewById(R.id.btStatusTxt);
KilogramTxt = findViewById(R.id.kiloTxt);
}
//Bluetooth management
private void bluetoothManagement() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
//Do sth
pairedBtDevices();
}
}
//Graph plotting
private void plotting() {
}
//Hide status bar
private void hideStatusbar() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
//Paired Bluetooth device
private void pairedBtDevices() {
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String module = "ECG_Module";
if (device.getName().equals(module)) {
pairedAddres = device.getAddress();
}
}
bluetoothAdapter.startDiscovery();
//Log.d(TAG, String.valueOf(mPairedDeviceList));
} else {
statusBtTxt.setText("No paired");
}
}
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
mDeviceList = new ArrayList<String>();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
String module = "ECG_Module";
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getName() != null && device.getName().equalsIgnoreCase(module) && device.getAddress() != null && device.getAddress().equalsIgnoreCase(pairedAddres)) {
mDeviceList.add(device.getName() + "\n" + device.getAddress());
bluetoothAdapter.cancelDiscovery();
open_dialog();
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
statusBtTxt.setText("Start Discovery...");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
statusBtTxt.setText("End Discovery....");
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
//Open dialog
public void open_dialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View row = getLayoutInflater().inflate(R.layout.rowitem, null);
ListView mlistView = (ListView) row.findViewById(R.id.listviewBt);
final Button mBtnConnect = (Button) row.findViewById(R.id.conBtn);
Button mBtnCancel = (Button) row.findViewById(R.id.canBtn);
mlistView.setClickable(true);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mDeviceList);
mlistView.setAdapter(adapter);
alertDialog.setView(row);
final AlertDialog dialog = alertDialog.create();
dialog.show();
mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String mNames = (String) parent.getItemAtPosition(position);
final String[] mMacAddress = mNames.split("\\r?\\n");
mBtnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(mMacAddress[1]);
ConnectThread connectThread = new ConnectThread(bluetoothDevice);
connectThread.start();
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Connnecting to " + mMacAddress[1], Toast.LENGTH_SHORT);
toast.show();
dialog.hide();
} catch (Exception e) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT);
toast.show();
return;
}
}
});
}
});
mBtnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.hide();
}
});
}
//Connection Bluetooth
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket
// because mmSocket is final.
BluetoothSocket tmp = null;
mmDevice = device;
try {
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "Socket's create() method failed", e);
}
mBluetoothSocket = tmp;
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it otherwise slows down the connection.
bluetoothAdapter.cancelDiscovery();
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
mmSocket.close();
Log.d(TAG, "Failed connection");
} catch (IOException closeException) {
Log.e(TAG, "Could not close the client socket", closeException);
}
return;
}
// The connection attempt succeeded. Perform work associated with
// the connection in a separate thread.
mHandler.obtainMessage(SUCESS_CONNECT, mmSocket).sendToTarget();
}
// Closes the client socket and causes the thread to finish.
public void cancel() {
try {
statusBtTxt.setText("Failed Connection");
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the client socket", e);
}
}
}
class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
StringBuffer sbb = new StringBuffer();
public void run() {
// Keep listening to the InputStream until an exception occurs
byte[] buffer;
int bytes = 0;
try {
while(mmInStream.read() != (byte) 's')
{}
//Since we already read out the "s", we need to read 4 more bytes, to regain alignment.
mmInStream.read();
mmInStream.read();
mmInStream.read();
mmInStream.read();
//Now, the next byte read should be an "s".
} catch (IOException e) {
return;
}
while (true) {
try {
buffer = new byte[1024];
// Read from the InputStream
int totalRead = 0;
while(totalRead < 5)
{
bytes = mmInStream.read(buffer,totalRead,5-totalRead);
if(bytes==-1)
throw new IOException("EOS reached");
totalRead += bytes;
}
//Log.d("Obtain: ", String.valueOf(totalRead));
mHandler.obtainMessage(MESSAGE_READ, totalRead, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write (String income){
try {
mmOutStream.write(income.getBytes());
} catch (IOException e) {
}
}
/* Call this from the main activity to shutdown the connection */
public void cancel () {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
}
Transmitted changeable value and Logcat in Android接收数据(没有myBT.print("\n")和Sensor输入):
2020-01-22 13:12:22.369 28250-28250/com.example.mscale D/MonitorActivity: s0.01
2020-01-22 13:12:22.401 28250-28250/com.example.mscale D/MonitorActivity: s0.01
2020-01-22 13:12:22.421 28250-28250/com.example.mscale D/MonitorActivity: s0.02
2020-01-22 13:12:22.421 28250-28250/com.example.mscale D/MonitorActivity: s0.02
2020-01-22 13:12:22.422 28250-28250/com.example.mscale D/MonitorActivity: s0.03
2020-01-22 13:12:22.444 28250-28250/com.example.mscale D/MonitorActivity: s0.03
2020-01-22 13:12:22.454 28250-28250/com.example.mscale D/MonitorActivity: s0.04
2020-01-22 13:12:22.454 28250-28250/com.example.mscale D/MonitorActivity: s0.04
2020-01-22 13:12:22.454 28250-28250/com.example.mscale D/MonitorActivity: s0.05
2020-01-22 13:12:22.478 28250-28250/com.example.mscale D/MonitorActivity: s0.05
2020-01-22 13:12:22.488 28250-28250/com.example.mscale D/MonitorActivity: s0.06
2020-01-22 13:12:22.488 28250-28250/com.example.mscale D/MonitorActivity: s0.06
2020-01-22 13:12:22.514 28250-28250/com.example.mscale D/MonitorActivity: s0.07
2020-01-22 13:12:22.522 28250-28250/com.example.mscale D/MonitorActivity: s0.07
2020-01-22 13:12:22.522 28250-28250/com.example.mscale D/MonitorActivity: s0.08
2020-01-22 13:12:22.548 28250-28250/com.example.mscale D/MonitorActivity: s0.08
2020-01-22 13:12:22.555 28250-28250/com.example.mscale D/MonitorActivity: s0.09
2020-01-22 13:12:22.555 28250-28250/com.example.mscale D/MonitorActivity: s0.09
2020-01-22 13:12:22.572 28250-28250/com.example.mscale D/MonitorActivity: s0.10
2020-01-22 13:12:22.589 28250-28250/com.example.mscale D/MonitorActivity: s0.10
2020-01-22 13:12:22.589 28250-28250/com.example.mscale D/MonitorActivity: s0.11
2020-01-22 13:12:22.606 28250-28250/com.example.mscale D/MonitorActivity: s0.11
2020-01-22 13:12:22.623 28250-28250/com.example.mscale D/MonitorActivity: s0.12
2020-01-22 13:12:22.640 28250-28250/com.example.mscale D/MonitorActivity: s0.12
2020-01-22 13:12:22.641 28250-28250/com.example.mscale D/MonitorActivity: s0.13
2020-01-22 13:12:22.659 28250-28250/com.example.mscale D/MonitorActivity: s0.13
2020-01-22 13:12:22.673 28250-28250/com.example.mscale D/MonitorActivity: s0.14
2020-01-22 13:12:22.673 28250-28250/com.example.mscale D/MonitorActivity: s0.14
2020-01-22 13:12:22.674 28250-28250/com.example.mscale D/MonitorActivity: s0.15
2020-01-22 13:12:22.700 28250-28250/com.example.mscale D/MonitorActivity: s0.15
2020-01-22 13:12:22.707 28250-28250/com.example.mscale D/MonitorActivity: s0.16
2020-01-22 13:12:22.707 28250-28250/com.example.mscale D/MonitorActivity: s0.16
2020-01-22 13:12:22.724 28250-28250/com.example.mscale D/MonitorActivity: s0.17
2020-01-22 13:12:22.724 28250-28250/com.example.mscale D/MonitorActivity: s0.17
2020-01-22 13:12:22.743 28250-28250/com.example.mscale D/MonitorActivity: s0.18
2020-01-22 13:12:22.758 28250-28250/com.example.mscale D/MonitorActivity: s0.18
2020-01-22 13:12:22.758 28250-28250/com.example.mscale D/MonitorActivity: s0.19
2020-01-22 13:12:22.778 28250-28250/com.example.mscale D/MonitorActivity: s0.19
2020-01-22 13:12:22.793 28250-28250/com.example.mscale D/MonitorActivity: s0.20
2020-01-22 13:12:22.794 28250-28250/com.example.mscale D/MonitorActivity: s0.20
2020-01-22 13:12:22.815 28250-28250/com.example.mscale D/MonitorActivity: s0.21
2020-01-22 13:12:22.828 28250-28250/com.example.mscale D/MonitorActivity: s0.21
2020-01-22 13:12:22.828 28250-28250/com.example.mscale D/MonitorActivity: s0.22
2020-01-22 13:12:22.829 28250-28250/com.example.mscale D/MonitorActivity: s0.22
2020-01-22 13:12:22.858 28250-28250/com.example.mscale D/MonitorActivity: s0.22
2020-01-22 13:12:22.877 28250-28250/com.example.mscale D/MonitorActivity: s0.23
2020-01-22 13:12:22.878 28250-28250/com.example.mscale D/MonitorActivity: s0.23
2020-01-22 13:12:22.878 28250-28250/com.example.mscale D/MonitorActivity: s0.24
2020-01-22 13:12:22.897 28250-28250/com.example.mscale D/MonitorActivity: s0.24
2020-01-22 13:12:22.911 28250-28250/com.example.mscale D/MonitorActivity: s0.25
2020-01-22 13:12:22.912 28250-28250/com.example.mscale D/MonitorActivity: s0.25
2020-01-22 13:12:22.929 28250-28250/com.example.mscale D/MonitorActivity: s0.26
2020-01-22 13:12:22.929 28250-28250/com.example.mscale D/MonitorActivity: s0.26
2020-01-22 13:12:22.945 28250-28250/com.example.mscale D/MonitorActivity: s0.27
2020-01-22 13:12:22.964 28250-28250/com.example.mscale D/MonitorActivity: s0.27
2020-01-22 13:12:22.965 28250-28250/com.example.mscale D/MonitorActivity: s0.28
2020-01-22 13:12:22.980 28250-28250/com.example.mscale D/MonitorActivity: s0.28
2020-01-22 13:12:22.998 28250-28250/com.example.mscale D/MonitorActivity: s0.29
2020-01-22 13:12:23.013 28250-28250/com.example.mscale D/MonitorActivity: s0.29
2020-01-22 13:12:23.013 28250-28250/com.example.mscale D/MonitorActivity: s0.30
2020-01-22 13:12:23.033 28250-28250/com.example.mscale D/MonitorActivity: s0.30
2020-01-22 13:12:23.046 28250-28250/com.example.mscale D/MonitorActivity: s0.31
2020-01-22 13:12:23.046 28250-28250/com.example.mscale D/MonitorActivity: s0.31
2020-01-22 13:12:23.047 28250-28250/com.example.mscale D/MonitorActivity: s0.32
2020-01-22 13:12:23.069 28250-28250/com.example.mscale D/MonitorActivity: s0.32
2020-01-22 13:12:23.079 28250-28250/com.example.mscale D/MonitorActivity: s0.33
2020-01-22 13:12:23.080 28250-28250/com.example.mscale D/MonitorActivity: s0.33
2020-01-22 13:12:23.104 28250-28250/com.example.mscale D/MonitorActivity: s0.34
2020-01-22 13:12:23.115 28250-28250/com.example.mscale D/MonitorActivity: s0.34
2020-01-22 13:12:23.115 28250-28250/com.example.mscale D/MonitorActivity: s0.35
2020-01-22 13:12:23.116 28250-28250/com.example.mscale D/MonitorActivity: s0.35
2020-01-22 13:12:23.139 28250-28250/com.example.mscale D/MonitorActivity: s0.36
2020-01-22 13:12:23.149 28250-28250/com.example.mscale D/MonitorActivity: s0.36
2020-01-22 13:12:23.149 28250-28250/com.example.mscale D/MonitorActivity: s0.37
2020-01-22 13:12:23.178 28250-28250/com.example.mscale D/MonitorActivity: s0.37
2020-01-22 13:12:23.198 28250-28250/com.example.mscale D/MonitorActivity: s0.38
2020-01-22 13:12:23.198 28250-28250/com.example.mscale D/MonitorActivity: s0.38
2020-01-22 13:12:23.215 28250-28250/com.example.mscale D/MonitorActivity: s0.39
2020-01-22 13:12:23.216 28250-28250/com.example.mscale D/MonitorActivity: s0.39
2020-01-22 13:12:23.216 28250-28250/com.example.mscale D/MonitorActivity: s0.40
2020-01-22 13:12:23.235 28250-28250/com.example.mscale D/MonitorActivity: s0.40
2020-01-22 13:12:23.248 28250-28250/com.example.mscale D/MonitorActivity: s0.41
2020-01-22 13:12:23.249 28250-28250/com.example.mscale D/MonitorActivity: s0.41
2020-01-22 13:12:23.266 28250-28250/com.example.mscale D/MonitorActivity: s0.42
2020-01-22 13:12:23.284 28250-28250/com.example.mscale D/MonitorActivity: s0.42
2020-01-22 13:12:23.299 28250-28250/com.example.mscale D/MonitorActivity: s0.43
2020-01-22 13:12:23.299 28250-28250/com.example.mscale D/MonitorActivity: s0.43
2020-01-22 13:12:23.319 28250-28250/com.example.mscale D/MonitorActivity: s0.43
2020-01-22 13:12:23.336 28250-28250/com.example.mscale D/MonitorActivity: s0.44
2020-01-22 13:12:23.336 28250-28250/com.example.mscale D/MonitorActivity: s0.44
2020-01-22 13:12:23.355 28250-28250/com.example.mscale D/MonitorActivity: s0.45
2020-01-22 13:12:23.367 28250-28250/com.example.mscale D/MonitorActivity: s0.45
2020-01-22 13:12:23.386 28250-28250/com.example.mscale D/MonitorActivity: s0.46
2020-01-22 13:12:23.386 28250-28250/com.example.mscale D/MonitorActivity: s0.46
2020-01-22 13:12:23.387 28250-28250/com.example.mscale D/MonitorActivity: s0.47
2020-01-22 13:12:23.387 28250-28250/com.example.mscale D/MonitorActivity: s0.47
2020-01-22 13:12:23.417 28250-28250/com.example.mscale D/MonitorActivity: s0.48
2020-01-22 13:12:23.435 28250-28250/com.example.mscale D/MonitorActivity: s0.48
2020-01-22 13:12:23.436 28250-28250/com.example.mscale D/MonitorActivity: s0.49
2020-01-22 13:12:23.436 28250-28250/com.example.mscale D/MonitorActivity: s0.49
2020-01-22 13:12:23.457 28250-28250/com.example.mscale D/MonitorActivity: s0.50
2020-01-22 13:12:23.474 28250-28250/com.example.mscale D/MonitorActivity: s0.50
2020-01-22 13:12:23.474 28250-28250/com.example.mscale D/MonitorActivity: s0.51
2020-01-22 13:12:23.474 28250-28250/com.example.mscale D/MonitorActivity: s0.51
2020-01-22 13:12:23.501 28250-28250/com.example.mscale D/MonitorActivity: s0.52
2020-01-22 13:12:23.501 28250-28250/com.example.mscale D/MonitorActivity: s0.52
2020-01-22 13:12:23.519 28250-28250/com.example.mscale D/MonitorActivity: s0.53
2020-01-22 13:12:23.540 28250-28250/com.example.mscale D/MonitorActivity: s0.53
2020-01-22 13:12:23.541 28250-28250/com.example.mscale D/MonitorActivity: s0.54
2020-01-22 13:12:23.541 28250-28250/com.example.mscale D/MonitorActivity: s0.54
2020-01-22 13:12:23.553 28250-28250/com.example.mscale D/MonitorActivity: s0.55
2020-01-22 13:12:23.573 28250-28250/com.example.mscale D/MonitorActivity: s0.55
2020-01-22 13:12:23.587 28250-28250/com.example.mscale D/MonitorActivity: s0.56
2020-01-22 13:12:23.587 28250-28250/com.example.mscale D/MonitorActivity: s0.56
【问题讨论】:
-
对
sleep(5)的调用非常可疑。除此之外,我建议您记录对mmInStream.read()和case MESSAGE_READ代码的调用,并确保后者没有“落后”,这表明存在某种瓶颈。此外,我每次都会记录strIncom,b/c 我无法从粗略的检查中看到,你如何确保String(Volt)正好是 4 个字符长,我认为你的s字符可能会不正常. -
@greeble31 感谢您的回复!请检查我的编辑。我尝试以 10 毫秒的延迟打印一些从 0.01 到 5.00 的浮点值。我打印出来的Logcat太长了(Last value是“s4.87”)所以我只取了Logcat的一部分。这是我从蓝牙模块获得的数据。另一个 Arduino 代码是我知道我的数据长 4 个字符的方式。
-
好的,很好的测试方法,但是你错过了一些可能很重要的东西。您正在以 0.01 的增量进行测试,这会产生良好的、行为良好的 4 字符浮点数。 但是当你的缩放逻辑留在里面时,情况不会发生。相反,您应该在进行测试时模拟
analogRead()的返回值,并保留其余代码。提示:考虑ECG为 5 时会发生什么。Volt会是什么? -
稍后我们将讨论您的 logcat 中的有趣字符。显然,该 logcat 输出并非全部正确,但我还不能 100% 确定它的来源。
-
@greeble31 其实心电模块最大输出值为3.3伏。