Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java IO exception while using the library in kotlin #18

Open
shashankRanaNW opened this issue Aug 9, 2022 · 5 comments
Open

Java IO exception while using the library in kotlin #18

shashankRanaNW opened this issue Aug 9, 2022 · 5 comments

Comments

@shashankRanaNW
Copy link

shashankRanaNW commented Aug 9, 2022

Hello. I used the library in my application which is written in Kotlin. I translated the code into kotlin and did some fixes but I am getting
java.io.IOException: read failed, socket might closed or timeout, read ret: -1

again and again. This post from stackEx() deals with this but I guess that deals with a different problem.

I am afixing my mainActivity which is just the java code translated into kotlin. To be precise onError is giving the error which is called onConnect
Thanks


package com.bottlerunner.bluetoothlibrabrytesting

import android.Manifest
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.harrysoft.androidbluetoothserial.BluetoothManager
import com.harrysoft.androidbluetoothserial.BluetoothSerialDevice
import com.harrysoft.androidbluetoothserial.SimpleBluetoothDeviceInterface
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import javax.xml.transform.ErrorListener


class MainActivity : AppCompatActivity() {

    val bluetoothManager: BluetoothManager = BluetoothManager.getInstance() //Harry's bluetooth manager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        connectDevice("FC:AA:B6:AA:AC:22")

        val pairedDevices: Collection<BluetoothDevice> = bluetoothManager.pairedDevicesList
        for (device in pairedDevices) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.BLUETOOTH_CONNECT
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                Toast.makeText(this,"permission denied",Toast.LENGTH_SHORT).show()

            }

            Log.d("My Bluetooth App", "Device name: " + device.name)
            Log.d("My Bluetooth App", "Device MAC Address: " + device.address)
            Log.d("Debugg",device.bondState.toString())
        }


        val btn = findViewById<Button>(R.id.button)
        btn.setOnClickListener {
            connectDevice("FC:AA:B6:AA:AC:22")          //Tab
            ActivityCompat.requestPermissions(this, arrayOf( Manifest.permission.BLUETOOTH),0  )
            Log.d("Permission","Reached end of btn")
        }

    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == 1) {
            // Request for camera permission.
            if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission has been granted. Start camera preview Activity.
                Log.d("Permission","Reached onRequestPermissionResult")
                ActivityCompat.requestPermissions(this, arrayOf( Manifest.permission.BLUETOOTH),0  )
                Toast.makeText(this,"Permission granted",Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this,grantResults.toString(),Toast.LENGTH_SHORT).show()}
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }


    var onMessageSentListener = SimpleBluetoothDeviceInterface.OnMessageSentListener{
        Toast.makeText(this, "Sent a message! Message was: " + it, Toast.LENGTH_LONG).show() // Replace context with your context instance.
    }

    var onMessageReveivedListener = SimpleBluetoothDeviceInterface.OnMessageReceivedListener{
        // We received a message! Handle it here.
        Toast.makeText(this, "Received a message! Message was: " + it, Toast.LENGTH_LONG).show(); // Replace context with your context instance.
    }

    var errorListener = SimpleBluetoothDeviceInterface.OnErrorListener{
        Toast.makeText(this,"Durr phite muh, error" + it.message, Toast.LENGTH_SHORT).show()
    }

    lateinit var deviceInterface: SimpleBluetoothDeviceInterface

    var onConnected = Consumer<BluetoothSerialDevice>{
        // You are now connected to this device!
        // Here you may want to retain an instance to your device:
        deviceInterface = it.toSimpleDeviceInterface()

        // Listen to bluetooth events
        deviceInterface.setListeners(onMessageReveivedListener, onMessageSentListener, errorListener)

        // Let's send a message:
        deviceInterface.sendMessage("Hello world!")
    }

    var onError = Consumer<Throwable> {
        Toast.makeText(this, "How lovely error" +it.message,Toast.LENGTH_SHORT).show()
        Log.d("onError" ,it.message + "\n"+ it.cause)

    }

    fun connectDevice(mac:String) {
        bluetoothManager.openSerialDevice(mac)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(onConnected, onError)
        Log.d("Debugg","reached 114")
    }

}
@mignacio
Copy link

Hi @shashankRanaNoidaWala where you able to solve this issue? I'm running into the same and I'm trying to know if the stack exchange that you posted helped you solving it or not. Thanks!

@shashankRanaNW
Copy link
Author

No it didn't help, in the end I used Android Bluetooth Standard Library, which is more fragmented and cumbersome but did the job.

@mignacio
Copy link

Thank you. Was the device you were trying to connect a Bluetooth Low-Energy or Bluetooth Classic? I read in another issue that this library does not support BLE devices.

@shashankRanaNW
Copy link
Author

Bluetooth classic HC-05.

@mcanyucel
Copy link

The issue still persists. sad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants