Bluetooth Connection failed “java.io.IOException: read failed, socket might closed or timeout, read ret: -1”

后端 未结 3 1636
半阙折子戏
半阙折子戏 2020-12-01 16:32

I am trying to connect devices through my app installed in Nexus 5.I want to make an app like rainbow contacts in android. In my app I aim to connect to another device throu

3条回答
  •  粉色の甜心
    2020-12-01 17:12

    Within the try ("//try the fallback") use this:

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

    Another tip may be to use a different UUID, it may solve the problem.


    I don't know if you tryed it yet, but similarly to what has been done in the bluetooth chat example, just before creating your ConnectThread, call this function:

    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);
    }
    

提交回复
热议问题