如何将数据从线程发送回主UI?

时间:2015-12-21 08:26:28

标签: android multithreading bluetooth

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()在连接的线程中调用。我试图将dataarray返回到Bluetooth_dataDisplay扩展AppCompatActivity并在oncreate中调用它。如何将dataarray发送回主要活动?请帮助任何专家。我看过帖子,谈到线程数据发送回主UI。但我很新,所以很混乱。 :(请帮忙。

0 个答案:

没有答案