gpt4 book ai didi

android - inputStream.read() 导致 NullPointerException(检查 inputStream 后!=null)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:21 29 4
gpt4 key购买 nike

我正在编写一个需要与蓝牙 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");

在发现阶段之后,我得到了我需要连接的 BluetoothDevice,然后我获得了套接字、输入和输出流。

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

   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

我在执行 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)

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

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

最佳答案

我发现了错误,你没问题:套接字有错误。我需要调用 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();
}

}

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

抱歉,我只是没有看到它..希望它可以帮助别人!

关于android - inputStream.read() 导致 NullPointerException(检查 inputStream 后!=null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267671/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com