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

后端 未结 16 2381
天涯浪人
天涯浪人 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:30

    well, i had the same problem with my code, and it's because since android 4.2 bluetooth stack has changed. so my code was running fine on devices with android < 4.2 , on the other devices i was getting the famous exception "read failed, socket might closed or timeout, read ret: -1"

    The problem is with the socket.mPort parameter. When you create your socket using socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); , the mPort gets integer value "-1", and this value seems doesn't work for android >=4.2 , so you need to set it to "1". The bad news is that createRfcommSocketToServiceRecord only accepts UUID as parameter and not mPort so we have to use other aproach. The answer posted by @matthes also worked for me, but i simplified it: socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);. We need to use both socket attribs , the second one as a fallback.

    So the code is (for connecting to a SPP on an ELM327 device):

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    
        if (btAdapter.isEnabled()) {
            SharedPreferences prefs_btdev = getSharedPreferences("btdev", 0);
            String btdevaddr=prefs_btdev.getString("btdevaddr","?");
    
            if (btdevaddr != "?")
            {
                BluetoothDevice device = btAdapter.getRemoteDevice(btdevaddr);
    
                UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // bluetooth serial port service
                //UUID SERIAL_UUID = device.getUuids()[0].getUuid(); //if you don't know the UUID of the bluetooth device service, you can get it like this from android cache
    
                BluetoothSocket socket = null;
    
                try {
                    socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
                } catch (Exception e) {Log.e("","Error creating socket");}
    
                try {
                    socket.connect();
                    Log.e("","Connected");
                } catch (IOException e) {
                    Log.e("",e.getMessage());
                    try {
                        Log.e("","trying fallback...");
    
                        socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
                        socket.connect();
    
                        Log.e("","Connected");
                    }
                 catch (Exception e2) {
                     Log.e("", "Couldn't establish Bluetooth connection!");
                  }
                }
            }
            else
            {
                Log.e("","BT device not selected");
            }
        }
    

提交回复
热议问题