与蓝牙 PC 服务器的蓝牙套接字连接失败 "ava.io.IOException: read failed, socket might closed or timeout, read ret: -1"

Bluetooth socket connection with Bluetooth PC server fails "ava.io.IOException: read failed, socket might closed or timeout, read ret: -1"

我正在尝试在 Android 智能手机(客户端)与 PC 上的蓝牙应用程序(服务器)运行 之间建立连接。

下面是客户端的代码片段

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

    private class ConnectThread extends Thread {
    private BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        try {
            //tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

        } catch (IOException e) {
            Log.d(TAG, "create() failed", e);
            e.printStackTrace();
        }
        mmSocket = tmp;
    }


    public void run() {
        mAdapter.cancelDiscovery();

        try {

            mmSocket.connect();
        }

        catch (IOException e) {  
        //Exception caught 
        //java.io.IOException: read failed, socket might closed or timeout,      read ret: -1
        ...
        }
        ...
    }

以下是服务器代码(使用 BlueCover jar)

private void waitForConnection() { LocalDevice local = null;

    StreamConnectionNotifier notifier;
    StreamConnection connection = null;

    try {
        local = LocalDevice.getLocalDevice();
        local.setDiscoverable(DiscoveryAgent.GIAC);
        String uuidstr = "00001101-0000-1000-8000-00805F9B34FB";
        String uuid_wo_space = uuidstr.replaceAll("-", "");      

        String url = "btspp://localhost:" + uuid_wo_space  + ";authenticate=false;encrypt=false;name=RemoteBluetooth";
        notifier = (StreamConnectionNotifier) Connector.open(url);
    }
    catch (Exception e) {
        e.printStackTrace();
        return;
    }

    // waiting for connection
    while(true) {
        try {
            connection = notifier.acceptAndOpen();
            Thread processThread = new Thread(new ProcessConnectionThread(connection));
            processThread.start();
        }
        catch(Exception e) {
            e.printStackTrace();
            return;
        }
    }

我阅读了几个建议将 UUID 更改为“00001101-0000-1000-8000-00805F9B34FB”的链接,我已经尝试过了。我还尝试使用 createInsecureRfcommSocketToServiceRecord 创建一个不安全的套接字,但仍然失败,结果相同。

下面是 IOException 堆栈跟踪

java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:912)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:531)

通常有一组 UUID 表示不同的操作模式,如文件传输 (FTP)、远程 SIM、Phone 图书请求等

您可以像这样查询并尝试您界面上的所有 UUID:

device = (from bd in adapter.BondedDevices
            where bd.Name == "YourDeviceName"
            select bd).FirstOrDefault();

Android.OS.ParcelUuid[] parcel = device.GetUuids();

for (int i = 0; i < parcel.Length; i++) {
    socket = device.CreateRfcommSocketToServiceRecord(parcel[i].Uuid);
    try {
        socket.Connect();
        break;
    }
    catch {
        throw new Exception("Unsupported UUID");
    }
}

还要确保没有其他设备连接到 PC/Android 并且有一个打开的套接字,因为一次只能为一个套接字提供服务。您可以拥有任意多对,但只能有一个插座 运行.

很抱歉写了一个新答案,但这样信息比回复更明显。以下是我的设备的 UUID 示例列表:

UUID: 0000xxxx-0000-1000-8000-00805f9b34fb          

i   xxxx    Status  Mode
0   110a    N.A.    
1   1105    Connected   Serial Port Protocol (SPP)
2   1106    Connected   File transfer (FTP)
3   1116    N.A.    
4   112d    Connected   Remote SIM mode
5   112f    Connected   Phone book request
6   1112    Connected   
7   111f    Connected   
8   1132    Connected   Message access request