简体   繁体   中英

How to send data from one activity to another. (Bluetooth)

I'm creating small Bluetooth messenger app. I have 2 activities Main Activity and Messenger Activity. Main activity have background threads for creating Bluetooth socket and managing connection. When socket is created it starts Messenger activity on both devices. Messenger activity have edit box, list view and button. Question is how can I send string that has been taken from edit box to write() function in Main Activity.

    public void write(byte[] out) {
    ConnectedThread cThread = new ConnectedThread(blueSocket);
    cThread.write(out);
}

When device gets message it sends buffer to Handler.

buffer = new byte[1024];
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();

Handler than takes string from buffer. And here is another question: How to send that string to the Messenger Activity to add it to the list view.

I am not sure if I understood perfectly - but I hope it helps:

Maybe you should do your bluetooth-socket-stuff in a service which you can start for and from your messenger activity (read http://developer.android.com/reference/android/app/Service.html ;-) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM