简体   繁体   English

为什么来自BluetoothSocket的输入/输出流被评估为非空值,然后引发空指针异常?

[英]Why do input/outputstream from BluetoothSocket get evaluated as NOT null, then throw null pointer exception?

I have an inputstream and outputstream created from a bluetooth socket that I try to write something to after checking if the socket is null (this is all within the OnCreate function): 我有一个从蓝牙套接字创建的输入流和输出流,在检查套接字是否为空(这全部在OnCreate函数中)之后,我尝试向其中写入一些内容:

BluetoothDevice btDevice = ...//get bluetooth device from user's selection of paired devices

UUID MY_UUID = btDevice.getUuid();

BluetoothDevice remotedevice = btAdapter.getRemoteDevice(btDevice.getAddress());

BluetoothSocket btsocket = remotedevice.createRfcommSocketToServiceRecord(MY_UUID);

InputStream inputStream = btsocket.getInputStream();
OutputStream outputStream = btsocket.getOutputStream();

if (outputStream != null){
         outputStream.write(1);
}

Whether or not the bluetooth device is connected or in range, the output stream will be evaluated as NOT null and the attempt will be made to write to it. 无论蓝牙设备是否已连接或在范围内,输出流都将被评估为“非空”,并尝试对其进行写入。 This write attempt is what triggers the null pointer exception. 这种写尝试是触发空指针异常的原因。

Why does this happen? 为什么会这样? Why is the outputStream evaluated NOT null on one line, and then throws a null pointer exception immediately on the next line? 为什么outputStream的评估结果在一行中不为null,然后在下一行立即引发null指针异常? I've tried this with a few different paired bluetooth devices and get the same result. 我已经尝试使用几种不同的配对蓝牙设备来获得相同的结果。

 java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
OutputStream outputStream = btsocket.getOutputStream();

outputStream is never null, so your null check will always return true. outputStream永远不会为null,因此您的null检查将始终返回true。

OutputStream getOutputStream ()
Get the output stream associated with this socket.

The output stream will be returned even if the socket is not yet connected, but operations on that stream will throw IOException until the associated socket is connected.

Ideally as per documentation it should throw IOException (and it does so for API LEVEL >= 21) 理想情况下,根据文档,它应该引发IOException (并且对于API LEVEL> = 21会这样做)

public void write(int oneByte) throws IOException {
    byte b[] = new byte[1];
    b[0] = (byte)oneByte;
    mSocket.write(b, 0, 1);
}

mSocket.write(b, 0, 1) uses mSocketOS which is null and causing the exception. mSocket.write(b, 0, 1)使用null mSocketOS并导致异常。 With API >= 21 you will get IOException with message "write is called on null OutputStream" 使用API​​> = 21,您将获得IOException并显示消息“在null OutputStream上调用了写操作”

You can use btsocket.connect() to initiate the outgoing connection which will initialize required mSocketOS . 您可以使用btsocket.connect() 发起传出连接 ,这将初始化所需的mSocketOS

Before writing to socket you should call isConnected() which will return true only if there is an active connection with remote device. 在写入套接字之前,您应该调用isConnected() ,该方法仅在与远程设备存在活动连接时才返回true。

 if(btsocket.isConnected()) {
     outputStream.write(1);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM