I have a problem with connecting my client socket in the Android bluetooth API. I already read these posts: this one, this one, this one and this one According to these posts I made my code like the following but it still not work:
class MainActivity : AppCompatActivity()
{
private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
private var connectThread: ConnectThread? = null
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (bluetoothAdapter != null)
{
if (!bluetoothAdapter.isEnabled)
bluetoothAdapter.enable()
val devices = bluetoothAdapter.bondedDevices.toTypedArray()
if (devices.isNotEmpty())
{
connectThread = ConnectThread(devices[0])
connectThread!!.run()
}
}
else
Log.i("BluetoothTest", "No bluetooth supported")
}
override fun onDestroy()
{
super.onDestroy()
connectThread!!.cancel()
}
private inner class ConnectThread(device: BluetoothDevice) : Thread()
{
//private val socket = device.createRfcommSocketToServiceRecord(UUID.fromString ("00001101-0000-1000-8000-00805F9B34FB"))
private val socket = device.javaClass.getMethod("createRfcommSocket", (Int::class
.javaPrimitiveType)).invoke(device, 2) as BluetoothSocket
override fun run()
{
bluetoothAdapter.cancelDiscovery()
socket.connect()
}
fun cancel()
{
socket.close()
}
}
}
Here is the logs with the returned error that comes on socket.connect():
--------- beginning of crash
05-15 15:59:19.553 24176-24176/com.bluetoothtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bluetoothtest, PID: 24176
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bluetoothtest/com.bluetoothtest.MainActivity}: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:685)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:697)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:374)
at com.bluetoothtest.MainActivity.onCreate(MainActivity.kt:36)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
It seems that the problem appeared after Android 4.3 because of the port value of the socket I am on Android 7.1 and 8.1 Does someone knows how to make bluetooth work on Android ?