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

后端 未结 3 1637
半阙折子戏
半阙折子戏 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:10

    Dirty cache. Find where on the device to clear app cache, then reboot the thing. You may have a device flow control interrupter (firewall, app wall, security software) that intercepts the port and re-issues it-- this is the problem. Depending upon where it instantiates at startup, it's now different than what your app wants. Therefore, rebuild the table by snarfing the cache and rebooting to build the cache/redirect tables back. I know this sounds wickedly ephemeral, but it worked for me.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-01 17:14

    In the constructor of BluetoothConnector.java, a list of uuidCandidates is passed.

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

    Have you passed it null?

    If yes,then try calling device.fetchUuidsWithSdp() for the Bluetooth device you want to connect and receive BluetoothDevice.ACTION_UUID intent in your receiver.As this will fetch a list of uuids supported for the device.

    0 讨论(0)
提交回复
热议问题