java - 调用 Write() 时蓝牙套接字 OutputStream NullPointerException

标签 java android bluetooth

我有一个奇怪的问题,显然我的输出流为空,即使我进行了多次检查并使用调试器确认它不为空。所以有问题的代码:

 @Override
protected void onHandleIntent(Intent intent) {
    device = (BluetoothDevice) intent.getExtras().get("device");

    if (device != null) {
        Log.v("DeviceManager", "Beginning attempts to communicate to: " + device.getName());
        try {
            BluetoothSocket socket = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();

            while (Utils.appIsInForeground && device != null) {
                if (outputStream == null){
                    Log.e("DeviceManager", "Looks like the outputStream is null...");
                    if (inputStream == null){
                        Log.e("DeviceManager", "And input stream is null, are you even connected to the device?");
                    }else{
                        Log.e("DeviceManager", "Although strangely, input stream has been set.");
                    }
                }else{
                    String myString = "This is a string!";
                    byte[] myByteArray = myString.getBytes("UTF-8");
                    //Crashes here:
                    outputStream.write(myByteArray);
                }
                Thread.sleep(1000);
            }

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("DeviceManager", "Can't create a socket to the bluetooth device.");
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.e("DeviceManager", "The sleep thread was interrupted.");
        } catch (NullPointerException e) {
            e.printStackTrace();
            Log.e("DeviceManager", "A null pointer exception, this is probably not good.");
        }
    }else {
        Log.e("DeviceManager", "The Device Manager has been passed a null object instead of a device.");
    }
}

堆栈轨迹如下:

03-23 15:55:12.134 3042-3171/com.name.app W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
03-23 15:55:12.135 3042-3171/com.name.app W/System.err:     at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:553)
03-23 15:55:12.136 3042-3171/com.name.app W/System.err:     at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
03-23 15:55:12.136 3042-3171/com.name.app W/System.err:     at java.io.OutputStream.write(OutputStream.java:75)
03-23 15:55:12.137 3042-3171/com.name.app W/System.err:     at com.name.app.service.DeviceManager.onHandleIntent(DeviceManager.java:73)
03-23 15:55:12.138 3042-3171/com.name.app W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:67)
03-23 15:55:12.138 3042-3171/com.name.app W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-23 15:55:12.139 3042-3171/com.name.app W/System.err:     at android.os.Looper.loop(Looper.java:154)
03-23 15:55:12.139 3042-3171/com.name.app W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)

这是调试器对每个变量的看法:

myByteArray = {byte[17]@4799} 
this = {DeviceManager@4698} 
intent = {Intent@4705} "Intent { cmp=com.name.app/.service.DeviceManager (has extras) }"
socket = {BluetoothSocket@4706} 
myString = "This is a string!"
myByteArray = {byte[17]@4799} 
outputStream = {BluetoothOutputStream@4743} 

此时我完全感到困惑,没有任何内容是空的,但错误仍然存​​在。我还尝试了以下方法:

outputStream.write(myByteArray, 0, myByteArray.length);

它产生完全相同的轨迹。

最佳答案

在使用输入/输出流之前需要调用 socket.connect() 。 详见我的回答https://stackoverflow.com/a/42570610/5664712

关于java - 调用 Write() 时蓝牙套接字 OutputStream NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42981325/

相关文章:

java - 从非 Activity 开始 Activity

java - 如何在不覆盖的情况下向 Firebase Firestore 添加值?

java - 为什么来自BluetoothSocket的输入/输出流被评估为NOT null,然后抛出空指针异常?

java - Tesseract 错误。非法的最小或最大规范

Java apache.commons.pool : How to set a maximum idle time for the objects from the pool?

java - 使用 UrlValidator 类进行 URL 验证

android - 如何在 Gmail 应用程序中打开特定邮件/对话

java - Android: 有没有办法将 byte[] 图像拆分成两个然后拉伸(stretch)它们?

android - 从文档中启用 Android 蓝牙

android - 使 Android 2.1 上的蓝牙无限期可发现