Skip to content

Commit 849c5df

Browse files
[Android] DNSSD callback has been modified to be called from the Main Thread. (#33585)
* Fix dnssd callback thread * Restyled by google-java-format * comment * Restyled by google-java-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 7453a11 commit 849c5df

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ public void onStopDiscoveryFailed(String serviceType, int errorCode) {
172172
@Override
173173
public void onDiscoveryStopped(String serviceType) {
174174
Log.w(TAG, "Successfully stopped discovery service '" + serviceType);
175-
this.handleServiceBrowse(chipMdnsCallback);
175+
new Handler(Looper.getMainLooper())
176+
.post(
177+
() -> {
178+
this.handleServiceBrowse(chipMdnsCallback);
179+
});
176180
}
177181

178182
public void handleServiceBrowse(ChipMdnsCallback chipMdnsCallback) {

src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java

+42-29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.net.nsd.NsdManager;
2222
import android.net.nsd.NsdServiceInfo;
2323
import android.net.wifi.WifiManager.MulticastLock;
24+
import android.os.Handler;
25+
import android.os.Looper;
2426
import android.util.Log;
2527
import androidx.annotation.Nullable;
2628
import java.util.concurrent.Executors;
@@ -118,20 +120,24 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
118120
Log.w(
119121
TAG,
120122
"Failed to resolve service '" + serviceInfo.getServiceName() + "': " + errorCode);
121-
chipMdnsCallback.handleServiceResolve(
122-
serviceInfo.getServiceName(),
123-
// Use the target service info since the resolved service info sometimes appends a
124-
// "." at the front likely because it is trying to strip the service name out of it
125-
// and something is missed.
126-
// The target service info service type should be effectively the same as the
127-
// resolved service info.
128-
NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
129-
null,
130-
null,
131-
0,
132-
null,
133-
callbackHandle,
134-
contextHandle);
123+
new Handler(Looper.getMainLooper())
124+
.post(
125+
() -> {
126+
chipMdnsCallback.handleServiceResolve(
127+
serviceInfo.getServiceName(),
128+
// Use the target service info since the resolved service info sometimes
129+
// appends a "." at the front likely because it is trying to strip the
130+
// service name out of it and something is missed.
131+
// The target service info service type should be effectively the same as
132+
// the resolved service info.
133+
NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
134+
null,
135+
null,
136+
0,
137+
null,
138+
callbackHandle,
139+
contextHandle);
140+
});
135141

136142
if (multicastLock.isHeld()) {
137143
multicastLock.release();
@@ -153,21 +159,28 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) {
153159
+ serviceInfo.getHost()
154160
+ ", type : "
155161
+ serviceInfo.getServiceType());
156-
// TODO: Find out if DNS-SD results for Android should contain interface ID
157-
chipMdnsCallback.handleServiceResolve(
158-
serviceInfo.getServiceName(),
159-
// Use the target service info since the resolved service info sometimes appends a
160-
// "." at the front likely because it is trying to strip the service name out of it
161-
// and something is missed.
162-
// The target service info service type should be effectively the same as the
163-
// resolved service info.
164-
NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
165-
serviceInfo.getHost().getHostName(),
166-
serviceInfo.getHost().getHostAddress(),
167-
serviceInfo.getPort(),
168-
serviceInfo.getAttributes(),
169-
callbackHandle,
170-
contextHandle);
162+
final String hostName = serviceInfo.getHost().getHostName();
163+
final String address = serviceInfo.getHost().getHostAddress();
164+
final int port = serviceInfo.getPort();
165+
new Handler(Looper.getMainLooper())
166+
.post(
167+
() -> {
168+
// TODO: Find out if DNS-SD results for Android should contain interface ID
169+
chipMdnsCallback.handleServiceResolve(
170+
serviceInfo.getServiceName(),
171+
// Use the target service info since the resolved service info sometimes
172+
// appends a "." at the front likely because it is trying to strip the
173+
// service name out of it and something is missed.
174+
// The target service info service type should be effectively the same as
175+
// the resolved service info.
176+
NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
177+
hostName,
178+
address,
179+
port,
180+
serviceInfo.getAttributes(),
181+
callbackHandle,
182+
contextHandle);
183+
});
171184

172185
if (multicastLock.isHeld()) {
173186
multicastLock.release();

0 commit comments

Comments
 (0)