蓝牙创建套接字但在 connect() 处阻塞 ANDROID

发布于 2024-09-27 19:20:33 字数 2725 浏览 2 评论 0原文

我正在使用 Google 蓝牙聊天示例中的代码来设置蓝牙监听服务器。我希望应用程序始终侦听传入连接。当应用程序看到连接请求时,它将接受连接,读取远程设备将发送的字符串,然后通过发送文件进行响应。

问题是该应用程序从不接受连接。它在 socket = mmServerSocket.accept(); 处阻塞,并且永远不会继续前进。请参阅以下行后的代码 if(D) System.out.println("Waiting to connect************"); 为了测试它,我启动了活动它启动线程,然后尝试连接我的笔记本电脑并发送文件。当我这样做时,整个 Android 蓝牙管理器会看到该文件并有效地绕过我的 Android 设备下载它。这是测试它的唯一方法吗?我无法弄清楚这是测试问题还是编码问题。

private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "listen() failed", e);
        }
        mmServerSocket = tmp;
    }

    public void run() {
        if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
        setName("AcceptThread");
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                if(D) System.out.println("Waiting to connect************");
                socket = mmServerSocket.accept();
                if(D) System.out.println("We have accepted connection and are connected***************");
            } catch (IOException e) {
                Log.e(TAG, "accept() failed", e);
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothServer.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D) Log.i(TAG, "END mAcceptThread");
    }

I am using code from the Google Bluetooth Chat Example to set up a bluetooth listening server. I want the app to listen always for an incoming connection. When the app sees a connection request it will accept the connection, read in the string that the remote device will send, and then respond by sending a file.

The problem is that the app never accepts the connection. It blocks at socket = mmServerSocket.accept(); and never moves on. See the code below after the line if(D) System.out.println("Waiting to connect************"); To test it I start the activity that starts the thread and then attempt to connect with my laptop and send a file. When I do this the overall Android bluetooth manager sees the file and downloads it effectively bypassing my Android device. Is this the only way to test it? I cant figure out if its a testing or coding problem.

private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "listen() failed", e);
        }
        mmServerSocket = tmp;
    }

    public void run() {
        if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
        setName("AcceptThread");
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                if(D) System.out.println("Waiting to connect************");
                socket = mmServerSocket.accept();
                if(D) System.out.println("We have accepted connection and are connected***************");
            } catch (IOException e) {
                Log.e(TAG, "accept() failed", e);
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothServer.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D) Log.i(TAG, "END mAcceptThread");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光沙漏 2024-10-04 19:20:33

此问题的解决方案位于您的 androidmanifest.xml 文件中;

uses-permission android:name="android.permission.BLUETOOTH_ADMIN" 
uses-permission android:name="android.permission.BLUETOOTH"

手机没有授予您的程序权限

我实际上添加了一些代码,但我猜它已被编辑掉

The solution to this problem lies in your androidmanifest.xml file;

uses-permission android:name="android.permission.BLUETOOTH_ADMIN" 
uses-permission android:name="android.permission.BLUETOOTH"

The phone isn't giving your program permissions

I have actually added some code but I guess it got edited out

春花秋月 2024-10-04 19:20:33

检查您的客户端是否正在寻找与 MY_UUID 相同的 UUID。您可能想要将 UUID 更改为 RFCOMM 的通用 UUID。

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

Check if your client side is looking for same UUID as MY_UUID. You may want to change the UUID to generic UUID for RFCOMM.

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
关于从前 2024-10-04 19:20:33

我也有同样的问题。我正在使用蓝牙聊天示例,该示例从输出 ADC 数据的 arduino BT 读取数据。一切都工作正常,直到我更新了 SDK。直到我尝试使用 DataInputStream 包装的 InpuStream 读取数据,程序才会退出,这样我就可以使用 readline() 函数。我有正确的 UUID。我多次尝试

更改读取功能:

public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        mmDataInStream = new DataInputStream(mmInStream);

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the DataInputStream
                String mess = mmDataInStream.readLine();
                int mes = Integer.parseInt(mess);
                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, mes)
                        .sendToTarget();

            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

I'm having the same problem. I'm using the Bluetooth Chat example that reads data from an arduino BT that is outputing ADC data. Everything was working fine until I updated my SDK. The program doesn't exit until I try to read in data using InpuStream wrapped with DataInputStream so I can use the readline() function. I have the right UUID. I've tried multiple time

my altered read function:

public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        mmDataInStream = new DataInputStream(mmInStream);

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the DataInputStream
                String mess = mmDataInStream.readLine();
                int mes = Integer.parseInt(mess);
                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, mes)
                        .sendToTarget();

            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文