BluetoothServerSocket.accept () is not returned even if the device is paired

I am developing an Android bluetooth application based on the example of BluetoothChat . I start the bluetooth server and listen to the device (not the phone) to connect to my application in an insecure rfcomm connection.

private class AcceptThread extends Thread { // The local server socket private final BluetoothServerSocket mmServerSocket; public AcceptThread(boolean secure) { BluetoothServerSocket tmp = null; // Create a new listening server socket try { tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE); } catch (Exception e) { Log.e(TAG, ".AcceptThread # listen() failed", e); } mmServerSocket = tmp; } public void run() { 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 Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn"); socket = mmServerSocket.accept(); //FIXME: it blocks here Log.d(TAG, ".AcceptThread.run # server socket connection accepted"); } catch (Exception e) { MMLog.e(TAG, ".run # accept() failed: "+e); connectionFailed(); break; } // If a connection was accepted if (socket != null) { synchronized (BluetoothService.this) { switch (mState) { case STATE_LISTEN: case STATE_CONNECTING: // starting the thread where i will receive input // streams from the other device 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; } } } } } public void cancel() { try { if(mmServerSocket != null) { mmServerSocket.close(); } } catch (IOException e) { Log.e(TAG, ".cancel # Could not close server socket: ", e); } } } 

I am using HTC Desire S, android 2.3.5. The device is paired, but I am not receiving data because the connection is blocked in the ".accept ()" method. He just keeps waiting.

socket = mmServerSocket.accept(); // ... and wait

  • Why is he still waiting if the device is paired?
  • How can I make a connection because I also tried to flip, and there is still no result.
  • Is there a problem with HTC Bluetooth glass? Has anyone made a connection, possibly using a different Android phone?
+4
source share
2 answers

I think that something is wrong with your code, there should not be socket = tmp.accept(); Here is what I need to do to connect to the socket server:

 BluetoothServerSocket serverSocket = null; BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter(); try { serverSocket = bta.listenUsingInsecureRfcommWithServiceRecord("BluetoothChatInsecure", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { e.printStackTrace(); } while(!Thread.interrupted()) { try { socket = serverSocket.accept(); if (socket != null) { Log.d("CONNECTED", "Connected bluetooth"); /// do your stuff 
0
source

This is most likely due to your other device (this is happening to me). Your Android is already doing its job, which lists incoming connections. There are many reasons why your special device will not connect correctly to your Android phone:

  • The device mysteriously switches to another Bluetooth profile, for example. HDP instead of SPP
  • The device somehow remembers another Android phone in its memory (the latter is connected to it or something like that) and continues to try to connect to this phone, but not the one you are using right now.

I believe that your best chance is to ask the manufacturer / seller of a special device for detailed specifications and / or software / driver for its configuration / testing.

0
source

Source: https://habr.com/ru/post/1395775/


All Articles