I’m losing my mind about a problem.
I’m developing an app that establishes a Bluetooth connection between two devices so that they can communicate to each other to change their own views.
Everything worked perfectly, but I needed to move some methods of my classes to an abstract class because I’d need those methods on various Activities.
This is my BluetoothMethods abstract class:
public abstract class BluetoothMethods extends AppCompatActivity {
protected CommunicationThread communicationThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
startListeningForMessages();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void startListeningForMessages() throws IOException {
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
String receivedMessage = (String) msg.obj;
try {
handleReceivedMessage(receivedMessage);
} catch (Exception e) {}
}
};
communicationThread = new CommunicationThread(BluetoothManager.getInstance().getBluetoothSocket(), handler);
communicationThread.start();
}
protected abstract void handleReceivedMessage(String message);
@Override
protected void onDestroy() {
super.onDestroy();
if (communicationThread != null) {
communicationThread.closeResources();
}
BluetoothManager.getInstance().cancelConnection();
}
}
This is my CommunicationThread class:
public class CommunicationThread extends Thread {
private InputStream inputStream;
private OutputStream outputStream;
private final BluetoothSocket socket;
private Handler handler;
public CommunicationThread(BluetoothSocket socket, Handler handler) throws IOException {
this.handler = handler;
this.socket = socket;
this.inputStream = socket.getInputStream();
this.outputStream = socket.getOutputStream();
}
@Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
try {
while (true) {
try {
bytes = inputStream.read(buffer);
if (bytes > 0) {
String receivedMessage = new String(buffer, 0, bytes);
handler.obtainMessage(0, receivedMessage).sendToTarget();
}
} catch (IOException e) {
Log.e("Exception", "Exc: ", e);
break;
}
}
} finally {
closeResources();
}
}
public void write(String message) {
try {
outputStream.write(message.getBytes());
outputStream.flush();
} catch (IOException e) {
Log.e("Exception", "Exc: ", e);
}
}
public void closeResources() {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
Log.e("Exception", "Failed to close resources: ", e);
}
}
}
And this is a part of my MultiplayerClient class that extends BluetoothMethods:
@Override
protected void handleReceivedMessage(String message) {
if (message.equals("String1")) {
Intent intent = new Intent(this, SharedTLP.class);
intent.putExtra("ROLE", "CLIENT");
startActivity(intent);
}
else if (message.equals("String2")) {
Intent intent = new Intent(this, SharedWords.class);
intent.putExtra("ROLE", "CLIENT");
startActivity(intent);
}
}
Then, in my SharedWords class, that extends BluetoothMethods as well, I defined again handleReceivedMessage:
protected void handleReceivedMessage(String message) {
...Method body...
}
Now, in another Activity (opened on the other device) when I send some messages on the first device using:
if (isDouble) {
message = ...
} else {
message = ...
}
if (communicationThread != null) {
communicationThread.write("NEW WORD:"+message);
}
Thanks to some Logs, I noticed that if isDouble is true the message is handled by SharedWords method, else it’s handled by MultiplayerClient method (and I can’t explain why).
I tried to use Overrides and toggle them, without success.
Please help me! Thanks in advance.
Andrea Orlandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.