BluetoothServerSocket.accept () will not return in an accept stream while a client socket is acquired

So, I have this Android bluetooth project and I am getting a really nasty problem; Let me describe the context:

The two phones that need to be connected are compatible with BluetoothSockets using the exact method described in the Bluetooth documentation on the Android developers site ( here ); Therefore, I use the following streams to connect:

private class AcceptThread extends Thread { private final BluetoothServerSocket mmServerSocket; public AcceptThread() { // Use a temporary object that is later assigned to mmServerSocket, // because mmServerSocket is final BluetoothServerSocket tmp = null; try { // MY_UUID is the app UUID string, also used by the client code tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); } catch (IOException e) { } mmServerSocket = tmp; } public void run() { BluetoothSocket socket = null; // Keep listening until exception occurs or a socket is returned while (true) { try { socket = mmServerSocket.accept(); } catch (IOException e) { break; } // If a connection was accepted if (socket != null) { // Do work to manage the connection (in a separate thread) manageConnectedSocket(socket); try { mmServerSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } /** Will cancel the listening socket, and cause the thread to finish */ public void cancel() { try { mmServerSocket.close(); } catch (IOException e) { } } } 

This is acceptthread and

 private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; 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 UUID string, also used by the server code tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { } 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) { // Unable to connect; close the socket and get out try { mmSocket.close(); } catch (IOException closeException) { } 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) { } } } 

i.e. ConnectThread (you may notice that they are really copied from the top link);

The problem is that when you try to connect, the connecting stream does return a socket (calls managesocket ()), but the receiving stream remains in socket = mmServerSocket.accept (), as if nothing would have happened; But something happens in AcceptThread, because when I initiate a connection with another device (with ConnectThread), the locator of the receiving device is updated; Sometimes a connection is actually created correctly, but only after closing a command, disconnecting → turning on bluetoothh, etc.

here is an interesting line in logcat (generated by calling .accept (), I suppose):

07-19 18: 04: 47.484: D / BLZ20_WRAPPER (3143): btlif_signal_event: ### event BTLIF_BTS_RFC_CON_IND does not match ###

So what could be the problem?

+4
source share

All Articles