Search code examples
javaandroidbroadcastreceiverstack-overflow

android- BroadcastReceiver -java.lang.StackOverflowError


I am receiving a StackOverflowError when trying to receive a broadcast I'm assuming that this error comes because of the number of times the broadcast is sent, but it throws the error when I try to get the context of the activity I am broadcasting to. I will post my code bellow as well.

        D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairba
                ERR: TOTAL BYTES WRITTEN: 1447748

This is the program that makes the connection and broadcasts it.

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.Scanner;
import java.util.UUID;
// import org.apache.commons.io.IOUtils;

    public class BluetoothConnection extends Thread {

        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        byte[] buffer;
        BluetoothAdapter mmAdapter;

        public static String output = "ABC";


        // Unique UUID for this application, you may use different
        private static  UUID MY_UUID ;



        public BluetoothConnection(BluetoothDevice device) {

            UUID MY_UUID = device.getUuids()[0].getUuid();

            BluetoothSocket tmp = null;
            mmAdapter = null;
            System.out.println("$$$$$$$$$$$$$$$$****** in constructor ******$$$$$$$$$$$$$$$$");

            // Get a BluetoothSocket for a connection with the given BluetoothDevice
            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mmSocket = tmp;

            //now make the socket connection in separate thread to avoid FC
            Thread connectionThread  = new Thread(new Runnable() {

                @Override
                public void run() {
                    // Always cancel discovery because it will slow down a connection
                    Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");

                    //mmAdapter.cancelDiscovery();

                    // Make a connection to the BluetoothSocket
//                    try {
//                        // This is a blocking call and will only return on a
//                        // successful connection or an exception
//                        mmSocket.connect();
//
//
//                        System.out.println("\n\n\n\n\n\n socket connected\n\n\n\n\n\n");
//
//                    } catch (IOException e) {
//                        //connection to device failed so close the socket
//                        try {
//                            mmSocket.close();
//                            System.out.println("\n\n\n\n\n\n socket closed\n\n\n\n\n\n");
//
//                        } catch (IOException e2) {
//                            System.out.println("\n\n\n\n\n\n in catch\n\n\n\n\n\n");
//
//                            e2.printStackTrace();
//                        }
//                    }
                }
            });

            connectionThread.start();

            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    mmSocket.connect();


                    System.out.println("$$$$$$$$$$$$$$$$****** socket connected ******$$$$$$$$$$$$$$$$");

                } catch (IOException e) {
                    //connection to device failed so close the socket
                    try {
                        mmSocket.close();
                        System.out.println("$$$$$$$$$$$$$$$$****** socket closed ******$$$$$$$$$$$$$$$$");

                    } catch (IOException e2) {
                        System.out.println("$$$$$$$$$$$$$$$$****** in catch ******$$$$$$$$$$$$$$$$");

                        e2.printStackTrace();
                    }
                }

                tmpIn = mmSocket.getInputStream();
                tmpOut = mmSocket.getOutputStream();
                buffer = new byte[50];
            } catch (IOException e) {
                e.printStackTrace();
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }


        public void run() {

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    //read the data from socket stream
                    if(mmInStream != null) {
                      mmInStream.read(buffer);
                      for(byte b : buffer)
                      {
                          char c = (char) b;
                          if(c >=' ' && c <'z') {
                            System.out.print(c);
                            output += c;
                          }

                      }
                       System.out.println();
                        Intent intent = new Intent("com.example.curie.fairbanks_01");
                        intent.putExtra("output",output);
                        LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcast(intent);
                        output = "";
                    }
                    // Send the obtained bytes to the UI Activity
                } catch (IOException e) {
                    //an exception here marks connection loss
                    //send message to UI Activity
                    break;
                }
            }
        }

        public void write(byte[] buffer) {
            try {
                //write the data to socket stream
                if(mmOutStream != null)
                mmOutStream.write(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

this Program receives the broadcast and throws the error.

import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v4.content.LocalBroadcastManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class InputActivity extends AppCompatActivity {

         TextView inputWindow;
         Button readWeight;
         BroadcastReceiver resultReceiver;

        BluetoothDevice instrument;
        BluetoothConnection connector;
        String weight;

        @Override
        protected void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_input);

            inputWindow = (TextView) findViewById(R.id.input_window);
            readWeight = (Button) findViewById(R.id.read_weight);

            instrument = DeviceList.instrument;
            System.out.println("$$$$$$$$$$$$$$$$****** In new Activity ******$$$$$$$$$$$$$$$$");
            connector = new BluetoothConnection(instrument);

            inputWindow.setText(BluetoothConnection.output);

            readWeight.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    connector.run();
                    //readWeight.setVisibility(View.INVISIBLE);
                }
            });

            resultReceiver = createBroadcastReceiver();
            LocalBroadcastManager.getInstance(this).registerReceiver(resultReceiver, new IntentFilter("com.intertech.random.generation"));

        }


        public static Context getContext()
        {
            return getContext();

        }

        private BroadcastReceiver createBroadcastReceiver() {
            return new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    updateResults(intent.getStringExtra("result"));
                }
            };
        }

        public void updateResults(String update)
        {
            inputWindow.setText(update);

        }




    }

Solution

  • The broadcast receiver should be set up like this...

    private BroadcastReceiver createBroadcastReceiver() {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                weight = intent.getStringExtra("Output");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //what you want to happen
                    }
                });
            }
        };
    }// end createBroadcastReceiver