蓝牙连接失败“java.io.IOException:读取失败,套接字可能关闭或超时,读取ret:-1”

时间:2014-09-06 08:58:35

标签: java android sockets bluetooth

我正在尝试通过我在Nexus 5中安装的应用程序连接设备。我想在android中创建类似rainbow contacts的应用程序。在我的应用程序中,我的目标是通过蓝牙连接到另一台设备并传输一组联系人或文件。 我跟着this question,但是那里提到的解决方法对我不起作用 Here是我的完整代码。 这是我的应用程序中的代码片段,我试图获取套接字并建立连接。

我能够通过配对设备对话框,但是当我尝试配对时会出现错误

//to create socket
if (secure) {
            bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
        } else {
            bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
        }
//connection establishment
try {

                bluetoothSocket.connect();
                success = true;
                break;
            } catch (IOException e) {
                //try the fallback
                try {
                    Class<?> clazz = tmp.getRemoteDevice().getClass();
                    Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
                    Method m = clazz.getMethod("createRfcommSocket", paramTypes);
                    Object[] params = new Object[] {Integer.valueOf(1)};
                    bluetoothSocket  = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
                    Thread.sleep(500);
                    bluetoothSocket.connect();
                    success = true;
                    break;
                } catch (FallbackException e1) {
                    Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
                } catch (InterruptedException e1) {
                    Log.w("BT", e1.getMessage(), e1);
                } catch (IOException e1) {
                    Log.w("BT", "Fallback failed. Cancelling.", e1);
                }
            }

我收到错误

09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication I/BT﹕ Attempting to connect to Protocol: 00001101-0000-1000-8000-00805f9b34fb
09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[56]}
09-06 13:44:58.667  27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:58.667  27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication W/BT﹕ Fallback failed. Cancelling.
    java.io.IOException: read failed, socket might closed or timeout, read ret: -1
            at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
            at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
            at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
            at com.example.gauravdubey.myapplication.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:198)
            at com.example.gauravdubey.myapplication.BluetoothConnector.connect(BluetoothConnector.java:62)
            at com.example.gauravdubey.myapplication.ConnectThread.run(ConnectThread.java:101)
            at com.example.gauravdubey.myapplication.MainActivity$1.onItemClick(MainActivity.java:288)
            at android.widget.AdapterView.performItemClick(AdapterView.java:299)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
            at android.widget.AbsListView$3.run(AbsListView.java:3645)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication V/connectThread﹕ Could not connect to device: B0:D0:9C:8B:A4:47
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication I/Choreographer﹕ Skipped 361 frames!  The application may be doing too much work on its main thread.

那么我做错了什么?任何帮助将不胜感激

4 个答案:

答案 0 :(得分:4)

BluetoothConnector.java 的构造函数中,传递 uuidCandidates 的列表。

public BluetoothConnector(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
                              List<UUID> uuidCandidates) 
          {
        this.device = device;
        this.secure = secure;
        this.adapter = adapter;
        this.uuidCandidates = uuidCandidates;

        if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
            this.uuidCandidates = new ArrayList<UUID>();
            this.uuidCandidates.add(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
        }
    }

你把它传递给了吗?

如果是,请尝试为您要连接的蓝牙设备调用device.fetchUuidsWithSdp(),并在接收器中接收BluetoothDevice.ACTION_UUID意图。因为这将获取该设备支持的uuids列表。

答案 1 :(得分:0)

try(“//尝试后备”)中使用以下内容:

socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
socket.connect();

另一个提示可能是使用不同的UUID,它可能会解决问题。


我不知道你是否尝试过它,但与创建ConnectThread之前在bluetooth chat example中所做的类似,请调用此函数:

public synchronized void connect(BluetoothDevice device) {

    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING) {
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);
    mConnectThread.start();
    setState(STATE_CONNECTING);
}

答案 2 :(得分:0)

我在Android 5.0系统上有这个奇怪的东西你可能需要清除缓存 如下所示:      http://www.gottabemobile.com/2014/12/01/nexus-lollipop-problems-fixes/

并且您可能还需要断开bt加密狗,以便在使用电脑和便宜的bt加密狗发射器时不会占用内存中的先前连接

答案 3 :(得分:0)

脏缓存。找到设备上清除应用缓存的位置,然后重新启动。您可能有一个设备流量控制中断器(防火墙,应用程序墙,安全软件)拦截端口并重新发出它 - 这就是问题所在。根据它在启动时实例化的位置,它现在与您的应用程序想要的不同。因此,通过snarfing缓存重新构建表并重新引导以构建缓存/重定向表。我知道这听起来很奇怪,但它对我有用。