inputStream.read()导致NullPointerException(在检查inputStream之后!= null)

The*_*ant 5 android bluetooth inputstream nullpointerexception

我正在编写一个需要与蓝牙2.1设备交换数据的应用程序.我已经做了好几次了,但这次发生了一些奇怪的事情.

    Log.d("TAG", "connectToDevice");

    if(macAddress != null)
        deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);

    Log.d("TAG", "macAddress != null");


    if(deviceToConnect != null)
        try {
            btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
        } catch (IOException e) {
            btSocket = null;
            e.printStackTrace();
        }

    Log.d("TAG", "deviceToConnect != null");

    if(btSocket != null){

        try {
            inputStream = btSocket.getInputStream();
            Log.d("TAG", "inputStream OK");

        } catch (IOException e) {
            inputStream = null;
            Log.d("TAG", "inputStream KO");
            e.printStackTrace();
        }

        try {
            outputStream =  btSocket.getOutputStream();
            Log.d("TAG", "outputStream OK");

        } catch (IOException e) {
            outputStream = null;
            Log.d("TAG", "outputStream KO");
            e.printStackTrace();
        }

    }


    Log.d("TAG", "btSocket != null");
    Log.d("TAG", "onConnectionEstablished");
Run Code Online (Sandbox Code Playgroud)

在发现阶段之后,我得到了我需要连接的蓝牙设备,然后我获得了套接字,输入和输出流.

然后我有一个从输入流中读取的线程.

   int byteRead;

    while (listening) {
        try {
            if(inputStream!=null){
                Log.d("TAG", "inputStream: " +inputStream);
                byteRead = inputStream.read();
            }else{
                Log.d("TAG", "inputStream is null!");
                continue;
            } // Rest of the code here
Run Code Online (Sandbox Code Playgroud)

执行该inputStream.read()行时出现此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference

at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:427)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
at com.me.testapplication.Connection_BT21.run(Connection_BT21.java:152)
Run Code Online (Sandbox Code Playgroud)

问题1:为什么NullPointerException如果我正在检查inputStream!= null?

问题2:为什么读取(byte [],int,int)如果我试图调用read()?

The*_*ant 8

我发现了错误,你没事:插座出错了......我需要调用socket.connect()!

if(macAddress != null)
    deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);

Log.d("TAG", "macAddress != null");


if(deviceToConnect != null)
    try {
        btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
    } catch (IOException e) {
        btSocket = null;
        e.printStackTrace();
    }

Log.d("TAG", "deviceToConnect != null");

if(btSocket != null){
 //This was the line missing!
 btSocket.connect();

    try {
        inputStream = btSocket.getInputStream();
        Log.d("TAG", "inputStream OK");

    } catch (IOException e) {
        inputStream = null;
        Log.d("TAG", "inputStream KO");
        e.printStackTrace();
    }

    try {
        outputStream =  btSocket.getOutputStream();
        Log.d("TAG", "outputStream OK");

    } catch (IOException e) {
        outputStream = null;
        Log.d("TAG", "outputStream KO");
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

这就是为什么inputStream没有准备好,我得到了那个错误.

对不起,我只是没有看到它..希望它可以帮助别人!