当从Arduino的接收数据的TextView值不更新 [英] Textview values does not update when data received from Arduino
问题描述
我接到Arduino的3个数据,数据刚在未来。我的问题是数据不更新到新接收的值。
下面是从logcat的截图:
所以D / BPM是在TextView的68值
D /血氧饱和度在99 TextView的价值
D /温度是28.0 TextView的价值
D /阅读:71是BPM 99是SpO2和28.11是温度。
但是TextView的价值观只是停留在68 99和28.0。 :(
下面是我的code为Android的部分:
公共类Bluetooth_dataDisplay延伸活动{
//宣言
BluetoothAdapter mAdapter;
私人ArrayAdapter适配器;
TextView的myLabel;
TextView的myLabel1;
TextView的myLabel2;
螺纹的WorkerThread;
字节[] readBuffer;
INT readBufferPosition;
挥发性布尔stopWorker;
私人的ListView列表视图;
私有的BluetoothSocket的BluetoothSocket;
私人ConnectedThread mConnectedThread;
最终诠释handlerState = 0;
私人StringBuilder的recDataString =新的StringBuilder(); 私人最终处理程序mHandler =新的处理程序(){
@覆盖
公共无效的handleMessage(消息MSG){
开关(msg.what){
案例Constants.MESSAGE_READ:
字节[] =的ReadBuf(字节[])msg.obj;
//从缓冲区中的有效字节构造字符串
字符串readMessage =新的String(的ReadBuf,0,msg.arg1);
recDataString.append(readMessage);
Log.d(读,readMessage);
如果(recDataString.charAt(0)=='#')
{
串的BPM = recDataString.substring(1,3);
串SPO2 = recDataString.substring(3,5);
字符串温度= recDataString.substring(5,9);
Log.d(BPM,BPM);
Log.d(血氧饱和度,血氧饱和度);
Log.d(温度,温度);
myLabel.setText(BPM++ BPM);
myLabel1.setText(血氧饱和度++血氧饱和度);
myLabel2.setText(TEMP++温度);
}
//recDataString.delete(0,recDataString.length());
打破;
}
}
};
@覆盖
保护无效的onCreate(捆绑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);
} //结束的OnCreate @覆盖
公共无效onResume(){
super.onResume(); //始终首先调用父类的方法
//使用意图和额外的BluetoothActivity获取MAC地址
字符串MAC = getIntent()getStringExtra(陆委会)。
//必须声明一个新的活动,每次如果不将导致空的错误
mAdapter = BluetoothAdapter.getDefaultAdapter();
//创建设备,并设置MAC地址
BluetoothDevice类BluetoothDevice类= mAdapter.getRemoteDevice(MAC);
//启动在单独的线程的连接请求
ConnectingThread T =新ConnectingThread(BluetoothDevice类);
t.start(); } @覆盖
公共无效的onPause()
{
super.onPause();
尝试{
bluetoothSocket.close();
}赶上(IOException异常五){
Log.e(错误,终止线程);
e.printStackTrace();
}
} / **
*启动ConnectedThread开始管理蓝牙连接
*
* @参数插座在其上进行连接的BluetoothSocket的
* @参数设备已连接该BluetoothDevice类
* /
公共同步无效连接(插座的BluetoothSocket,BluetoothDevice类设备){ //开始线程管理连接并执行传输
mConnectedThread =新ConnectedThread(插座);
mConnectedThread.start(); } 私有类ConnectingThread继承Thread {
私人最终的BluetoothSocket的BluetoothSocket;
私人最终BluetoothDevice类BluetoothDevice类; 公共ConnectingThread(BluetoothDevice类设备){ 温度的BluetoothSocket = NULL;
BluetoothDevice类=设备; //获取的BluetoothSocket与给定的BluetoothDevice类连接
尝试{
TEMP = bluetoothDevice.createRfcommSocketToServiceRecord(UUID);
}赶上(IOException异常五){
e.printStackTrace();
}
的BluetoothSocket =温度;
} 公共无效的run(){
//取消任何发现,因为它会连接放缓
mAdapter.cancelDiscovery(); 尝试{
//这将阻止,直到它连接到该设备成功
//通过的BluetoothSocket或抛出一个异常
bluetoothSocket.connect();
}赶上(IOException异常的ConnectException){
connectException.printStackTrace();
尝试{
bluetoothSocket.close();
}赶上(IOException异常closeException){
closeException.printStackTrace();
}
} // code来管理一个单独的线程的连接
连接(的BluetoothSocket,BluetoothDevice类);
} //取消一个开放的连接,并终止线程
公共无效取消(){
尝试{
bluetoothSocket.close();
}赶上(IOException异常五){
e.printStackTrace();
}
}
} / **
*此螺纹与远程设备的连接过程中运行。
*它可以处理所有传入和传出传输。
* /
私有类ConnectedThread继承Thread {
私人最终的BluetoothSocket的BluetoothSocket;
私人最终的InputStream mmInputStream;
//私人最终的OutputStream mmOutputStream; 公共ConnectedThread(插座的BluetoothSocket){
的BluetoothSocket =插座;
InputStream的tmpIn = NULL;
OutputStream的tmpOut = NULL; //获取的BluetoothSocket输入和输出流
尝试{
tmpIn = socket.getInputStream();
// tmpOut = socket.getOutputStream();
}赶上(IOException异常五){
} mmInputStream = tmpIn;
// mmOutputStream = tmpOut;
} // endofConnectedThread(的BluetoothSocket插座)
公共无效的run(){
字节[]缓冲区=新的字节[1024];
诠释字节; //继续听InputStream的同时连接
而(真){
尝试{
//从InputStream读
字节= mmInputStream.read(缓冲); //发送所获得的字节到UI活动
mHandler.obtainMessage(Constants.MESSAGE_READ,字节,-1,缓冲液)
.sendToTarget();
}赶上(IOException异常五){
打破;
}
}
空运行} //结束 } // connectecthread结束}
由于Arduino的将如#719928.11数据发送因此指数将在此顺序(0123456789)
这就是为什么我这种方式来提取数据做到了:
如果(recDataString.charAt(0)=='#')
{
串的BPM = recDataString.substring(1,3);
串SPO2 = recDataString.substring(3,5);
字符串温度= recDataString.substring(5,9);
Log.d(BPM,BPM);
Log.d(血氧饱和度,血氧饱和度);
Log.d(温度,温度);
myLabel.setText(BPM++ BPM);
myLabel1.setText(血氧饱和度++血氧饱和度);
myLabel2.setText(TEMP++温度);
}
我想补充有关数据的详细信息:
BPM和血氧饱和度是INT(脉搏血氧仪)
温度是浮点格式
所以,如果我的脉搏血氧仪不上。我的数据是这样的#0028.28其中第一个0是BPM和第二0血氧饱和度。 28.28是临时
因此,如果脉搏血氧仪上。我的数据是这样的#669928.28,其中66 BPM和99是血氧饱和度。 28.28是温度。下面是显示在Arduino的串口监视器数据:
试试这个
1。
替换
私人StringBuilder的recDataString =新的StringBuilder();
与此一
私人字符串recDataString =;
2。替换
recDataString.append(readMessage);
与此一
recDataString = readMessage;
数据解析为不同-2的情况:
如果(recDataString.lenth大于0){
//如果Oxiometer为OFF
如果(recDataString.startsWith(#00)及与放大器; recDataString.length == 8){
//不要在这里解析#0028.14
串的BPM =0;
串血氧=0;
串临时= recDataString.split(00)[1]; myLabel.setText(BPM++ BPM);
myLabel1.setText(血氧饱和度++血氧饱和度);
myLabel2.setText(TEMP++温度);
} //如果Oxiometer是
如果(recDataString.startsWith(#)及&放大器; recDataString.length()== 10){
//不要在这里解析#779928.08
串的BPM = recDataString.substring(1,3);
串SPO2 = recDataString.substring(3,5);
串临时= recDataString.substring(5,recDataString.lenth()); myLabel.setText(BPM++ BPM);
myLabel1.setText(血氧饱和度++血氧饱和度);
myLabel2.setText(TEMP++温度);
} //如果字符串只包含#。
如果(recDataString.startsWith(#)及&放大器; recDataString.length == 1){
//什么也不做,你可以决定 }}
I receiving 3 data from Arduino and the data just keep coming in. My problem is the data doesn't update to the new received values.
Here is the screenshot from the logcat: So D/BPM is the value in the textview 68 D/SPo2 is the value in the textview 99 D/temp is the value in textview 28.0
D/read: 71 is for BPM 99 is for SPo2 and 28.11 is for temp. But the textview values just stuck at 68 99 and 28.0. :(
Here are my code for the Android part:
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
}
Because Arduino will be sending in data eg #719928.11 so the index will be in this order (0123456789)
Which is why I did it this way to extract the data:
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);
}
Just to add more details regarding the data: BPM and SPO2 are in int (pulse oximeter) Temp is in float format So if my pulse oximeter is not on. My data look like this #0028.28 where the first 0 is BPM and the second 0 is SPO2. 28.28 is temp
So if the pulse oximeter is on. My data look like this #669928.28 where 66 is BPM and 99 is SPO2. 28.28 is temp. Below is data displayed in Arduino serial monitor:
Try this
1 . replace
private StringBuilder recDataString = new StringBuilder();
with this one
private String recDataString = "";
2 . replace
recDataString.append(readMessage);
with this one
recDataString = readMessage ;
Data Parsing for different-2 situations :
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
}
}
这篇关于当从Arduino的接收数据的TextView值不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!