gpt4 book ai didi

android - 当从 Arduino 收到数据时,Textview 值不会更新

转载 作者:行者123 更新时间:2023-11-29 01:25:11 25 4
gpt4 key购买 nike

我从 Arduino 接收到 3 个数据,并且数据不断传入。我的问题是数据没有更新为接收到的新值。

这是 logcat 的屏幕截图: Screenshot from Log cat所以D/BPM是textview中的值68D/SPo2 是 TextView 中的值 99d/temp是textview 28.0中的值

D/read:71 表示 BPM,99 表示 SPo2,28.11 表示温度。但是 textview 值只是停留在 68 99 和 28.0。 :(

这是我的 Android 部分代码:

 public class Bluetooth_dataDisplay extends Activity {
//declaration
BluetoothAdapter mAdapter;
private ArrayAdapter adapter;
TextView myLabel;
TextView myLabel1;
TextView myLabel2;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;
private ListView listview;
private BluetoothSocket bluetoothSocket;
private ConnectedThread mConnectedThread;
final int handlerState = 0;
private StringBuilder recDataString = new StringBuilder();

private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
recDataString.append(readMessage);
Log.d("read",readMessage);
if (recDataString.charAt(0) == '#')
{
String BPM = recDataString.substring(1,3);
String SPO2= recDataString.substring(3,5);
String Temp= recDataString.substring(5,9);
Log.d("BPM", BPM);
Log.d("SPO2", SPO2);
Log.d("Temp", Temp);
myLabel.setText("BPM" + " " + BPM);
myLabel1.setText("SPO2" +" "+ SPO2);
myLabel2.setText("Temp"+" " + Temp);
}
//recDataString.delete(0, recDataString.length());


break;


}
}
};




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth_datadisplay);
myLabel = (TextView)findViewById(R.id.label);
myLabel1 = (TextView)findViewById(R.id.label1);
myLabel2= (TextView)findViewById(R.id.label2);






}//end oncreate

@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
//Get MAC address from BluetoothActivity using intent and Extra
String MAC = getIntent().getStringExtra("MAC");
//must declare every time on a new activity if not will result in null error
mAdapter = BluetoothAdapter.getDefaultAdapter();
//create device and set the MAC address
BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);
// Initiate a connection request in a separate thread
ConnectingThread t = new ConnectingThread(bluetoothDevice);
t.start();

}

@Override
public void onPause()
{
super.onPause();
try {
bluetoothSocket.close();
} catch (IOException e) {
Log.e("error", "terminate thread");
e.printStackTrace();
}
}

/**
* Start the ConnectedThread to begin managing a Bluetooth connection
*
* @param socket The BluetoothSocket on which the connection was made
* @param device The BluetoothDevice that has been connected
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {

// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();

}

private class ConnectingThread extends Thread {
private final BluetoothSocket bluetoothSocket;
private final BluetoothDevice bluetoothDevice;

public ConnectingThread(BluetoothDevice device) {

BluetoothSocket temp = null;
bluetoothDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = temp;
}

public void run() {
// Cancel any discovery as it will slow down the connection
mAdapter.cancelDiscovery();

try {
// This will block until it succeeds in connecting to the device
// through the bluetoothSocket or throws an exception
bluetoothSocket.connect();
} catch (IOException connectException) {
connectException.printStackTrace();
try {
bluetoothSocket.close();
} catch (IOException closeException) {
closeException.printStackTrace();
}
}

// Code to manage the connection in a separate thread
connected(bluetoothSocket, bluetoothDevice);
}

// Cancel an open connection and terminate the thread
public void cancel() {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket bluetoothSocket;
private final InputStream mmInputStream;
//private final OutputStream mmOutputStream;

public ConnectedThread(BluetoothSocket socket) {
bluetoothSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
//tmpOut = socket.getOutputStream();
} catch (IOException e) {
}

mmInputStream = tmpIn;
//mmOutputStream = tmpOut;
}//endofConnectedThread(BluetoothSocket socket)


public void run() {
byte[] buffer = new byte[1024];
int bytes;

// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInputStream.read(buffer);

// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}//end of void run

}//end of connectecthread

}

因为 Arduino 将发送数据,例如#719928.11,所以索引将按此顺序 (0123456789)

这就是为什么我这样做是为了提取数据:

 if (recDataString.charAt(0) == '#')
{
String BPM = recDataString.substring(1,3);
String SPO2= recDataString.substring(3,5);
String Temp= recDataString.substring(5,9);
Log.d("BPM", BPM);
Log.d("SPO2", SPO2);
Log.d("Temp", Temp);
myLabel.setText("BPM" + " " + BPM);
myLabel1.setText("SPO2" +" "+ SPO2);
myLabel2.setText("Temp"+" " + Temp);
}

只是添加有关数据的更多详细信息:BPM 和 SPO2 为整数(脉搏血氧计)温度为浮点格式所以如果我的脉搏血氧仪没有打开。我的数据类似于 #0028.28,其中第一个 0 是 BPM,第二个 0 是 SPO2。 28.28 是温度

因此,如果脉搏血氧仪开启。我的数据如下所示 #669928.28,其中 66 是 BPM,99 是 SPO2。 28.28 是温度。以下是 Arduino 串行监视器中显示的数据:

pulse oximeter off pulse oximeter on

logcat when oximeter is on logcat when oximter is off

最佳答案

试试这个

1 。 替换

 private StringBuilder recDataString = new StringBuilder();

用这个

private String recDataString = "";

2。替换

recDataString.append(readMessage);

用这个

recDataString = readMessage ;

different-2 情况下的数据解析:

if(recDataString.lenth > 0){
//If Oxiometer is OFF
if(recDataString.startsWith(#00) && recDataString.length == 8){
//Do Parsing here #0028.14
String BPM = "0";
String SPO2= "0";
String temp = recDataString.split("00")[1];

myLabel.setText("BPM" + " " + BPM);
myLabel1.setText("SPO2" +" "+ SPO2);
myLabel2.setText("Temp"+" " + Temp);
}

//If Oxiometer is on
if(recDataString.startsWith(#) && recDataString.length() == 10){
//Do Parsing here #779928.08
String BPM = recDataString.substring(1,3);
String SPO2= recDataString.substring(3,5);
String temp = recDataString.substring(5,recDataString.lenth());

myLabel.setText("BPM" + " " + BPM);
myLabel1.setText("SPO2" +" "+ SPO2);
myLabel2.setText("Temp"+" " + Temp);
}

//If String contains # only .
if(recDataString.startsWith(#) && recDataString.length == 1){
//Do nothing or you can decide

}

}

关于android - 当从 Arduino 收到数据时,Textview 值不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34490859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com