I'm doing some stuff with bluetooth on android and I would like to connect to one of the discovered devices and open a socket connection towards it.
I've granted all of the needed permissions: Bluetooth, Bluetooth_Admin, Access_Fine_Location and Access_Coarse_Location and ask for them before I do anything with bluetooth.
Now, I've discovered some devices with adapter.startDiscovery(); and activity.registerReceiver(receiver, filter);
In the receiver finds a device of a certain name, I try connecting to it like this:
adapter.cancelDiscovery(); Log.d(TAG, "Create Bond"); device.createBond(); try { socket = device.createRfcommSocketToServiceRecord(uuid); Log.d(TAG, "Sleep 10"); sleep(10000); Log.d(TAG, "Create Socket"); //socket = device.createInsecureRfcommSocketToServiceRecord(uuid); Log.d(TAG, "Connect socket"); socket.connect(); Log.d(TAG, "Connecting Done"); } catch (Exception e) { Log.d(TAG, "Failed to connect to device", e); try { socket.close(); } catch (Exception e2) { Log.d(TAG, "Failed to close socket", e2); } } This is a test code with which I'm trying to create a socket and open a connection.
I get the following Exception on .connect():
java.io.IOException: read failed, socket might closed or timeout, read ret: -1 at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:684) at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:696) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:373)
What am I doing wrong.
The bluetooth device I connect to is a Android mobile device, but I plan on using others when I manage to get the connection.
Update1: Android version is 7.0