Use createRfcommSocketToServiceRecord instead of createRfcommSocket
createRfcommSocketToServiceRecord accepts the UUID that you are transmitting and uses SDP to determine which radio channel is used for the connection. It also verifies that the server is listening on the remote endpoint, with the same UUID. Thus, this is the most reliable way to get a connection: it will always use the correct channel, and if the connection is successful, you know that something on the other end can understand your protocol.
In contrast, createRfcommSocket just connects to the channel you tell it to. There is no way to find out if something is listening on the remote endpoint: you know what the device is. In addition, your choice of radio channel may be completely inappropriate. This is why this function is not published in the API, and another function is preferable.
createRfcommSocket may seem more reliable at first glance, but this is because it does not check for a listener on another endpoint: it ignores some cases of errors. This may be good for experimentation, but it is useless for a production system, because often the user will forget to start the server on a different endpoint, and your application will not get confused.
Of course, since createRfcommSocket not published in the API, you have no guarantee that it will continue to work in general in future releases of Android.
madhu131313 May 04 '15 at 11:07 2015-05-04 11:07
source share