CB Midkiff
CB Midkiff

Reputation: 195

Handling incoming bluetooth data stream in Kotlin Android app

I am working on a small app that connects via bluetooth to an Arduino with a bluetooth shield attached. My bluetooth connection is fine and I'm able to send commands from my app to the Arduino. I'm doing this in Kotlin. I'm learning as I go, so I'm misunderstanding something. Which is where I hope someone can point me in the right direction.

You can assume that all the bluetooth connection stuff is working fine(it is).

This is the part of my code that handles the sending of data to the Arduino.

private fun writeDataSendToMothership(outputToBt: String) {
    try {
        bluetoothSocket.outputStream.write(outputToBt.toByteArray())
        Log.i(LOGTAG, "Button clicked, info sent: $outputToBt")
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

button_led_on.setOnClickListener { writeDataSendToMothership("1")}
button_led_off.setOnClickListener { writeDataSendToMothership("0")}

The part i'm having trouble with is receiving data from the Arduino(Mothership) and doing something with it. I cannot figure out what I need to do.

What I am trying to do is show an image in the app depending on what the Arduino sends after a button on the Arduino is pushed.

What I have so far is:

private fun readDataFromMothership(inputFromBt: String) {
    try {
        bluetoothSocket.inputStream.read(inputFromBt.toByteArray())
        Log.i(LOGTAG, "Incoming data from Mothership recieved: $inputFromBt")
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

private fun View.showOrInvisible(imageShow: Boolean) {
    visibility = if (imageShow) {
        View.VISIBLE
    } else {
        View.INVISIBLE
    }
}

This is where I fall flat.

if (readDataFromMothership()) {
        imageView_mothership_button_pushed.showOrInvisible(true)
    } else {
        imageView_mothership_button_pushed.showOrInvisible(false)
    }

I've left out anything from that function call. I've tried many different things, but I'm just not understanding what parameter I need, or am I way off. Am I even in the right neighborhood?

EDIT Other than my lack of general knowledge about programming, I think my hangup has to do with what to do with the "inputFromBt" String. Do I need to use a buffer of some sort. I'm trying/researching/reading up on everything I can. But stalling out.

Upvotes: 3

Views: 3427

Answers (1)

CB Midkiff
CB Midkiff

Reputation: 195

Here is the code I have in place and currently working in my app:

private fun readBlueToothDataFromMothership(bluetoothSocket: BluetoothSocket) {
    Log.i(LOGTAG, Thread.currentThread().name)
    val bluetoothSocketInputStream = bluetoothSocket.inputStream
    val buffer = ByteArray(1024)
    var bytes: Int
    //Loop to listen for received bluetooth messages
    while (true) {
        try {
            bytes = bluetoothSocketInputStream.read(buffer)
            val readMessage = String(buffer, 0, bytes)
            liveData.postValue(readMessage)
        } catch (e: IOException) {
            e.printStackTrace()
            break
        }
    }
}

// display or don't star image
private fun View.showOrHideImage(imageShow: Boolean) {
    visibility = if (imageShow) View.VISIBLE else View.GONE
}

I mentioned in a comment to user frnnd, My main issue was the data being sent from my arduino. I was using println() instead of print() and that newline was messing things up.

Upvotes: 4

Related Questions