- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个连接到Arduino Uno和HC-06蓝牙模块的ECG模块,该模块以100 Hz的频率测量ECG信号,并每10毫秒将6字节数据发送到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);
}
}
}
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) {
}
}
}
}
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
最佳答案
您的蓝牙模块可能没问题。您看到的性能问题很可能是协议实现方面的问题:即,s
字符不在您期望的位置。造成这种情况的原因有很多,所以让我们一一讲解它们:
心电图的价值
请注意,analogRead()
的返回值的范围是0到1023(docs),对应于0V到Vref
的范围。假设您的Vref
为5V,则ECG的3.3V输出将导致analogRead()
返回675
。因此,实际上,ECG
的范围可能从0
到675
。这就是为什么您拥有以下缩放逻辑的原因:
float Volt = (float)ECG*5.0/1023.0;
ECG
是
5
,则
Volt
将是
0.024437929
。这违反了您的假设,即
Volt
始终是四个字符的浮点数,并且导致您的
s
字符未对齐。
mmInStream.read(buffer)
将尝试读取多达1024个字节;这意味着它可能会抓取多条消息。但是,如果您在缓冲区中收到多个消息,那么您将只处理第一个消息。
s0.����
。这是因为
mmInStream.read(buffer)
仅读取3个字节,但是您假定它读取为5。您会发现,
read()
在至少有1个新字节可用时立即返回一个值。这并不意味着它已读取您想要的所有内容。在这种情况下,最后2个字节已进行单位化(0),但无论如何您都尝试将其转换为
String
。
15s0.
。这是因为第15条消息(
s0.15
)在中间被拆分。然后,第16条消息(
s0.
)的一部分将附加到末尾,因为现在您的对齐方式已关闭。由于套接字时序的怪异,仅在下一行重新获得了重新对齐。
read()
块,直到它精确读取5个字节为止(请参见“修复#2”)。
myBT.print("s"+String(Volt,2)); //2 decimal places works out to 4 characters total, for your input range.
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;
}
// Keep listening to the InputStream until an exception occurs
byte[] buffer;
int bytes = 0;
try {
while(mmInStream.read() != (byte) 's') //TODO: Handle "-1" case
{}
//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) {
(...)
sleep()
,因为
read()
逻辑会阻塞,直到接收到必要的数据为止。另外,除了不满足仅在第一个字节为
s
的情况下才处理消息的情况,如果不满足该条件,则应关闭连接(因为这表明您的对等方有故障)。
if(strlen(String(Volt))!=4)
。如果
s
不在您期望的位置,请不要忽略该问题;找出原因。
关于java - Android蓝牙实时数据流慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59807939/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!