Skip to content

Commit ef58386

Browse files
author
SBALAVIGNESH123
committed
Fix bugs reported by code review: thread safety, socket leaks, file logic
1 parent 6fb0195 commit ef58386

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

play-services-wearable/core/src/main/java/org/microg/gms/wearable/BluetoothWearableConnection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ protected MessagePiece readMessagePiece() throws IOException {
6262
return new Wire().parseFrom(bytes, MessagePiece.class);
6363
}
6464

65+
public String getRemoteAddress() {
66+
return socket.getRemoteDevice().getAddress();
67+
}
68+
6569
@Override
6670
public void close() throws IOException {
6771
socket.close();

play-services-wearable/core/src/main/java/org/microg/gms/wearable/WearableImpl.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,29 @@ public void run() {
667667
if (adapter != null && adapter.isEnabled()) {
668668
Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
669669
for (BluetoothDevice device : bondedDevices) {
670-
// Skip if already connected
671-
if (activeConnections.containsKey(device.getAddress())) {
670+
// Synchronized check for existing connections to this device
671+
boolean isConnected = false;
672+
synchronized (activeConnections) {
673+
for (WearableConnection conn : activeConnections.values()) {
674+
if (conn instanceof BluetoothWearableConnection) {
675+
if (((BluetoothWearableConnection) conn).getRemoteAddress().equals(device.getAddress())) {
676+
isConnected = true;
677+
break;
678+
}
679+
}
680+
}
681+
}
682+
683+
if (isConnected) {
672684
continue;
673685
}
674686

675687
Log.d(TAG, "Attempting BT connection to " + device.getName() + " (" + device.getAddress() + ")");
676688

689+
BluetoothSocket socket = null;
677690
try {
678691
// Create RFCOMM socket using SPP UUID
679-
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID_WEAR);
692+
socket = device.createRfcommSocketToServiceRecord(UUID_WEAR);
680693
socket.connect();
681694

682695
if (socket.isConnected()) {
@@ -697,8 +710,10 @@ public void onMessage(WearableConnection connection, RootMessage message) {
697710
if (message.connect != null) {
698711
onConnectReceived(connection, message.connect.id, message.connect);
699712
} else if (message.filePiece != null) {
713+
// Only pass digest if this is the final piece
714+
String digest = message.filePiece.finalPiece ? message.filePiece.digest : null;
700715
handleFilePiece(connection, message.filePiece.fileName,
701-
message.filePiece.piece.toByteArray(), message.filePiece.digest);
716+
message.filePiece.piece.toByteArray(), digest);
702717
} else {
703718
Log.d(TAG, "Received message: " + message);
704719
}
@@ -727,6 +742,13 @@ public void onDisconnected() {
727742
}
728743
} catch (IOException e) {
729744
Log.d(TAG, "BT connection failed: " + e.getMessage());
745+
if (socket != null) {
746+
try {
747+
socket.close();
748+
} catch (IOException closeErr) {
749+
// Ignore
750+
}
751+
}
730752
}
731753
}
732754
}

0 commit comments

Comments
 (0)