开发者

Bluetooth creates socket but blocks at connect() ANDROID

开发者 https://www.devze.com 2023-01-19 13:38 出处:网络
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 c开发者_Python百科onnection

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 c开发者_Python百科onnection 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");
    }


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


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");


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;
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消