The UUID you try to use is the Serial Port Profile UUID. It could be declared in code like
private static final UUID BLUETOOTH_SPP_UUID =
I tried your code with a Windows 8 laptop and a Nexus S. First I had to pair the devices from the Nexus. Then I started the server and used the Bluetooth SPP app to test the connection. It worked flawlessly. Then I started your code and it worked as well and it worked too.
I could reproduce the "Service discovery failed" exception when the server side was not started. Also, after I scanned all 30 channels with the createRfcommSocket method, the bluetooth stack on the phone got corrupted and I had to restart the device.
on the laptop make sure that the bluetooth is in discoverable state
(optional) turn off UAC and use an admin account to make sure you don't encounter any security bugs (I didn't have to do this but on the second start the firewall had to be opened for eclipse to enable debugging)
use the SPP UUID, not a custom one
test the connection with a third party app from the play store. I used this one in command line mode. Only the connection needs to be tested.
update the MAC address and run the app
Here is the output I got from the run:
Waiting for incoming connection...
Exception Occured: java.io.EOFException
BlueCove stack shutdown completed
at java.io.DataInputStream.readChar(Unknown Source)
For the sake of completeness, here is the error that is returned under various conditions
javax.bluetooth.BluetoothStateException: Bluetooth not detected
java.io.IOException: Unable to start Service Discovery
java.io.IOException: Service discovery failed
Theoretically pairing should happen automatically when a secure connection is initiated but in reality the pairing dialog may not appear in all cases on the phone or in Windows. This is a factor of uncertainty that needs to be considered if failures happen.
Thanks. But when I try the option 2 : the socket.connect() fail with a "connection timeout". Additionally, I'm not sure that the uuid of my server is not in the repository since when I call bluetoothDevice.getUuids() on the client I receive an array with the SPP-UUID + my custom UUID (if I define one randomly generated)
What version of android do you use?
Ok, I'll try to confirm again that this works. I have the same version on my device.
@ben75 I tested the whole scenario and updated the post. Btw: getUuids() for me returns the 0x1000, 0x1101, 0x1115, 0x1200 services in this order.