@rmiron

Как передать inputStream с одного активити на другой?

Приветствую всех!
Перейду сразу к сути. Есть Android приложение использующее bluetooth соединение (соединение с arduino устройством). Мне нужно использовать уже имеющийся поток данных в другом активити.
исходник bluetooth соединения
public class ConnectedThread extends Thread {
        private  BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        public BluetoothSocket getSocket(){
            return mmSocket;
        }
        public void setSocket(BluetoothSocket socket){
            this.mmSocket = socket;
        }
        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }
получение данных
void beginListenForData() {
        final Handler handler = new Handler();
        final byte delimiter = 10; // This is the ASCII code for a newline
        // character
        stopThread = false;
        bufferPosition = 0;
        buffer = new byte[1024];
        Thread thread = new Thread(new Runnable() {
            public void run() {
                while (!Thread.currentThread().isInterrupted() && !stopThread) {
                    try {
                        int bytesAvailable = inputStream.available();
                        if (bytesAvailable > 0) {
                            byte[] packetBytes = new byte[bytesAvailable];
                            inputStream.read(packetBytes);
                            for (int i = 0; i < bytesAvailable; i++) {
                                byte b = packetBytes[i];
                                if (b == delimiter) {
                                    byte[] encodedBytes = new byte[bufferPosition];
                                    System.arraycopy(buffer, 0,
                                            encodedBytes, 0,
                                            encodedBytes.length);
                                    final String data = new String(
                                            encodedBytes, "US-ASCII");
                                    bufferPosition = 0;

                                    handler.post(new Runnable() {
                                        public void run() {
                                            textView.setText(data);
                                        }
                                    });
                                } else {
                                    buffer[bufferPosition++] = b;
                                }
                            }
                        }
                    } catch (IOException ex) {
                        stopThread = true;
                    }
                }
            }
        });
        thread.start();
    }

Я так думаю, что для получения данных нужен inputStream? Как можно получить его с другого класса?
  • Вопрос задан
  • 131 просмотр
Пригласить эксперта
Ответы на вопрос 1
@onepavel
Консультация и разработка мобильных приложений
Для таких вещей используют Service
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы