蓝牙连接失败:读取失败,套接字可能关闭或超时,读取ret:-1

Jos*_* M. 6 java android bluetooth

我正在尝试创建蓝牙连接.

我可以搜索附近的设备但是当我尝试连接时出现错误,我不明白:

logcat的

01-03 00:55:06.909 6654-6654/com.bluetooth.prova3.listdiscovery D/CONNECTTHREAD: Could not close connection:java.io.IOException: read failed, socket might closed or timeout, read ret: -1
Run Code Online (Sandbox Code Playgroud)

我有两个连接类,一个接收设备并执行连接,另一个用于连接.

ConexionActivity.Java

package com.bluetooth.prova3.listdiscovery;

***Imports*****

public class ConexionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conexion);

        //Aqui rebo el dispositiu que he seleccionat per conectarme
        Intent intent = getIntent();
        BluetoothDevice bluetoothDevice = intent.getExtras().getParcelable("btdevice");
        //mostro el nom per la pantalla amb un text view
        TextView MacAddress = (TextView)findViewById(R.id.MAC);
        String aaaa = bluetoothDevice.getName() + "\n" + bluetoothDevice.getAddress();
        MacAddress.setText(aaaa);

        ConnectThread conexion = new ConnectThread(bluetoothDevice);
        conexion.run();
    }
}
Run Code Online (Sandbox Code Playgroud)

ConnectThread.java

package com.bluetooth.prova3.listdiscovery;
Run Code Online (Sandbox Code Playgroud)

进口

public class ConnectThread extends Thread{
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    UUID UUIDaleatorio = UUID.randomUUID();
    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(UUIDaleatorio);
        } catch (IOException e) {

            Log.d("CONNECTTHREAD", "Could not close connection:" + e.toString());
        }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        //mBluetoothAdapter.cancelDiscovery();
       try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
           Log.d("CONNECTTHREAD", "Could not close connection:" + connectException.toString());
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { Log.d("CONNECTTHREAD", "Could not close connection:" + closeException.toString());}
            return;
        }

        // Do work to manage the connection (in a separate thread)
       // manageConnectedSocket(mmSocket);
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*eek 12

尝试使用,

createInsecureRfcommSocketToServiceRecord(MY_UUID)

代替

createRfcommSocketToServiceRecord(MY_UUID)

这应该可以解决问题.如果这不能解决问题,请分享您的调试结果.

此外,不要生成随机UUID,尝试下面的一个.

UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  • prateek,感谢您的回答,但我在 logcat 中遇到相同的错误。 (3认同)
  • UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); ,尝试使用这个UUID。 (3认同)
  • 好的,那就工作了!谢谢!问题出在 randomUUID() 上? (2认同)
  • 它没有为我解决任何问题,我没有生成 GUID,而是使用与您相同的 GUID (2认同)

naf*_*med 6

这对我有用..

final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb";
mmSocket=device.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid());

        // mmSocket = device.createRfcommSocketToServiceRecord(uuid);
           Log.d("Connection","Created");
        try {
            mmSocket.connect();
                    Log.d("Connection","Connected");

                }catch (Exception e){
                    if(mmSocket != null) {
                        try {
                            mmSocket.close();
                        } catch (IOException e1) {

                    Log.e("Connection","Socket close Error"+e1.getMessage());
                        }
                        mmSocket = null;
                    }
                    e.printStackTrace();
                    Log.e("Connection","Genmral Error "+e.getMessage());
                }
Run Code Online (Sandbox Code Playgroud)