I cannot connect to my HC-05 bluetooth module using my own Java app

22 hours ago 3
ARTICLE AD BOX

I built a Java app where I implemented bluetooth functionality. Using the documentation I managed to discover devices and pair with them. I managed to get the whole device info (address, name), but it fails when I want to establish communication with the module.

In the ConnectThread constructor:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { try { tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { Log.e(TAG, "Socket's create() method failed", e); } } else { Log.e(TAG, "Missing BLUETOOTH_CONNECT permission"); } targetSocket = tmp;

In the ConnectThread method run():

bluetoothAdapter.cancelDiscovery(); try { targetSocket.connect(); Log.i(TAG, "Connection successful!"); } catch (IOException e) { Log.d(TAG, Log.getStackTraceString(e)); Log.e(TAG, "Could not connect; closing socket", e); try { targetSocket.close(); } catch (IOException e2) { Log.e(TAG, "Could not close the client socket", e2); } }

In the MainActivity, when I choose a device from the paired devices list:

ConnectThread connectThread = new ConnectThread(device, mBluetoothManager, MainActivity.this); connectThread.start();

**
What I have in my LOGCAT**

ConnectThread com.example.carcontroller D java.io.IOException: read failed, socket might closed or timeout, read ret: -1 at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1170) at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:1188) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:566) at com.example.carcontroller.ConnectThread.run(ConnectThread.java:50) ConnectThread com.example.carcontroller E Could not connect; closing socket java.io.IOException: read failed, socket might closed or timeout, read ret: -1 at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1170) at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:1188) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:566) at com.example.carcontroller.ConnectThread.run(ConnectThread.java:50)

Using the Serial Bluetooth Terminal from google play I managed to connect to my module and send data so I figure that the problem is on my end but I can't find any information about why this happens.

Read Entire Article