Textview values does not update when data received from Arduino

别来无恙 提交于 2020-01-04 05:31:11

问题


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:


回答1:


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

  }

}



回答2:


Before recDataString.append(readMessage); try to reset the recDataString first, like this:

recDataString.setLength(0);

Because if it does not reset, it will continue append new strings:

#689928.0
#689928.06899719928.11 ...


来源:https://stackoverflow.com/questions/34490859/textview-values-does-not-update-when-data-received-from-arduino

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!