BluetoothSocket Connect()抛出IOException读取失败,socket可能关闭

时间:2017-03-26 10:52:50

标签: java android sockets bluetooth

所以我正在尝试将Android应用程序连接到覆盆子Pi,设备已配对且受到信任我收到的错误与此处的错误相同: IOException: read failed, socket might closed - Bluetooth on Android 4.3

然而,该解决方案似乎非常混乱,我甚至不确定如何将其与我当前的代码集成。所以这是我目前的代码:

 @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(MY_UUID);
                //Throws exception here:
                socket.connect();
                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");
                        outputStream.write(myByteArray, 0, myByteArray.length);
                    }
                    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-26 11:44:09.525 7916-8048/com.app.name W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
03-26 11:44:09.526 7916-8048/com.app.name W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:685)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:697)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:374)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err:     at com.app.name.service.DeviceManager.onHandleIntent(DeviceManager.java:60)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:67)
03-26 11:44:09.528 7916-8048/com.app.name W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-26 11:44:09.528 7916-8048/com.app.name W/System.err:     at android.os.Looper.loop(Looper.java:154)
03-26 11:44:09.528 7916-8048/com.app.name W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
03-26 11:44:09.528 7916-8048/com.app.name E/DeviceManager: Can't create a socket to the bluetooth device.

奇怪的是,我的Raspberry Pi出现在BluetoothCtl上,说手机已经连接但是它会立即断开连接。

1 个答案:

答案 0 :(得分:0)

仅为查询Raspberry板上的SDP数据库创建连接。

1)检查Raspberry端的SDP是否确实包含所请求的MY_UUID记录

2)Raspberry板是否与您的Android绑定并不明显。这是必需的,因为您在发布的代码中创建了一个安全套接字。

  • 您可以尝试不需要具有绑定设备的不安全版本:createInsecureRfcommSocketToServiceRecord