IOException: read failed, socket might closed - Bluetooth on Android 4.3

后端 未结 16 2473
天涯浪人
天涯浪人 2020-11-22 04:08

Currently I am trying to deal with a strange Exception when opening a BluetoothSocket on my Nexus 7 (2012), with Android 4.3 (Build JWR66Y, I guess the second 4.3 update). I

16条回答
  •  佛祖请我去吃肉
    2020-11-22 04:47

    Well, I have actually found the problem.

    The most people who try to make a connection using socket.Connect(); get an exception called Java.IO.IOException: read failed, socket might closed, read ret: -1.

    In some cases it also depends on your Bluetooth device, because there are two different types of Bluetooth, namely BLE (low energy) and Classic.

    If you want to check the type of your Bluetooth device is, here's the code:

            String checkType;
            var listDevices = BluetoothAdapter.BondedDevices;
            if (listDevices.Count > 0)
            {
                foreach (var btDevice in listDevices)
                {
                    if(btDevice.Name == "MOCUTE-032_B52-CA7E")
                    {
                        checkType = btDevice.Type.ToString();
                        Console.WriteLine(checkType);
                    }
                }
            }
    

    I've been trying for days to solve the problem, but since today I have found the problem. The solution from @matthes has unfortunately still a few issues as he said already, but here's my solution.

    At the moment I work in Xamarin Android, but this should also work for other platforms.

    SOLUTION

    If there is more than one paired device, then you should remove the other paired devices. So keep only the one that you want to connect (see the right image).

    In the left image you see that I have two paired devices, namely "MOCUTE-032_B52-CA7E" and "Blue Easy". That's the issue, but I have no idea why that problem occurs. Maybe the Bluetooth protocol is trying to get some information from another Bluetooth device.

    However, the socket.Connect(); works great right now, without any problems. So I just wanted to share this, because that error is really annoying.

    Good luck!

提交回复
热议问题