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

时间:2015-12-28 09:27:45

标签: android bluetooth arduino-uno

我从Arduino收到了3个数据,而且数据一直在进来。我的问题是数据没有更新到新接收的值。

以下是logcat的截图: Screenshot from Log cat 所以D / BPM是textview 68中的值 D / SPo2是textview 99中的值 D / temp是textview 28.0中的值

D /读: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在int(脉搏血氧仪) Temp是浮动格式 所以,如果我的脉搏血氧仪没有打开。我的数据看起来像这样#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

2 个答案:

答案 0 :(得分:1)

试试这个

1。  替换

 private StringBuilder recDataString = new StringBuilder();

这一个

private String recDataString = "";

2。替换

recDataString.append(readMessage);

这一个

recDataString = readMessage ;

针对不同情况的数据解析:

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

  }

}

答案 1 :(得分:0)

recDataString.append(readMessage);尝试首先重置recDataString之前,请执行以下操作:

recDataString.setLength(0);

因为如果它没有重置,它将继续附加新字符串:

#689928.0
#689928.06899719928.11 ...