如何计算在另一个线程中接收到的已接收消息的数量

时间:2019-04-06 19:48:05

标签: java android multithreading handler

我不断在另一个线程中的蓝牙上接收消息,我需要将此数据放入arrayList,然后在主线程中使用此数组

在此代码中,我从蓝牙接收消息并将其粘贴到EditText中,但是我不知道如何将其粘贴到ArrayList中

     public ConnectedThread(BluetoothSocket bluetoothSocket) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = bluetoothSocket.getInputStream();
            outputStream = bluetoothSocket.getOutputStream();
        }catch (IOException e) {
            e.printStackTrace();
        }
        this.inputStream = inputStream;
        this.outputStream = outputStream;
        isConnected = true;
    }

    @Override
    public void run() {
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        final StringBuffer buffer = new StringBuffer();//Формирует из пришедшего симаола строку
        final StringBuffer sbConsole = new StringBuffer();
        final ScrollingMovementMethod movementMethod = new ScrollingMovementMethod();
        while (isConnected) {
            try {
                final int bytes = bis.read();
                buffer.append((char)bytes);
                int eof = buffer.indexOf("\r\n");
                if (eof > 0) {
                    turn =buffer.toString();
                    sbConsole.append(buffer.toString());
                    buffer.delete(0, buffer.length());
                    handler.sendEmptyMessage(bytes);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            etConsole.setText(sbConsole);
                            etConsole.setMovementMethod(movementMethod);
                        }
                    });
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }

0 个答案:

没有答案