I have had this problem for several days now and cannot find the way to solve it. To learn more about Android development, I started to work on a newbie Bluetooth chat application. When testing the functionality with two physical devices, it is possible to send and receive text messages (as Logcat shows), but the incoming messages are not showing up in the ChatActivity/UI.
Can maybe and please someone give me a hint on how to solve this?
Thanks a lot in advance.
—
Relevant code fragments:
This method is part of the BluetoothUtilities class, inside an inner class called ConnectedThread. It supposedly should send the incomming Message to the ChatActivity class:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = inputStream.read(buffer);
//
byte[] readBuf = new byte[bytes];
System.arraycopy(buffer, 0, readBuf, 0, bytes);
handler.obtainMessage(ChatActivity.MESSAGE_READ, bytes, -1, readBuf).sendToTarget();
String receivedMessage = new String(readBuf, 0, bytes);
handleMessageReceived(buffer, bytes);
Log.e(TAG, "Received message (BU run): " + receivedMessage);
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
connectionLost();
BluetoothUtilities.this.start();
break;
}
}
//This shows up later in the class, just a redundant method that does not send anything to ChatActivity either
//Yet the log message is showing up in Logcat of the other device, so it sends the message successfully so far
private void handleMessageReceived(byte[] buffer, int bytes) {
String message = new String(buffer, 0, bytes);
Log.d("BluetoothUtilities", "Message received BU handleMR: " + message);
handler.obtainMessage(ChatActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
And this is the relevant part of the Chat Activity class:
// Handles messages from BluetoothUtilities
private final Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGED:
Log.d(TAG, "MESSAGE_STATE_CHANGED: " + msg.arg1);
switch (msg.arg1) {
case BluetoothUtilities.STATE_NONE:
case BluetoothUtilities.STATE_LISTEN:
setState("Not connected");
break;
case BluetoothUtilities.STATE_CONNECTING:
setState("Connecting...");
break;
case BluetoothUtilities.STATE_CONNECTED:
setState("Connected: " + contactName);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
String writeMessage = new String(writeBuf);
Log.d(TAG, "MESSAGE_WRITE CA: " + writeMessage);
chatStorage.saveMessage(contactAddress, "Me: " + writeMessage);
addMessage("Me 4: " + writeMessage);
loadMessages();
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf, 0, msg.arg1);
Log.d(TAG, "MESSAGE_READ: " + readMessage);
onMessageReceived(contactName + ": " + readMessage); // Call to handle received message
chatStorage.saveMessage(contactAddress, contactName + ": " + readMessage);
addMessage(contactName + ": " + readMessage);
loadMessages();
return true;
//break;
case MESSAGE_DEVICE_NAME:
contactName = msg.getData().getString(DEVICE_NAME);
break;
case MESSAGE_TOAST:
Toast.makeText(ChatActivity.this, msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
// Later in the class:
//Call this method when a new message is received
//This log message never shows up in Logcat
public void onMessageReceived(String message) {
Log.d(TAG, "Message received in ChatActivity: " + message);
//messageList.add("Received: " + message);
addMessage(contactName + "CA: " + message); // This should update the UI, but does not work
adapter.notifyDataSetChanged();
}
This is the ChatActivity layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ChatActivity">
<!-- Header with Back and Menu Buttons -->
<LinearLayout
android:id="@+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageButton
android:id="@+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_back" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Chat"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="center"/>
<ImageButton
android:id="@+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_menu" />
</LinearLayout>
<!-- Chat Messages List -->
<ListView
android:id="@+id/chat_list_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/header_layout"
android:layout_above="@id/input_layout"
android:padding="10dp" />
<!-- Input Layout for Messages -->
<LinearLayout
android:id="@+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="10dp">
<EditText
android:id="@+id/message_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Write a message" />
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
</RelativeLayout>
What I tried:
Write several log messages to be sent in different points to know what is failing. So far I only know the messages are being received, but incoming messages are not being added to the ListView.
The sent messages are appearing in the ChatActivity UI (added to the ListView).