Benz
Benz

Reputation: 463

Execute an external class from my main class file

I have a main class which manages the ui of my application and other things.

There is another class named BluetoothConnection which manages the bt connection.

I'm new to java, what I need to understand is how to use this external class! I know that is a very simple question :)

I'm initialing the second class in the main one with

private BluetoothConnection btConnection;

Then in the main class onCreat method I'm doing:

btConnection.run();

Is that right? My app crashes but probably is for other reasons.

Here is the code of the second class

public class BluetoothConnection extends Thread{

    public BluetoothSocket mmSocket = null;
    public BluetoothAdapter mAdapter;
    private InputStream mmInStream = null;
    private OutputStream mmOutStream = null;
    byte[] buffer;

    private static final UUID MY_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");

    public BluetoothConnection(BluetoothDevice device) {

        BluetoothSocket tmp = null;

        // 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
                mAdapter.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();
                } catch (IOException e) {
                    //connection to device failed so close the socket
                    try {
                        mmSocket.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        });

        connectionThread.start();

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = mmSocket.getInputStream();
            tmpOut = mmSocket.getOutputStream();
            buffer = new byte[1024];
        } 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
                mmInStream.read(buffer);
                // 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
            mmOutStream.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 70

Answers (2)

yshahak
yshahak

Reputation: 5096

In the line :

 private BluetoothConnection btConnection;

You don't initalize the class, you just declare about intance of it. you need to add before you call to the run:

btConnection = new BluetoothConnection();
btConnection.run();

Upvotes: 1

Guillermo Merino
Guillermo Merino

Reputation: 3257

run() is the hook method of a thread, you shouldn't call run() unless you want this code to be executed in the same thread you're calling it. To start a thread call start().

Also consider this discussion to implement a Runnable instead of extend a Thread.

About your crash, as @yshahak says in his answer, check the initialization of your thread.

Upvotes: 1

Related Questions