Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection refused while attempting to connect Bluetooth device
    primarykey
    data
    text
    <p>I'm using BluetoothChat example(3.1) code sample to test communications between two Samsung Galaxy Tabs. I know this has been asked many times before, but I'm still confused that when I do this:</p> <pre><code>BluetoothSocket mmSocket ... mmSocket.connect(); </code></pre> <p>This produces a connection refused. I've tried many times to pair and unpair outside and inside the program without any results. Does anyone have any ideas?</p> <p><strong>Update (Added from Answer):</strong></p> <p>The code in both client &amp; server is the same. These are actually two Samsung Galaxy Tabs (3.1). The error occurs while trying to connect to remote device.</p> <pre><code> public ConnectThread(BluetoothDevice device, boolean secure) { mmDevice = device; BluetoothSocket tmp = null; mSocketType = secure ? "Secure" : "Insecure"; // Get a BluetoothSocket for a connection with the // given BluetoothDevice try { if (secure) { //tmp = device.createRfcommSocketToServiceRecord( //MY_UUID_SECURE); // tmp = device.createRfcommSocketToServiceRecord( // UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); tmp = (BluetoothSocket) m.invoke(device, 1); } else { Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] {int.class}); tmp = (BluetoothSocket)m.invoke(device, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } } catch (Exception e) { Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); } mmSocket = tmp; } public void run() { Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType); setName("ConnectThread" + mSocketType); // Always cancel discovery because it will slow down a connection mAdapter.cancelDiscovery(); // Make a connection to the BluetoothSocket try { // This is a blocking call and will only return on a // successful connection or an exception mmSocket.connect(); } catch (IOException e) { // Close the socket try { Log.e("Ali", "Error interacting with remote device. Here is the cause: "+ e.getMessage() ); mmSocket.close(); } catch (IOException e2) { Log.e(TAG, "unable to close() " + mSocketType + " socket during connection failure", e2); } connectionFailed(); return; } // Reset the ConnectThread because we're done synchronized (BluetoothChatService.this) { mConnectThread = null; } // Start the connected thread connected(mmSocket, mmDevice, mSocketType); } </code></pre> <p>The listening part is as follows:</p> <pre><code> private class AcceptThread extends Thread { // The local server socket private final BluetoothServerSocket mmServerSocket; private String mSocketType; public AcceptThread(boolean secure) { BluetoothServerSocket tmp = null; mSocketType = secure ? "Secure":"Insecure"; // Create a new listening server socket try { if (secure) { tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } else { tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord( NAME_INSECURE, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } } catch (IOException e) { Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e); } mmServerSocket = tmp; } public void run() { if (D) Log.d(TAG, "Socket Type: " + mSocketType + "BEGIN mAcceptThread" + this); setName("AcceptThread" + mSocketType); 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 socket = mmServerSocket.accept(); } catch (IOException e) { Log.e("Ali", "Error while connecting to device because: "+ e.getStackTrace()); break; } // If a connection was accepted if (socket != null) { synchronized (BluetoothChatService.this) { switch (mState) { case STATE_LISTEN: case STATE_CONNECTING: // Situation normal. Start the connected thread. connected(socket, socket.getRemoteDevice(), mSocketType); 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, socket Type: " + mSocketType); } </code></pre> <p>Finally here you have the logcat:</p> <blockquote> <p></p> </blockquote> <ul> <li>01-05 10:34:07.749: E/BluetoothChat(22495): +++ ON CREATE +++ 01-05</li> <li>10:34:07.779: E/BluetoothChat(22495): ++ ON START ++ 01-05</li> <li>10:37:41.109: E/BluetoothChat(22599): ++ ON START ++ 01-05</li> <li>10:37:50.009: D/BluetoothChat(22599): onActivityResult -1 01-05</li> <li>10:37:50.009: D/BluetoothChatService(22599): connect to:</li> <li>60:D0:A9:85:AE:6A 01-05 10:37:50.009: D/BluetoothChatService(22599):</li> <li>setState() 1 -> 2 01-05 10:37:50.009: E/BluetoothChat(22599): + ON</li> <li>RESUME + 01-05 10:37:50.029: I/BluetoothChatService(22599): BEGIN</li> <li>mConnectThread SocketType:Secure 01-05 10:37:50.029:</li> <li>I/BluetoothChat(22599): MESSAGE_STATE_CHANGE: 2 01-05 10:37:55.169:</li> <li>E/Ali(22599): Error interacting with remote device. Here is the cause: Host is down 01-05 10:37:55.169:</li> <li>D/BluetoothChatService(22599): start 01-05 10:37:55.169:</li> <li>D/BluetoothChatService(22599): setState() 2 -> 1 01-05 10:37:55.189:</li> <li>I/BluetoothChat(22599): MESSAGE_STATE_CHANGE: 1 01-05 10:37:55.219:</li> <li>D/dalvikvm(22599): GC_CONCURRENT freed 171K, 4% free 6522K/6791K,paused 2ms+3ms 01-05 10:38:02.749:</li> <li>W/PhoneWindow(22599): Couldn't get audio manager 01-05 10:38:04.969:</li> <li>E/BluetoothChat(22599): - ON PAUSE - 01-05 10:38:04.989:</li> <li>I/ApplicationPackageManager(22599): cscCountry is not German : TPH 01-05 10:38:07.059:</li> <li>D/DeviceListActivity(22599): doDiscovery() 01-05 10:38:10.609:</li> <li>D/BluetoothChat(22599):onActivityResult -1 01-05 10:38:10.609:</li> <li>D/BluetoothChatService(22599): connect to: F0:08:F1:5E:51:67 01-05 10:38:10.609:</li> <li>D/BluetoothChatService(22599): setState() 1 -> 2 01-05 10:38:10.609:</li> <li>E/BluetoothChat(22599): + ON RESUME + 01-05 10:38:10.609:</li> <li>I/BluetoothChatService(22599): BEGIN mConnectThread SocketType:Secure 01-05 10:38:10.629:</li> <li>I/BluetoothChat(22599): MESSAGE_STATE_CHANGE: 2 01-05 10:38:12.089:</li> <li>E/BluetoothChat(22599): - ON PAUSE - 01-05 10:38:12.329:</li> <li>D/CLIPBOARD(22599): Hide Clipboard dialog at Starting input: finished by someone else... ! 01-05 10:38:12.339:</li> <li>W/IInputConnectionWrapper(22599): showStatusIcon on inactive InputConnection 01-05 10:38:19.589:</li> <li>E/BluetoothChat(22599):+ ON RESUME + 01-05 10:38:20.799:</li> <li><em><strong>E/Ali(22599): Error interacting with remote device. Here is the cause: Connection refused 01-05 10:38:20.799:</em></strong></li> <li>D/BluetoothChatService(22599): start 01-05 10:38:20.799:</li> <li>D/BluetoothChatService(22599): setState() 2 -> 1 01-05 10:38:20.799:</li> <li>I/BluetoothChat(22599): MESSAGE_STATE_CHANGE: 1 01-05 10:39:23.489: </li> <li>E/BluetoothChat(22599): - ON PAUSE -</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload