I am trying to connect to an RN4870 BLE module using my Kotlin app, but keep getting an error when trying to connect a socket. I am able to pair my phone and the BLE module, and verify in kotlin they they are bonded correctly, but when I try to run these two lines.
btSocket = RN4780Device.createInsecureRfcommSocketToServiceRecord(RN4870_UUID)
btSocket.connect()
I always get this error: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
Note: I am able to send and receive data using another app (SmartData iOS app) so I know the Bluetooth FW is working correctly.
After doing a lot of research, I can only assume that there is an issue with my UUID. I'm not sure if I should use the generic one I am now, or somehow get a UUID that is being used by the RN4870 module.
Main Activity:
class MainActivity : AppCompatActivity() {
lateinit var btAdapter: BluetoothAdapter
lateinit var RN4780Device: BluetoothDevice
lateinit var btSocket: BluetoothSocket
lateinit var btService: MyBluetoothService
@SuppressLint("MissingPermission")
@RequiresApi(Build.VERSION_CODES.S)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize bluetooth adapter
val btManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
btAdapter = btManager.adapter
// Start home activity if connected to Bluetooth module
btn_cont.setOnClickListener {
if (checkPaired()) {
// Connect client socket
try {
btSocket = RN4780Device.createInsecureRfcommSocketToServiceRecord(RN4870_UUID)
btSocket.connect()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
@SuppressLint("MissingPermission")
private fun checkPaired() : Boolean {
// Find RN4780 device and check bond state
val device: BluetoothDevice = btAdapter.getRemoteDevice(RN4780_ADDRESS)
if (device.bondState != BluetoothDevice.BOND_BONDED) {
Toast.makeText(this, "RN4870 BLE Module is not Connected", Toast.LENGTH_LONG).show()
return false
}
RN4780Device = device
return true
}
}
Constants
object Constants {
var RN4780_ADDRESS = "04:91:62:A1:7D:0D"
var RN4870_UUID = UUID.fromString("49535343-8841-43F4-A8D4-ECBE34729BB3")
}
Full StackTrace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.geoboard, PID: 1294
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:758)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:772)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:404)
at com.example.geoboard.MainActivity.onCreate$lambda-0(MainActivity.kt:54)
at com.example.geoboard.MainActivity.$r8$lambda$z_JWuogCboqpr2BmqKXIU9Cwb9g(Unknown Source:0)
at com.example.geoboard.MainActivity$$ExternalSyntheticLambda2.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7471)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
at android.view.View.performClickInternal(View.java:7448)
at android.view.View.access$3600(View.java:813)
at android.view.View$PerformClick.run(View.java:28408)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:250)
at android.app.ActivityThread.main(ActivityThread.java:7755)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
I have been looking at different stack overflow posts for hours and was unable to find a solution. Thanks for any help!
Turns out that you can't connect to Low Energy Bluetooth with a socket. Instead, see https://developer.android.com/guide/topics/connectivity/bluetooth/connect-gatt-server