从我的 class 主文件执行外部 class

Execute an external class from my main class file

我有一个主要的 class 管理我的应用程序的 ui 和其他东西。

还有另一个名为 BluetoothConnection 的 class 管理 bt 连接。

我是java的新手,我需要了解的是如何使用这个外部class!我知道这是一个非常简单的问题:)

我正在用

为主要的第二个 class 起草
private BluetoothConnection btConnection;

然后在主要的 class onCreat 方法中我正在做:

btConnection.run();

是吗?我的应用程序崩溃了,但可能是因为其他原因。

这是第二个的代码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();
        }
    }
}

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().

同时考虑 this 讨论实现 Runnable 而不是扩展线程。

关于你的崩溃,正如@yshahak 在他的回答中所说,检查你的线程的初始化。

在行中:

 private BluetoothConnection btConnection;

您无需初始化 class,您只需声明它的实例。 您需要在调用 运行:

之前添加
btConnection = new BluetoothConnection();
btConnection.run();