Connecting BluetoothServerSocket to BluetoothSocket using matching UUID



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am having trouble connecting to the client socket using Android's Bluetooth API. I have already read this post and all of the related posts mentioned within. I am using the same UUID for the client and the server. The server bluetoothSocket.connect() times out each time, and throws the exception
java.io.IOException: read failed, socket might closed or timeout, read ret: -1. I'm not sure that I'm doing wrong in this case. I've been reading the Android Bluetooth API documentation and Chat application code samples and what I'm trying to do is quite similar. I have tried unpairing and pairing again, to no avail.



Here is the client code, running as part of an application on a OnePlus A3003, Android 7.1.1:



public class BluetoothTrafficLightConnectThread extends Thread 
private BluetoothDevice bluetoothDevice;
private BluetoothSocket bluetoothSocket;

public BluetoothTrafficLightConnectThread(BluetoothDevice btDevice)
BluetoothSocket tmp = null;
bluetoothDevice = btDevice;
boolean temp = btDevice.fetchUuidsWithSdp();
UUID uuid = null;

if(temp)
uuid = btDevice.getUuids()[0].getUuid();


Log.i(tag, "SmartRide using UUID " + uuid);

try
//MY_UUID is the app's UUID string, also used by the server code
tmp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);

catch(IOException e)
Log.i(tag, "Get socket failed in BluetoothTrafficLightConnectThread");


bluetoothSocket = tmp;


public void run()

try
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
bluetoothSocket.connect();
Log.i(tag, "TrafficLightConnect succeeded.");

catch (IOException connectException)
Log.i(tag, "TrafficLightConnect failed.n" + connectException.toString());

try
bluetoothSocket.close();

catch (IOException closeException)
Log.i(tag, "Failed to close socket after connection unsuccessful." + closeException);

return;


// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT_TRAFFIC_LIGHT, bluetoothSocket).sendToTarget();


public void cancel()
try
bluetoothSocket.close();

catch (IOException e)




And the server code, running on a Samsung J5, Android version 8.1.0:



private class AcceptThread extends Thread 
// The local server socket - listens for incoming requests
private final BluetoothServerSocket mmServerSocket;
private String mSocketType;

public AcceptThread(boolean secure)
BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";

Log.d(TAG, "AcceptThread constructor");

// Create a new listening server socket
try
if(secure)
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID);

Log.d(TAG, "Have a secure rfcomm socket. " + tmp.toString());
Log.d(TAG, "TrafficLight using UUID " + MY_UUID.toString());

else
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, MY_UUID);
Log.d(TAG, "Have an insecure rfcomm socket." + tmp.toString());


catch (IOException e)
Log.e(TAG, "Socket type: " + mSocketType + "listen() failed", e);


mmServerSocket = tmp;
mState = STATE_LISTEN;

updateUserInterfaceTitle();


@Override
public void run()
Log.d(TAG, "Begin acceptThread");
setName("AcceptThread" + mSocketType);

BluetoothSocket socket = null;

while(mState != STATE_CONNECTED)
try
// This is a blocking call and will only return on a
// successful connection or an exception
Log.d(TAG, "Trying to connect to the remote device.");
socket = mmServerSocket.accept();
mmServerSocket.close();
Log.d(TAG, "Accepted the incoming connection request.");
catch(IOException e)
Log.e(TAG, "Socket Type: " + mSocketType + ", .accept() failed", e);
break;


// Connection was accepted
if(socket != null)
synchronized(BluetoothTLService.this)
switch(mState)
case STATE_LISTEN:
Log.d(TAG, "Connection established.");
// Can terminate the AcceptThread as we are now connected
// Should notify others that we are ready to send data now
mIsConnected = true;
setUpConnection(socket, socket.getRemoteDevice(), mSocketType);
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket
try
socket.close();
Log.d(TAG, "Closing socket.");
catch(IOException e)
Log.e(TAG, "Could not close unwanted socket", e);

break;





Log.d(TAG, "END mAcceptThread, socket Type: " + mSocketType);


public void cancel()
Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
try
mmServerSocket.close();
catch (IOException e)
Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);












