2017-08-28 115 views
-1

我已經顯示通過藍牙從微處理器收到的數據。它每秒向我發送一個具有實際電壓和溫度狀態的8位幀。動態顯示數據TextView

問題是TextView不顯示實際數據。當應用程序加載時,數據會顯示並保持不變。一種刷新數據的方法是再次加載應用程序。

我得到了處理程序和ConnectedThread我附加的代碼。

一切都來自波蘭。

bluetoothIn = new Handler() { 
     public void handleMessage(android.os.Message msg) { 
      if (msg.what == handlerState) { 
       String readMessage = (String) msg.obj; 
       sb.append(readMessage); 
       int endOfLineIndex = sb.indexOf(";\r\n"); 

       if (endOfLineIndex > 0) { 
        String dataInPrint = sb.substring(0, endOfLineIndex); 
        strDlugosc.setText(dataInPrint); 
        int dataLenght = dataInPrint.length(); 
        strLenght.setText("ilość otrzymanych znakow =" + String.valueOf(dataLenght)); 

        if (sb.charAt(0) == '9') { 

         String statusb = sb.substring(8,9); 
         String temperatura = sb.substring(21, 27); 
         String napiecie = sb.substring(12, 18); 

         status.setText("Status :" + statusb); 
         temp_1.setText("TEMPERATURA = " + temperatura + " *C"); 
         nap_1.setText("NAPIECIE = " + napiecie + " V"); 


        } 
       } 
      } 


    private class ConnectedThread extends Thread { 
    private final InputStream mmInStream; 


    //creation of the connect thread 
    public ConnectedThread(BluetoothSocket socket) { 
     InputStream tmpIn = null; 

     try { 
      //Create I/O streams for connection 
      tmpIn = socket.getInputStream(); 
     } catch (IOException e) { } 

     mmInStream = tmpIn; 
    } 


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

     // Keep looping to listen for received messages 
     while (!ConnectedThread.interrupted()) { 
      try { 
       bytes = mmInStream.read(buffer);   //read bytes from input buffer 
       String readMessage = new String(buffer, 0, bytes); 
       // Send the obtained bytes to the UI Activity via handler 
       bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget(); 

      } catch (IOException e) { 

       break; 
      } 
     } 
    } 

回答

0

我以前做過。我用定時器從藍牙插座每1秒鐘獲取數據

Timer t = new Timer(); 
t.scheduleAtFixedRate(new TimerTask() { 

    @Override 
    public void run() { 
    // get data from socket and set to text view here 
    } 

}, 0, 10000 /** milliseconds **/);