2015-12-21 36 views
0
void beginListenForData() { 
      //final Handler handler = new Handler(); 
      final Handler handler = new Handler(Looper.getMainLooper()); 
      final byte delimiter = 10; //This is the ASCII code for a newline character 

      stopWorker = false; 
      readBufferPosition = 0; 
      readBuffer = new byte[2048]; 
      workerThread = new Thread(new Runnable() { 
       public void run() { 
        while (!Thread.currentThread().isInterrupted() && !stopWorker) { 
         try { 
          int bytesAvailable = mmInputStream.available(); 
          if (bytesAvailable > 0) { 
           byte[] packetBytes = new byte[bytesAvailable]; 
           mmInputStream.read(packetBytes); 
           for (int i = 0; i < bytesAvailable; i++) { 
            byte b = packetBytes[i]; 
            if (b == delimiter) { 
             byte[] encodedBytes = new byte[readBufferPosition]; 
             System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); 
             final String data = new String(encodedBytes, "US-ASCII"); 
             readBufferPosition = 0; 


                       handler.post(new Runnable() { 

              public void run() { 

               //myLabel.setText(data); 
               dataArray = new String []{data}; 
               //Log.d("dataArray", data); 
              } 
             }); 

            } else { 
             readBuffer[readBufferPosition++] = b; 
            } 
           } 
          } 
         } catch (IOException ex) { 
          stopWorker = true; 
         } 
        } 
       } 
      }); 


public class Bluetooth_dataDisplay extends AppCompatActivity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout); 

     String MAC = getIntent().getStringExtra("MAC"); 
     mAdapter = BluetoothAdapter.getDefaultAdapter(); 
     BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC); 
     // Initiate a connection request in a separate thread 
     ConnectingThread t = new ConnectingThread(bluetoothDevice); 
     t.start(); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     recyclerView.setHasFixedSize(true); 
     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 
     adapter = new RecyclerAdapter(dataArray); 
     recyclerView.setAdapter(adapter); 

    } 

Begindata()在連接的線程中調用。我試圖讓數據返回到Bluetooth_dataDisplay,擴展AppCompatActivity並在oncreate中調用它。我如何將數據發送回主要活動?請幫助任何專家。我看過帖子中提到線程數據發送回主界面。但我對它很新,所以很混亂。下runOnUiThread()在UI /主線程運行在你的beginListenForData()功能如何從線程發回數據回主界面?

handler.post(new Runnable() { 
     public void run() { 
     //myLabel.setText(data); 
     dataArray = new String[]{data}; 
     //Log.d("dataArray", data); 
     } 
    }); 

:(請幫助。

+0

你爲什麼不使用'AsyncTask'代替它很容易維護,當線程啓動時,它會給出一些方法回調,當結束時,在中間它也可以給出re sponse。 –

+0

1)通過重寫'handleMessage'方法或將'Handler.Callback'傳遞給你的'Handler'構造函數來創建一個自定義的'Handler' 2)使用:'msg = handler.obtainMessage(int what,Object obj)'obj '是你的數據3)調用'msg.sendToTarget()'或只是'handler.obtainMessage(...)。sendToTarget()' – pskink

+0

@NigamPatro我不明白異步任務。 – Spotty

回答

0

更換處理程序調用與runOnUiThread()

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
      //myLabel.setText(data); 
      dataArray = new String[]{data}; 
      //Log.d("dataArray", data); 
     } 
    }); 

一切。