akexorcist / BluetoothSPPLibrary

[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth
Apache License 2.0
1.7k stars 570 forks source link

ConnectThread NullPointerException when the BT has been manually disabled #92

Open artetrad opened 6 years ago

artetrad commented 6 years ago

Hi, I am using the DeviceList Activity in order to choose a device. But if I disable the BT, when viewing the DeviceList screen and choose one of the already rendered results, then in my onActivityResult() I get an NPE when trying to connect.

Specifically, in the BluetoothService.java, the ConnectThread's run method, the mmSocket.connect(); line gets a NPE, when executed. Since you already handle the IOException, would it be possible to add another NullPointerException catch block below?

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) {
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) { }
                connectionFailed();
                return;
            }

            // Reset the ConnectThread because we're done
            synchronized (BluetoothService.this) {
                mConnectThread = null;
            }

            // Start the connected thread
            connected(mmSocket, mmDevice, mSocketType);
        }

Thanks!