share|improve this question




























    0















    I am having trouble connecting to the client socket using Android's Bluetooth API. I have already read this post and all of the related posts mentioned within. I am using the same UUID for the client and the server. The server bluetoothSocket.connect() times out each time, and throws the exception
    java.io.IOException: read failed, socket might closed or timeout, read ret: -1. I'm not sure that I'm doing wrong in this case. I've been reading the Android Bluetooth API documentation and Chat application code samples and what I'm trying to do is quite similar. I have tried unpairing and pairing again, to no avail.



    Here is the client code, running as part of an application on a OnePlus A3003, Android 7.1.1:



    public class BluetoothTrafficLightConnectThread extends Thread 
    private BluetoothDevice bluetoothDevice;
    private BluetoothSocket bluetoothSocket;

    public BluetoothTrafficLightConnectThread(BluetoothDevice btDevice)
    BluetoothSocket tmp = null;
    bluetoothDevice = btDevice;
    boolean temp = btDevice.fetchUuidsWithSdp();
    UUID uuid = null;

    if(temp)
    uuid = btDevice.getUuids()[0].getUuid();


    Log.i(tag, "SmartRide using UUID " + uuid);

    try
    //MY_UUID is the app's UUID string, also used by the server code
    tmp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);

    catch(IOException e)
    Log.i(tag, "Get socket failed in BluetoothTrafficLightConnectThread");


    bluetoothSocket = tmp;


    public void run()

    try
    // Connect the device through the socket. This will block
    // until it succeeds or throws an exception
    bluetoothSocket.connect();
    Log.i(tag, "TrafficLightConnect succeeded.");

    catch (IOException connectException)
    Log.i(tag, "TrafficLightConnect failed.n" + connectException.toString());

    try
    bluetoothSocket.close();

    catch (IOException closeException)
    Log.i(tag, "Failed to close socket after connection unsuccessful." + closeException);

    return;


    // Do work to manage the connection (in a separate thread)
    mHandler.obtainMessage(SUCCESS_CONNECT_TRAFFIC_LIGHT, bluetoothSocket).sendToTarget();


    public void cancel()
    try
    bluetoothSocket.close();

    catch (IOException e)




    And the server code, running on a Samsung J5, Android version 8.1.0:



    private class AcceptThread extends Thread 
    // The local server socket - listens for incoming requests
    private final BluetoothServerSocket mmServerSocket;
    private String mSocketType;

    public AcceptThread(boolean secure)
    BluetoothServerSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    Log.d(TAG, "AcceptThread constructor");

    // Create a new listening server socket
    try
    if(secure)
    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID);

    Log.d(TAG, "Have a secure rfcomm socket. " + tmp.toString());
    Log.d(TAG, "TrafficLight using UUID " + MY_UUID.toString());

    else
    tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, MY_UUID);
    Log.d(TAG, "Have an insecure rfcomm socket." + tmp.toString());


    catch (IOException e)
    Log.e(TAG, "Socket type: " + mSocketType + "listen() failed", e);


    mmServerSocket = tmp;
    mState = STATE_LISTEN;

    updateUserInterfaceTitle();


    @Override
    public void run()
    Log.d(TAG, "Begin acceptThread");
    setName("AcceptThread" + mSocketType);

    BluetoothSocket socket = null;

    while(mState != STATE_CONNECTED)
    try
    // This is a blocking call and will only return on a
    // successful connection or an exception
    Log.d(TAG, "Trying to connect to the remote device.");
    socket = mmServerSocket.accept();
    mmServerSocket.close();
    Log.d(TAG, "Accepted the incoming connection request.");
    catch(IOException e)
    Log.e(TAG, "Socket Type: " + mSocketType + ", .accept() failed", e);
    break;


    // Connection was accepted
    if(socket != null)
    synchronized(BluetoothTLService.this)
    switch(mState)
    case STATE_LISTEN:
    Log.d(TAG, "Connection established.");
    // Can terminate the AcceptThread as we are now connected
    // Should notify others that we are ready to send data now
    mIsConnected = true;
    setUpConnection(socket, socket.getRemoteDevice(), mSocketType);
    break;
    case STATE_NONE:
    case STATE_CONNECTED:
    // Either not ready or already connected. Terminate new socket
    try
    socket.close();
    Log.d(TAG, "Closing socket.");
    catch(IOException e)
    Log.e(TAG, "Could not close unwanted socket", e);

    break;





    Log.d(TAG, "END mAcceptThread, socket Type: " + mSocketType);


    public void cancel()
    Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
    try
    mmServerSocket.close();
    catch (IOException e)
    Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);












    share|improve this question
























      0












      0








      0








      I am having trouble connecting to the client socket using Android's Bluetooth API. I have already read this post and all of the related posts mentioned within. I am using the same UUID for the client and the server. The server bluetoothSocket.connect() times out each time, and throws the exception
      java.io.IOException: read failed, socket might closed or timeout, read ret: -1. I'm not sure that I'm doing wrong in this case. I've been reading the Android Bluetooth API documentation and Chat application code samples and what I'm trying to do is quite similar. I have tried unpairing and pairing again, to no avail.



      Here is the client code, running as part of an application on a OnePlus A3003, Android 7.1.1:



      public class BluetoothTrafficLightConnectThread extends Thread 
      private BluetoothDevice bluetoothDevice;
      private BluetoothSocket bluetoothSocket;

      public BluetoothTrafficLightConnectThread(BluetoothDevice btDevice)
      BluetoothSocket tmp = null;
      bluetoothDevice = btDevice;
      boolean temp = btDevice.fetchUuidsWithSdp();
      UUID uuid = null;

      if(temp)
      uuid = btDevice.getUuids()[0].getUuid();


      Log.i(tag, "SmartRide using UUID " + uuid);

      try
      //MY_UUID is the app's UUID string, also used by the server code
      tmp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);

      catch(IOException e)
      Log.i(tag, "Get socket failed in BluetoothTrafficLightConnectThread");


      bluetoothSocket = tmp;


      public void run()

      try
      // Connect the device through the socket. This will block
      // until it succeeds or throws an exception
      bluetoothSocket.connect();
      Log.i(tag, "TrafficLightConnect succeeded.");

      catch (IOException connectException)
      Log.i(tag, "TrafficLightConnect failed.n" + connectException.toString());

      try
      bluetoothSocket.close();

      catch (IOException closeException)
      Log.i(tag, "Failed to close socket after connection unsuccessful." + closeException);

      return;


      // Do work to manage the connection (in a separate thread)
      mHandler.obtainMessage(SUCCESS_CONNECT_TRAFFIC_LIGHT, bluetoothSocket).sendToTarget();


      public void cancel()
      try
      bluetoothSocket.close();

      catch (IOException e)




      And the server code, running on a Samsung J5, Android version 8.1.0:



      private class AcceptThread extends Thread 
      // The local server socket - listens for incoming requests
      private final BluetoothServerSocket mmServerSocket;
      private String mSocketType;

      public AcceptThread(boolean secure)
      BluetoothServerSocket tmp = null;
      mSocketType = secure ? "Secure" : "Insecure";

      Log.d(TAG, "AcceptThread constructor");

      // Create a new listening server socket
      try
      if(secure)
      tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID);

      Log.d(TAG, "Have a secure rfcomm socket. " + tmp.toString());
      Log.d(TAG, "TrafficLight using UUID " + MY_UUID.toString());

      else
      tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, MY_UUID);
      Log.d(TAG, "Have an insecure rfcomm socket." + tmp.toString());


      catch (IOException e)
      Log.e(TAG, "Socket type: " + mSocketType + "listen() failed", e);


      mmServerSocket = tmp;
      mState = STATE_LISTEN;

      updateUserInterfaceTitle();


      @Override
      public void run()
      Log.d(TAG, "Begin acceptThread");
      setName("AcceptThread" + mSocketType);

      BluetoothSocket socket = null;

      while(mState != STATE_CONNECTED)
      try
      // This is a blocking call and will only return on a
      // successful connection or an exception
      Log.d(TAG, "Trying to connect to the remote device.");
      socket = mmServerSocket.accept();
      mmServerSocket.close();
      Log.d(TAG, "Accepted the incoming connection request.");
      catch(IOException e)
      Log.e(TAG, "Socket Type: " + mSocketType + ", .accept() failed", e);
      break;


      // Connection was accepted
      if(socket != null)
      synchronized(BluetoothTLService.this)
      switch(mState)
      case STATE_LISTEN:
      Log.d(TAG, "Connection established.");
      // Can terminate the AcceptThread as we are now connected
      // Should notify others that we are ready to send data now
      mIsConnected = true;
      setUpConnection(socket, socket.getRemoteDevice(), mSocketType);
      break;
      case STATE_NONE:
      case STATE_CONNECTED:
      // Either not ready or already connected. Terminate new socket
      try
      socket.close();
      Log.d(TAG, "Closing socket.");
      catch(IOException e)
      Log.e(TAG, "Could not close unwanted socket", e);

      break;





      Log.d(TAG, "END mAcceptThread, socket Type: " + mSocketType);


      public void cancel()
      Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
      try
      mmServerSocket.close();
      catch (IOException e)
      Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);












      share|improve this question














      I am having trouble connecting to the client socket using Android's Bluetooth API. I have already read this post and all of the related posts mentioned within. I am using the same UUID for the client and the server. The server bluetoothSocket.connect() times out each time, and throws the exception
      java.io.IOException: read failed, socket might closed or timeout, read ret: -1. I'm not sure that I'm doing wrong in this case. I've been reading the Android Bluetooth API documentation and Chat application code samples and what I'm trying to do is quite similar. I have tried unpairing and pairing again, to no avail.



      Here is the client code, running as part of an application on a OnePlus A3003, Android 7.1.1:



      public class BluetoothTrafficLightConnectThread extends Thread 
      private BluetoothDevice bluetoothDevice;
      private BluetoothSocket bluetoothSocket;

      public BluetoothTrafficLightConnectThread(BluetoothDevice btDevice)
      BluetoothSocket tmp = null;
      bluetoothDevice = btDevice;
      boolean temp = btDevice.fetchUuidsWithSdp();
      UUID uuid = null;

      if(temp)
      uuid = btDevice.getUuids()[0].getUuid();


      Log.i(tag, "SmartRide using UUID " + uuid);

      try
      //MY_UUID is the app's UUID string, also used by the server code
      tmp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);

      catch(IOException e)
      Log.i(tag, "Get socket failed in BluetoothTrafficLightConnectThread");


      bluetoothSocket = tmp;


      public void run()

      try
      // Connect the device through the socket. This will block
      // until it succeeds or throws an exception
      bluetoothSocket.connect();
      Log.i(tag, "TrafficLightConnect succeeded.");

      catch (IOException connectException)
      Log.i(tag, "TrafficLightConnect failed.n" + connectException.toString());

      try
      bluetoothSocket.close();

      catch (IOException closeException)
      Log.i(tag, "Failed to close socket after connection unsuccessful." + closeException);

      return;


      // Do work to manage the connection (in a separate thread)
      mHandler.obtainMessage(SUCCESS_CONNECT_TRAFFIC_LIGHT, bluetoothSocket).sendToTarget();


      public void cancel()
      try
      bluetoothSocket.close();

      catch (IOException e)




      And the server code, running on a Samsung J5, Android version 8.1.0:



      private class AcceptThread extends Thread 
      // The local server socket - listens for incoming requests
      private final BluetoothServerSocket mmServerSocket;
      private String mSocketType;

      public AcceptThread(boolean secure)
      BluetoothServerSocket tmp = null;
      mSocketType = secure ? "Secure" : "Insecure";

      Log.d(TAG, "AcceptThread constructor");

      // Create a new listening server socket
      try
      if(secure)
      tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID);

      Log.d(TAG, "Have a secure rfcomm socket. " + tmp.toString());
      Log.d(TAG, "TrafficLight using UUID " + MY_UUID.toString());

      else
      tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, MY_UUID);
      Log.d(TAG, "Have an insecure rfcomm socket." + tmp.toString());


      catch (IOException e)
      Log.e(TAG, "Socket type: " + mSocketType + "listen() failed", e);


      mmServerSocket = tmp;
      mState = STATE_LISTEN;

      updateUserInterfaceTitle();


      @Override
      public void run()
      Log.d(TAG, "Begin acceptThread");
      setName("AcceptThread" + mSocketType);

      BluetoothSocket socket = null;

      while(mState != STATE_CONNECTED)
      try
      // This is a blocking call and will only return on a
      // successful connection or an exception
      Log.d(TAG, "Trying to connect to the remote device.");
      socket = mmServerSocket.accept();
      mmServerSocket.close();
      Log.d(TAG, "Accepted the incoming connection request.");
      catch(IOException e)
      Log.e(TAG, "Socket Type: " + mSocketType + ", .accept() failed", e);
      break;


      // Connection was accepted
      if(socket != null)
      synchronized(BluetoothTLService.this)
      switch(mState)
      case STATE_LISTEN:
      Log.d(TAG, "Connection established.");
      // Can terminate the AcceptThread as we are now connected
      // Should notify others that we are ready to send data now
      mIsConnected = true;
      setUpConnection(socket, socket.getRemoteDevice(), mSocketType);
      break;
      case STATE_NONE:
      case STATE_CONNECTED:
      // Either not ready or already connected. Terminate new socket
      try
      socket.close();
      Log.d(TAG, "Closing socket.");
      catch(IOException e)
      Log.e(TAG, "Could not close unwanted socket", e);

      break;





      Log.d(TAG, "END mAcceptThread, socket Type: " + mSocketType);


      public void cancel()
      Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
      try
      mmServerSocket.close();
      catch (IOException e)
      Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);









      java android bluetooth






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 13:42









      and_mannand_mann

      12




      12






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320802%2fconnecting-bluetoothserversocket-to-bluetoothsocket-using-matching-uuid%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320802%2fconnecting-bluetoothserversocket-to-bluetoothsocket-using-matching-uuid%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20