Question

I have an android application connected to a Bluetooth mate silver chip. I'm in the process of testing the send/receive function of it. Mostly I have been following the bluetooth examples on the android dev site.

I can tell sending data works because when I write("$$$") to the chip, it enters command mode and flashes it's status LED very quickly. When the chip enters command mode, it sends a reply: "CMD". I am having trouble receiving this reply.

When I press a button, the following code is executed. mct is the global ConnectedThread that I am using to read and write. As poor form as it is, all functions are inside MainActivity.java

if(connected){
    if (cmdMode == false){
        mct.write("$$$".getBytes());  //enter command mode
            mct.listen();
        TextView lbl_history = (TextView) findViewById(R.id.lbl_history);
        lbl_history.setText(message);
        cmdMode = true;
    }
    else{
        mct.write("k,\n".getBytes());  //kill the connection
        cmdMode = false;
    }

}

My communication thread:

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    }

    public void listen() {
        handled = false;
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()
        reply=null;
        while (reply==null) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                reply = buffer.toString(); 
                    //message is a global String to store the latest message received
                    message = reply;                             
            } catch (IOException e) {
                break;
            }
        }
        reply = null;
    }
//write and cancel functions removed for simplicity
}

When I run this code, the result is a textview that says "[B@415f8910", which I assume is junk. Multiple runs of the same code will produce similar results, with the last few digits varying. The expected result would be "CMD". Any ideas on what the problem is here? I am new to android development, so any help is appreciated.

Further inspection reveals that multiple runs strictly increase "[B@415f8910", leading me to believe that it is a memory address. Still, I don't know What to do with it though.

Was it helpful?

Solution

I found the problem. Rather than straight up calling "toString()" on the array of bytes, I needed to call the String constructor to properly convert the data:

String message = new String(buffer, "UTF-8");

specifying UTF-8 is what made the difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top