R
R
rmiron2018-11-08 15:10:38
Java
rmiron, 2018-11-08 15:10:38

How to pass inputStream from one activity to another?

Welcome all!
I'll go straight to the point. There is an Android application using a bluetooth connection (connection with an arduino device). I need to use an already existing data stream in another activity.

bluetooth connection source
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;
        }
data acquisition
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();
    }

I think that inputStream is needed to receive data? How can I get it from another class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2018-11-14
@onepavel

For such things use Service

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question