Application are not entering "Public void run()" section

时间:2016-07-11 23:16:55

标签: java android bluetooth

I've been create an app for Bluetooth control.

Everything is fine until I open BluetoothServerSocket to listen to an incoming connection.

here is my code:

public class ListeningThread extends Thread {
    private final BluetoothServerSocket bluetoothServerSocket;

    public ListeningThread() {
        BluetoothServerSocket temp = null;
        try {
            temp = myBluetoothAdapter.listenUsingRfcommWithServiceRecord(getString(R.string.app_name), uuid);
            Toast.makeText(getApplicationContext(), "Listening",
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bluetoothServerSocket = temp;
    }

    public void run() {
        BluetoothSocket bluetoothSocket;
        // This will block while listening until a BluetoothSocket is returned
        // or an exception occurs
        while (true) {
            try {
                bluetoothSocket = bluetoothServerSocket.accept();
                Toast.makeText(getApplicationContext(), "Alert", <-------//the code is not run through here, there is no toast coming out
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                break;
            }
            // If a connection is accepted
            if (bluetoothSocket != null) {

                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "A connection has been accepted.",
                                Toast.LENGTH_SHORT).show();
                    }
                });

                // Manage the connection in a separate thread

                try {
                    bluetoothServerSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    // Cancel the listening socket and terminate the thread
    public void cancel() {
        try {
            bluetoothServerSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

there is no error shown but when i triggered the ListeningThread() function, this line of code is running Toast.makeText(getApplicationContext(), "Listening" it is shown that my server is listening to incoming connection but when I'm using other client to connect to this server, then client are showing "paired" but on the server there is nothing shown, but the connection was actually successfull.

from the line //This will block while listening until a BluetoothSocket is returned//or an exception occurs and //If a connection is acceptedis not running, because the toast did not coming out.

this function is called from MainActivity Threads and was putted on a button. I don't think pasting all the code here will be good, but here is the button codes. if there is anything you need to see, simply ask me.

serverBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               ListeningThread t = new ListeningThread();
                t.start();
            }
        });

0 个答案:

没有答案